Reputation: 101
Through Edit link button I'm able to edit record using code below . As you can see here number of columns are 4 , for fixed column number this code is fine but In my condition Column numbers are not fixed ,they may 4 or may be 5 for next insert. How can I get column names and create that number of string variable, so that I can assign string values to particular field ?
protected void OnUpdate(object sender, EventArgs e)
{
GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;
string a = (row.Cells[3].Controls[0] as TextBox).Text;
string b = (row.Cells[4].Controls[0] as TextBox).Text;
string c = (row.Cells[5].Controls[0] as TextBox).Text;
string d = (row.Cells[6].Controls[0] as TextBox).Text;
DataTable dt = ViewState["dt"] as DataTable;
dt.Rows[row.RowIndex]["Column0"] = a;
dt.Rows[row.RowIndex]["Column1"] = b;
dt.Rows[row.RowIndex]["Column2"] = c;
dt.Rows[row.RowIndex]["Column3"] = d;
ViewState["dt"] = dt;
GridView1.EditIndex = -1;
this.BindGrid();
btnGetSelected.Visible = true;
}
Upvotes: 1
Views: 430
Reputation: 101
This worked for me.
protected void OnUpdate(object sender, EventArgs e)
{
GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;
DataTable dt = ViewState["dt"] as DataTable;
int j=0;
for (int i = 3; i < row.Cells.Count; i++)
{
string a = (row.Cells[i].Controls[0] as TextBox).Text;
dt.Rows[row.RowIndex][j] = a;
j++;
}
ViewState["dt"] = dt;
GridView1.EditIndex = -1;
this.BindGrid();
btnGetSelected.Visible = true;
}
Upvotes: 1
Reputation: 434
Do you mean you need the columns names of the GridView
?
You could use either of these
gv.HeaderRow.Cells[i].Text
gv.Rows[0].Cells[i].Text
It you want column names of DataTable
use
string[] columnNames = dt.Columns.Cast<DataColumn>()
.Select(x => x.ColumnName)
.ToArray();
Upvotes: 1