-2

I want to insert specific row values based on check box click. The design and source is given below. From the design I have to select some Items. If I click approve, I have to store those check box checked row values into DB. Help me to find a proper solution.

Design:

enter image description here

ASPX:

enter image description here

C#:

The given below code is getting the entire gridview value and storing into db. How can I modify this code as per above requirements.

protected void btnApprove_Click(object sender, EventArgs e)
    {

       ShadingAnalysisDataSetTableAdapters.tbl_ItemRequest_StatusTableAdapter rs;
       rs = new ShadingAnalysisDataSetTableAdapters.tbl_ItemRequest_StatusTableAdapter();

       foreach (GridViewRow row in GridView2.Rows)
        {
            string ItemName = row.Cells[0].Text;
            string Quantity = row.Cells[1].Text;

            rs.testInsert(ItemName, Quantity);
        }
    }
Vipin
  • 261
  • 6
  • 20
  • 44

3 Answers3

3

You need to iterate through your gridview and find check box by Id in cell of current row.

foreach (GridViewRow row in GridView2.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            CheckBox chkRow = (row.Cells[2].FindControl("CheckBox1") as CheckBox);
            bool chk = chkRow.Checked;
            // Do your stuff
        }
    }

Source: http://www.aspsnippets.com/Articles/GridView-with-CheckBox-Get-Selected-Rows-in-ASPNet.aspx

Imad
  • 7,126
  • 12
  • 55
  • 112
1

The given below code is working perfectly.

foreach (GridViewRow row in GridView2.Rows)
    {
      if (row.RowType == DataControlRowType.DataRow)
        {
           CheckBox chkRow = (row.Cells[2].FindControl("CheckBox1") as CheckBox);
             if (chkRow.Checked)
                {
                   string ItemName = row.Cells[0].Text;
                   string Quantity = row.Cells[1].Text;

                   rs.testInsert(ItemName, Quantity);
                }
        }
    }
Vipin
  • 261
  • 6
  • 20
  • 44
0

Here is my GridView

<asp:GridView
                ID="grdAccounts"
                class="table table-condensed"
                runat="server"
                AutoGenerateColumns="False"
                CellPadding="4"
                ForeColor="#333333"
                GridLines="None">
                <AlternatingRowStyle BackColor="White" />
                <Columns>
                    <asp:BoundField DataField="CUST_NAME" HeaderText="Name" />
                    <asp:BoundField DataField="CUST_NUMBER" HeaderText="Account Number" />
                    <asp:BoundField DataField="BR_CODE" HeaderText="CIF Number" />
                    <asp:TemplateField HeaderText="Select" >
                        <EditItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" ItemStyle-HorizontalAlign="Right"  />
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" onclick="SingleCheckboxCheck(this)" OnCheckedChanged="CheckBox1_CheckedChanged" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <EditRowStyle BackColor="#7C6F57" />
                <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                <PagerStyle CssClass="pagination-ys" />
                <RowStyle BackColor="#E3EAEB" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#F8FAFA" />
                <SortedAscendingHeaderStyle BackColor="#246B61" />
                <SortedDescendingCellStyle BackColor="#D4DFE1" />
                <SortedDescendingHeaderStyle BackColor="#15524A" />
            </asp:GridView>

c#(I assumed as the required value is in the 1 cell of the selected row.)

string accnbr = string.Empty;
                foreach (GridViewRow gvrow in grdAccounts.Rows)
                {
                    CheckBox chk = (CheckBox)gvrow.FindControl("CheckBox1");
                    if (chk != null & chk.Checked)
                    {
                        cif += gvrow.Cells[1].Text + ',';
                    }
                }
Aruna Prabhath
  • 206
  • 2
  • 10