Friday, 15 April 2011

c# - Confused about DatagridviewRow -



c# - Confused about DatagridviewRow -

i trying retrieve info database , bind them datagridviewview command have added. problem when execute code getting exception:

rows cannot programmatically added datagridview's rows collection when command data-bound.

i have noticed columns appearing twice when programme runs. reason exception? when set autogeneratecolumns property false instead, nil display.

what doing wrong?

private void button1_click(object sender, eventargs e) { datagridview1.rows.clear(); sqlconnection con = new sqlconnection("server=.\\sqlexpress;database=projects;user id=sa;password=1234"); datatable dt = new datatable(); sqldataadapter adp = new sqldataadapter("select * info",con); adp.fill(dt); datagridview1.datasource = dt; foreach (datarow item in dt.rows) { int n = datagridview1.rows.add(); datagridview1.rows[n].cells[0].value = item["id"].tostring(); datagridview1.rows[n].cells[1].value = item["name"].tostring(); datagridview1.rows[n].cells[2].value = item["code"].tostring(); } }

c#

No comments:

Post a Comment