2
Reply

datagridview's column's width problem

sachi vasishta

sachi vasishta

Jun 1 2009 2:48 AM
17.4k

Hi,
In a form's form_load event I will retrive some info from db and display it on the dgv. Before I will display it on the dgv I want to change the width of some of dgv's columns, for that I have written the following code but when I run this it is giving me the following error.
ArgumentOutOfRangeException was unhandled.
Index was out of range. Must be  non-negative and less than the size of the collection.
 
private
void Form40_Load(object sender, EventArgs e)
{
Form41.ActiveForm.Close();
dataGridView1.Columns[1].Width = 300;
dataGridView1.Columns[2].Width = 300;
dataGridView1.Columns[10].Width = 350;
Class1 obj = new Class1();
string sql = string.Format("select officeid as Hospital_Id,officename as Hospital_Name,address as Address,city as City,state as State,zip as Zip,officephno as Phone,fax as Fax,tollfreeno as TollFree_No,officeemail as Email from officetable");
DataTable dt = new DataTable();
dt = obj.gettable(sql);
dataGridView1.DataSource = dt;
obj.closecon();

}

 

Answers (2)