.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Code Snippets >> DataGrid >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Auto resize column in datagridview in Windows Form Application

Posted By:Amit Mehra       Posted Date: May 15, 2013    Points: 40    Category: DataGrid    URL: http://www.dotnetspark.com  

In this code example we will see how to auto resize of a perticular or all columns in datagridview in Windows Form Application using C#

By Default DataGridView arrange the column width by itself and in this case if some column data length is more it truncate the data and to view this data user have re size the column width by dragging the column header.

AutoResizeColumn() Method of the DataGridView give developer flexibility to resize of a particular column to be Auto Resize based on its data.

AutoResizeColumn() takes index of the column as in input which tells which column you want to resize.

Auto resize of a perticular column

In below code myDataGridView is my DataGridView and column index 1 I would like to autoresize when data is bind to the dataGridView

myDataGridView.AutoResizeColumn( 1 );

Note: Call the above method after binding the data to DatGridView else it will throw exception.

Auto resize of all columns

Similarly if you would like to auto resize all the columns available in DataGridView than call AutoResizeColumns( )

myDataGridView.AutoResizeColumns( );



Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend