I fetch the data datafrom database in to a datatable and bind it to a datagridview.Now I want to edit the data shown in the datagridviewand also want to add new rows in dgv.The editing part is going ok but I m not able to add new rows in dgv.Also I want S.No. column to be autogenerated on new row...the coding is below

if (con.State == ConnectionState.Closed)
{
con.Open();
}
DataColumn Col;

Col = new DataColumn();
Col.DataType = System.Type.GetType("System.String");
Col.ColumnName = "S.No.";
dt_medilist1.Columns.Add(Col);
SqlDataAdapter adp1 = new SqlDataAdapter("Select Drugs,Schedule,Instructions,(DateTo)Till,Route,(Qty)Quantity,Comments from tblTreatment where Patient=111", con);
adp1.Fill(dt_medilist1);
int rows = dt_medilist1.Rows.Count;

for (int i = 0; i < rows; i++)
{
dt_medilist1.Rows[i]["S.No."] = i + 1;
}
dataGridView1.DataSource = dt_medilist1;

Did you enable adding records in your datagridview, and if you're manually adding them have you tried adding them to the datasource?

It is impossible to add a datagridviewrow to a data bound data grid, if you do you will recieve an exception at run time

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.