7
Answers

Unable to edit rows in grid with checkbox

Vignesh Kumar

Vignesh Kumar

11y
1.3k
1
Hi All,
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".

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="EmpID"
  AllowPaging="true" ShowFooter="true" OnPageIndexChanging="PageIndexChanging"
  PageSize="10" PagerStyle-ForeColor="#000000">
  <Columns>
  <asp:TemplateField>
  <HeaderTemplate>
  <asp:CheckBox ID="chkAll" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
  </HeaderTemplate>
  <ItemTemplate>
  <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
  </ItemTemplate>
  </asp:TemplateField>
  <asp:TemplateField HeaderText="EAI Code" ItemStyle-Width="75">
  <ItemTemplate>
  <asp:Label ID="lblEmpID" runat="server" Text='<%# Eval("EmpID") %>'></asp:Label>
  <asp:TextBox ID="txtEmpID" runat="server" Text='<%# Eval("EmpID") %>' Visible="false"></asp:TextBox>
  </ItemTemplate>
  <FooterTemplate>
  <asp:TextBox ID="txtEmpID" runat="server" Width="75px"></asp:TextBox>
  </FooterTemplate>
  <ItemStyle Width="100px" />
  </asp:TemplateField>
  <asp:TemplateField HeaderText="Name" ItemStyle-Width="150">
  <ItemTemplate>
  <asp:Label ID="lblName" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
  <asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>' Visible="false"></asp:TextBox>
  </ItemTemplate>
  <FooterTemplate>
  <asp:TextBox ID="txtName" runat="server" Width="100px"></asp:TextBox>
  </FooterTemplate>
  <ItemStyle Width="150px" />
  </asp:TemplateField>
  <asp:TemplateField HeaderText="Group" ItemStyle-Width="150">
  <ItemTemplate>
  <asp:Label ID="lblDesignation" runat="server" Text='<%# Bind("Designation") %>'></asp:Label>
  <asp:TextBox ID="txtDesignation" runat="server" Text='<%# Bind("Designation") %>'
  Visible="false"></asp:TextBox>
  </ItemTemplate>
  <FooterTemplate>
  <asp:TextBox ID="txtDesignation" runat="server" Width="100px"></asp:TextBox>
  </FooterTemplate>
  <ItemStyle Width="150px" />
  </asp:TemplateField> 
  <asp:TemplateField HeaderText="City" ItemStyle-Width="150">
  <ItemTemplate>
  <asp:Label ID="City" runat="server" Text='<%# Bind("City") %>'></asp:Label>
  <asp:TextBox ID="txtCity" runat="server" Text='<%# Bind("City") %>' Visible="false"></asp:TextBox>
  </ItemTemplate>
  <FooterTemplate>
  <asp:TextBox ID="txtCity" Width="100px" runat="server"></asp:TextBox>
  </FooterTemplate>
  <ItemStyle Width="150px" />
  </asp:TemplateField>
  <asp:TemplateField HeaderText="">
  <FooterTemplate>
  <asp:LinkButton ID="lbtnBulkUpdate" ForeColor="#333333" Width="65px" runat="server" CausesValidation="False"
  CommandName="BulkUpdate" Text="Bulk Update"></asp:LinkButton>
  </FooterTemplate>
  </asp:TemplateField>
  </Columns>
  <HeaderStyle BackColor="#8c8f96" Font-Bold="True" ForeColor="White" />
  <RowStyle BackColor="#f7f7f7" />
  <FooterStyle BackColor="#f7f7f7" ForeColor="White" Font-Bold="True" />
  <EditRowStyle BackColor="#D1DDF1" />
  <SelectedRowStyle BackColor="#D1DDF1" ForeColor="#333333" />
  </asp:GridView>

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>().FirstOrDefault().Checked = chk.Checked;
  }
  }
  }
  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<CheckBox>().FirstOrDefault().Checked;
  for (int i = 1; i < row.Cells.Count; i++)
  {
  row.Cells[i].Controls.OfType<Label>().FirstOrDefault().Visible = !isChecked;
  if (row.Cells[i].Controls.OfType<TextBox>().ToList().Count > 0)
  {
  row.Cells[i].Controls.OfType<TextBox>().FirstOrDefault().Visible = isChecked;
  }
  if (isChecked && !isUpdateVisible)
  {
  isUpdateVisible = true;
  }
  if (!isChecked)
  {
  chkAll.Checked = false;
  }
  }
  }
  }
  btnSave.Visible = isUpdateVisible;
  } 
Answers (7)