Click here to Skip to main content
15,904,346 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to change width of column in datagridview by adding these lines

VB
Dim column As DataGridViewColumn = DataGridView1.Columns(0)

           column.Width = 50

But i am getting ERROR:Index out of range.must be non negitive and less than the size of the collection.parameter name:index

can anyone help me plz..
Posted
Comments
Karwa_Vivek 23-May-11 6:24am    
What you really want to do with this code

Just want to Set the width of column or
whole of the DataGridView

1 solution

To set the Width of your DataGridView Coulumn
you First need to fill it with the Required Data.If it will not have any data it will surely throw this exception.Set the Datasource of DataGridView First, then add the code to set the width.Also if in your Database table there is no data then also this problem will happen.
Use Try Catch Exception handling to remove this exception.
Also in place of writing the this code in two line you can reduce it to
DataGridView1.Columns(0).width =50
Try this
 
Share this answer
 
v2
Comments
Member 7940184 23-May-11 7:11am    
Ya its working thank you..
Karwa_Vivek 23-May-11 7:21am    
glad to help you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900