Click here to Skip to main content
15,894,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends,
First Of all Happy Valentine Day to All of You.
now come to my question
in my page i am binding a gridview using List.
i have different fields on my page and storing those fields values into list and binding it with a grid
it is working well but now i want to update gridview using checkbox in gridview,on clicking on checkbox the associated values in that row comes on the different fields and on button save click gridview should be updated.
i don't know how to achieve it.
kindly help me fix this issue.

Thanks In Advance
My HTML Code is like this
ASP.NET
<table class="tblPopUp">
<tr>
<td>
<asp:Label ID="lblCaste" CssClass="Label" runat="server" Text="Caste"></asp:Label>
</td>
<td>
 <asp:DropDownList ID="ddlCaste" runat="server" CssClass="Dropdownlist myddlinPopUp"></asp:DropDownList>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblNo_OfPosition" CssClass="Label" runat="server" Text="No Of Position"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtNoOfPositioninPopUp" runat="server" CssClass="TextBox mytextboxinPopUp"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblMinAge" CssClass="Label" runat="server" Text="Min Age"></asp:Label>
 </td>
 <td>
 <asp:TextBox ID="txtMinAge" runat="server" CssClass="TextBox mytextboxinPopUp"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblMaxAge" CssClass="Label" runat="server" Text="Max Age"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtMaxAge" runat="server" CssClass="TextBox mytextboxinPopUp"></asp:TextBox>
  </td>
</tr>
<tr>                    
<td>
<asp:Button ID="btnSaveInPopUpReservation" runat="server" CssClass="button" Text="Save" OnClick="btnSaveInPopUpReservation_Click" />
 </td>
</tr>
</table>
<asp:GridView ID="gridReservationDetails" runat="server" GridLines="Both" AutoGenerateColumns="false" CssClass="gridview" ShowHeaderWhenEmpty="true">
<Columns>
<asp:TemplateField HeaderStyle-HorizontalAlign="Left">
<HeaderTemplate>
<asp:CheckBox ID="checkAll" runat="server" onclick="checkAll(this);" />
 </HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" onclick="Check_Click(this)" />
 </ItemTemplate>
  <HeaderStyle Font-Bold="False" />
 <ItemStyle Font-Bold="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Sl. No" HeaderStyle-HorizontalAlign="Left" ItemStyle-CssClass="item">
<ItemTemplate>
 <%# Container.DataItemIndex + 1 %>
 </ItemTemplate>
</asp:TemplateField>
 <asp:BoundField HeaderText="Caste" DataField="casteName" ItemStyle-CssClass="item" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField HeaderText="No Of Position" DataField="NoOfPosition" ItemStyle-CssClass="item" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField HeaderText="Min Age" DataField="MinAge" ItemStyle-CssClass="item" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField HeaderText="Max Age" DataField="MaxAge" ItemStyle-CssClass="item" HeaderStyle-HorizontalAlign="Left" />

</Columns>
</asp:GridView>


my code behind for binding list values is like this
C#
public class CommonHRMS_PositionRequisitionReservationDetails
    {
        public int casteID { get; set; }
        public string casteName { get; set; }
        public int NoOfPosition { get; set; }
        public int MaxAge { get; set; }
        public int MinAge { get; set; }
    }

C#
protected List<CommonHRMS_PositionRequisitionReservationDetails> FillReservation()
       {
           List<CommonHRMS_PositionRequisitionReservationDetails> lst = new List<CommonHRMS_PositionRequisitionReservationDetails>();
           List<CommonHRMS_PositionRequisitionReservationDetails> lstPrevious = new List<CommonHRMS_PositionRequisitionReservationDetails>();
           lstPrevious = (List<CommonHRMS_PositionRequisitionReservationDetails>)Session["cprrd"];
           if (lstPrevious != null)
           {
               CommonHRMS_PositionRequisitionReservationDetails obj = new CommonHRMS_PositionRequisitionReservationDetails();
               obj.casteID = int.Parse(ddlCaste.SelectedValue);
               obj.casteName = ddlCaste.SelectedItem.ToString();
               obj.NoOfPosition = int.Parse(txtNoOfPositioninPopUp.Text);
               obj.MaxAge = int.Parse(txtMaxAge.Text);
               obj.MinAge = int.Parse(txtMinAge.Text);
               lstPrevious.Add(obj);
               Session["cprrd"] = lstPrevious;
               return lstPrevious;
           }
           else
           {
               CommonHRMS_PositionRequisitionReservationDetails obj = new CommonHRMS_PositionRequisitionReservationDetails();
               obj.casteID = int.Parse(ddlCaste.SelectedValue);
               obj.casteName = ddlCaste.SelectedItem.ToString();
               obj.NoOfPosition = Convert.ToInt32(txtNoOfPositioninPopUp.Text);
               obj.MaxAge = Convert.ToInt32(txtMaxAge.Text);
               obj.MinAge = Convert.ToInt32(txtMinAge.Text);
               lst.Add(obj);
               Session["cprrd"] = lst;
               return lst;
           }
       }

C#
protected void btnSaveInPopUpReservation_Click(object sender, EventArgs e)
      {
        List<CommonHRMS_PositionRequisitionReservationDetails> lst = FillReservation();
        gridReservationDetails.DataSource = lst;
        gridReservationDetails.DataBind();
      }
Posted

1 solution

Okay, logic would be following.
  • Place a Update Button.
  • Handle Update Button Click Event.
  • Inside that Button, Loop through all the GridView Rows.
  • Inside the Loop find the CheckBox Control.
  • If that CheckBox is checked, append Update Statement to declared StringBuilder.
  • After the Loop, Execute the that StringBuilder and your update will be done.

See one Example - Edit Update Delete Multiple Records/Rows Gridview With Checkbox[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900