I have a grid with checkbox where I can select multiple rows and edit. But when I added the FooterTemplate to the grid for columns, I am not able to edit the grid. Its like I am trying to add textbox below each column in the grid. Can anyone help me solve this. When I try to click on the checkbox, I get this error "Object reference not set to an instance of an object".
PageSize="10" PagerStyle-ForeColor="#000000">
CODE BEHIND:
protected void OnCheckedChanged(object sender, EventArgs e)
{
bool isUpdateVisible = false;
CheckBox chk = (sender as CheckBox);
if (chk.ID == "chkAll")
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
row.Cells[0].Controls.OfType
}
}
}
CheckBox chkAll = (GridView1.HeaderRow.FindControl("chkAll") as CheckBox);
chkAll.Checked = true;
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
bool isChecked = row.Cells[0].Controls.OfType
for (int i = 1; i < row.Cells.Count; i++)
{
row.Cells[i].Controls.OfType
if (row.Cells[i].Controls.OfType
{
row.Cells[i].Controls.OfType
}
if (isChecked && !isUpdateVisible)
{
isUpdateVisible = true;
}
if (!isChecked)
{
chkAll.Checked = false;
}
}
}
}
btnSave.Visible = isUpdateVisible;
}
Jaganathan BantheswaranPosted Nov 13, 2013, 4:23 AM
Try by adding if condition,
if (row.Cells[i].Controls.OfType
Vignesh KumarPosted Nov 14, 2013, 1:41 AM
protected void MassSave(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
bool isChecked = row.Cells[0].Controls.OfType
if (isChecked)
{
SqlCommand cmd = new SqlCommand("UPDATE AppInvent_Test SET Name = @Name, Designation = @Designation, City = @City WHERE EmpID = @EmpID");
cmd.Parameters.AddWithValue("@EmpID", row.Cells[1].Controls.OfType
cmd.Parameters.AddWithValue("@Name", row.Cells[2].Controls.OfType
cmd.Parameters.AddWithValue("@Designation", row.Cells[3].Controls.OfType
cmd.Parameters.AddWithValue("@City", row.Cells[4].Controls.OfType
this.ExecuteQuery(cmd, "SELECT");
}
}
}
this.FillVendorGrid();
}
But if this is the case, I will probably not be able to convert textbox to bool.
Jaganathan BantheswaranPosted Nov 13, 2013, 11:26 PM
row.Cells[0].Controls.OfType
So check for NULL wherever required.
Vignesh KumarPosted Nov 13, 2013, 5:07 AM
Is it possible to do this scenario for the same grid?
I have a grid where I use a checkbox for bulk update by selecting multiple rows. Now the footer template with the textbox , assume like I select two rows in the grid(using checkbox) and when I enter values in the textbox in footertemplate below the grid, then changes have to be reflected to the selected rows in the grid for the columns(to the DB). And I use the below code for updating the grid wirh checkbox.
protected void Save(object sender, EventArgs e)().FirstOrDefault().Checked;().FirstOrDefault().Text);().FirstOrDefault().Text);().FirstOrDefault().Text);().FirstOrDefault().Text);
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
bool isChecked = row.Cells[0].Controls.OfType
if (isChecked)
{
SqlCommand cmd = new SqlCommand("UPDATE AppInvent_Test SET Name = @Name, Designation = @Designation, City = @City WHERE EmpID = @EmpID");
cmd.Parameters.AddWithValue("@EmpID", row.Cells[1].Controls.OfType
cmd.Parameters.AddWithValue("@Name", row.Cells[2].Controls.OfType
cmd.Parameters.AddWithValue("@Designation", row.Cells[3].Controls.OfType
cmd.Parameters.AddWithValue("@City", row.Cells[4].Controls.OfType
this.ExecuteQuery(cmd, "SELECT");
}
}
}
this.FillVendorGrid();
}
Vignesh KumarPosted Nov 13, 2013, 4:38 AM
Vignesh KumarPosted Nov 13, 2013, 4:15 AM
row.Cells[i].Controls.OfType
Attached is the screenshot of the error.
Jaganathan BantheswaranPosted Nov 13, 2013, 3:44 AM
At which line, you are getting this error?