带复选框的网格视图:如何在 ASP.Net 中获取选定的行

本文关键字:Net 获取 ASP 网格 复选框 视图 | 更新日期: 2023-09-27 17:57:15

选中复选框时如何获取网格视图行值。我在按钮的单击事件中使用此代码,但它不起作用.

网页代码:

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%"
        DataKeyNames="ReportId" OnRowDataBound="GridView2_OnRowDataBound" ForeColor="#333333"
        PageSize="5" Style="text-align: center">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBoxG1" runat="server" />
                </ItemTemplate>

C# 代码:

    protected void Button1_Click(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            CheckBox CheckRow = (row.Cells[0].FindControl("CheckBoxG1") as CheckBox);
            if (CheckRow.Checked)
            {
            }
        }
    }
}

带复选框的网格视图:如何在 ASP.Net 中获取选定的行

我看不到您如何绑定数据以及按钮位置在哪里。所以这是工作示例。

<asp:Button Text="text" runat="server" OnClick="Unnamed_Click" />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ReportId" Width="100%"
            ForeColor="#333333" PageSize="5" Style="text-align: center">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBoxG1" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GridView1.DataSource = new RowModel[]
            {
                new RowModel { ReportId = "1" },
                new RowModel { ReportId = "2" },
                new RowModel { ReportId = "3" }
            };
            GridView1.DataBind();
        }
    }
    protected void Unnamed_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox CheckRow = (row.Cells[0].FindControl("CheckBoxG1") as CheckBox);
                if (CheckRow.Checked)
                {
                }
            }
        }
    }
public class RowModel
{
    public string ReportId { get; set; }
}

在我的示例代码中,我在绑定网格视图时考虑了手动数据,因为您没有指定如何通过数据库绑定网格视图,但它应该在两种方法中都有效。我的网页代码

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Get Checkbox Selected Row Values from Gridview in Asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:GridView ID="gvDetails" DataKeyNames="UserId" AutoGenerateColumns="false" CellPadding="5" runat="server">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:CheckBox ID="chkSelect" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField HeaderText="UserId" DataField="UserId" />
                    <asp:BoundField HeaderText="UserName" DataField="UserName" />
                    <asp:BoundField HeaderText="Education" DataField="Education" />
                    <asp:BoundField HeaderText="Location" DataField="Location" />
                </Columns>
                <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
            </asp:GridView>
            <asp:Button ID="btnProcess" Text="Get Selected Records" runat="server" Font-Bold="true" onclick="btnProcess_Click" />
            <br />
            <asp:Label ID="lblmsg" runat="server" />
        </div>
    </form>
</body>
</html>

代码隐藏:这仅用于将 GridView 与正确的数据绑定

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindGridviewData();
    }
}
protected void BindGridviewData()
{
    DataTable dt = new DataTable();
    dt.Columns.Add("UserId", typeof(Int32));
    dt.Columns.Add("UserName", typeof(string));
    dt.Columns.Add("Education", typeof(string));
    dt.Columns.Add("Location", typeof(string));
    DataRow dtrow = dt.NewRow();        //Create New Row
    dtrow["UserId"] = 1;                //Bind Data to Columns
    dtrow["UserName"] = "SureshDasari";
    dtrow["Education"] = "B.Tech";
    dtrow["Location"] = "Chennai";
    dt.Rows.Add(dtrow);
    dtrow = dt.NewRow();               //Create New Row
    dtrow["UserId"] = 2;               //Bind Data to Columns
    dtrow["UserName"] = "MadhavSai";
    dtrow["Education"] = "MBA";
    dtrow["Location"] = "Nagpur";
    dt.Rows.Add(dtrow);
    dtrow = dt.NewRow();              //Create New Row
    dtrow["UserId"] = 3;              //Bind Data to Columns
    dtrow["UserName"] = "MaheshDasari";
    dtrow["Education"] = "B.Tech";
    dtrow["Location"] = "Nuzividu";
    dt.Rows.Add(dtrow);
    
    gvDetails.DataSource = dt;
    gvDetails.DataBind();
}

Button_Click事件代码

protected void btnProcess_Click(object sender, EventArgs e)
{
    string str = string.Empty;
    string strname = string.Empty;
    string edu = string.Empty;
    string location = string.Empty;
    foreach (GridViewRow gvrow in gvDetails.Rows)
    {
        CheckBox chk = (CheckBox)gvrow.FindControl("chkSelect");
        if (chk != null & chk.Checked)
        {
            //To Fetch the row index
            //str += gvDetails.SelectedIndex.ToString();
            
            //To Fetch the value of Selected Row.
            str += gvDetails.DataKeys[gvrow.RowIndex].Value.ToString() + ',';
            strname += gvrow.Cells[2].Text + ',';
            edu += gvrow.Cells[3].Text + ',';
            location += gvrow.Cells[4].Text + ',';
        }
    }
    str = str.Trim(",".ToCharArray());
    strname = strname.Trim(",".ToCharArray());
    lblmsg.Text = "Selected UserIds: <b>" + str + "</b><br/>" + "Selected UserNames: <b>" + strname + "</b><br>" + " Education: <b>" + edu + "</b><br>" + " Location: <b>" + location + "</b><br>";
}
protected void Button1_Click(object sender, EventArgs e)
{
        int count= 0;
        foreach (GridViewRow gvindex in GridView1.Rows)
        {
            CheckBox chck = gvrow.FindControl("CheckBoxG1") as CheckBox;
            if (chck.Checked)
            {
                GridView1.EditIndex = count;
                DataBind(); //Your Databind Function
            }
            count++;
        }