Anand Deva

Anand Deva

  • NA
  • 43
  • 4.3k

GridView Bulk Update using check box selection

Jun 25 2018 11:48 PM

I have three issues to be fixed.
1. Editable template Field to be bounded correctly. This is not binding my value
2. When I check multiple checkboxes only odd numbered rows are enabled.
3. on clicking update button I should copy the checked rows to a datatable and pass this to data access layer
Please find the code below
.ascx

<div> <asp:GridView ID="GVErrorRecords" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Select" ItemStyle-Width = "150">
<ItemTemplate>
<asp:CheckBox runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="s_no" HeaderText="S.No" ItemStyle-Width="150" />
<asp:BoundField DataField="err_flg" HeaderText="UnFlag Error" ItemStyle-Width="150" />
<asp:BoundField DataField="spnd_dt" HeaderText="Date" ItemStyle-Width="150" />

<asp:TemplateField HeaderText="Spend Purpose" ItemStyle-Width = "150">
<AlternatingItemTemplate>
<asp:Label ID="lblSpendPurpose" runat="server" Text='<% #Eval("SPNDPRPS") %>'>
</asp:Label>
</AlternatingItemTemplate>
<ItemTemplate>
<asp:TextBox ID="TxtSpendPurpose" runat="server" Text='<% #Eval("SPNDPRPS") %>' Visible="false">
</asp:TextBox>
</ItemTemplate>

</asp:TemplateField> </Columns>
</asp:GridView>


</div>
<div class="btn-container align-right">
<asp:Button ID="btnUpdate" runat="server" CssClass="btn orange search-submit searchresults" CausesValidation="true" Text="Update" OnClick = "Update" Visible = "false"/>

</div>

.cs file
protected void btnSearch_Click(object sender, EventArgs e)
{

BindGrid3();

}
private void BindGrid3()
{
DataSet dsErrorRecords = new DataSet();
Return returnEntity = GetErrorDetails.Get(objCurrentUser.Country, objCurrentUser.Division, txtFromDate.Text, txtToDate.Text,ddlErrorDescription.SelectedValue);
dsErrorRecords = returnEntity.ResultSet;
if(dsErrorRecords != null)
GVErrorRecords.DataSource = dsErrorRecords.Tables[0];
GVErrorRecords.DataBind();
}

protected void OnCheckedChanged(object sender, EventArgs e)
{
bool isUpdateVisible = false;

foreach (GridViewRow row in GVErrorRecords.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 (row.Cells[i].Controls.OfType<DropDownList>().ToList().Count > 0)
{
row.Cells[i].Controls.OfType<DropDownList>().FirstOrDefault().Visible = isChecked;
}
if (isChecked && !isUpdateVisible)
{
isUpdateVisible = true;
}

}
}
}
btnUpdate.Visible = isUpdateVisible;
}
protected void Update(object sender, EventArgs e)
{
foreach (GridViewRow row in GVErrorRecords.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
bool isChecked = row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked;
if (isChecked)
{

}
}
}
btnUpdate.Visible = false;
this.BindGrid3();
}

Answers (2)