dr.BeginEdit()
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { GridView _gridView = (GridView)sender; if (e.RowIndex > -1) { // Loop though the columns to find a cell in edit mode for (int i = _firstEditCellIndex; i < _gridView.Columns.Count; i++) { // Get the editing control for the cell Control _editControl = _gridView.Rows[e.RowIndex].Cells[i].Controls[0]; if (_editControl.Visible) { int _dataTableColumnIndex = i - 1; try { // Get the id of the row Label idLabel = (Label)_gridView.Rows[e.RowIndex].FindControl("lblMessage"); String QuestionNo = (lblMessage.Text).ToString(); // Get the value of the edit control and update the DataTable DataTable dt = _sampleData; DataRow dr = dt.Rows.Find(QuestionNo); dr.BeginEdit(); if (_editControl is TextBox) { dr[_dataTableColumnIndex] = ((TextBox)_editControl).Text; } //else if (_editControl is DropDownList) //{ // dr[_dataTableColumnIndex] = ((DropDownList)_editControl).SelectedValue; //} //else if (_editControl is CheckBox) //{ // dr[_dataTableColumnIndex] = ((CheckBox)_editControl).Checked; //} dr.EndEdit(); // Save the updated DataTable _sampleData = dt; // Clear the selected index to prevent // another update on the next postback _gridView.SelectedIndex = -1; // Repopulate the GridView _gridView.DataSource = dt; _gridView.DataBind(); } catch (ArgumentException) { this.lblMessage.Text += "Error updating GridView row at index " + e.RowIndex + "<br />"; // Repopulate the GridView _gridView.DataSource = _sampleData; _gridView.DataBind(); } } } } }
DataRow dr = dt.Rows.Find(QuestionNo); //this probably returns null if(Equals(dr, null)) { //do whatever you want. or continue the loop continue; } dr.BeginEdit();
if (dr != null)
DataTable dt = new DataTable(); dt = _sampleData; DataRow dr = dt.Rows.Find(QuestionNo); dr.BeginEdit();
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)