I have added a couple new colums to the Gridview such as:
Here's the c# code:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int regno = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lblREGNO = (Label)row.FindControl("lblREGNO");
TextBox textName = (TextBox)row.Cells[0].Controls[0];
TextBox textc = (TextBox)row.Cells[1].Controls[0];
TextBox textmob = (TextBox)row.Cells[2].Controls[0];
TextBox textdob = (TextBox)row.Cells[3].Controls[0];
TextBox textgen = (TextBox)row.Cells[4].Controls[0];
TextBox textadd = (TextBox)row.Cells[5].Controls[0];
GridView1.EditIndex = -1; cn.Open();
SqlCommand cmd = new SqlCommand("update student set name='" + textName.Text + "',address='" + textadd.Text + "',class='" + textc.Text + "',gender='" + textgen.Text + "',contact='" + textmob.Text + "',dob='" + textdob.Text + "'where regno='" + regno + "'", cn); cmd.ExecuteNonQuery();
cn.Close();
gvbind();
}
However, i'm getting an error that says: A field or property with the name 'mob' was not found on the selected data source. According to the original code, there were only 3 columns in the Gridview, I have added 4 more columns, but am getting an error saying the column is not found. How do i add new columns to the gridview, and be able to edit and delete them accoring to the above code? It is a program to edit and delete student details in Gridview.
Jayraj ChhayaPosted Mar 27, 2024, 9:05 AM
Hi Sumathi,
When adding new columns to a GridView dynamically, it's essential to ensure that the DataField property in the BoundField matches the actual field name in the data source. In this case, the 'mob' field is not found because it doesn't exist in the data source bound to the GridView.
To resolve this issue, you need to update the DataField property of the 'mob' BoundField to match the actual field name in the data source. Additionally, ensure that the field names in the C# code match the DataField values in the GridView columns.
Here's an example of how to modify the 'mob' BoundField in the GridView:
By changing the DataField value to "contact" (assuming that is the actual field name in the data source), you should no longer encounter the error related to the 'mob' field not being found. Make sure to adjust the C# code accordingly to reflect any changes in the GridView column names.