asp.net 如何从网格视图中获取复选框的选中值?c#

xtfmy6hx  于 2022-11-19  发布在  .NET
关注(0)|答案(3)|浏览(158)

我想插入基于复选框点击的特定行值。设计和源如下所示。我必须从设计中选择一些项目。如果我点击批准,我必须将那些复选框选中的行值存储到数据库中。请帮助我找到合适的解决方案。
设计:

ASPX:

C#:
下面给出的代码是获取整个gridview值并存储到数据库中。我如何根据上述要求修改此代码?

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);
        }
    }
j13ufse2

j13ufse21#

你需要遍历你的gridview,并在当前行的单元格中通过Id找到复选框。

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
        }
    }

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

gk7wooem

gk7wooem2#

下面给出的代码运行良好。

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);
                }
        }
    }
5t7ly7z5

5t7ly7z53#

这是我的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#(假设所需值位于所选行的1单元格中。)

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 + ',';
                    }
                }

相关问题