教你GridView如何获取选中行的值?

本文介绍如何使用ASP.NET中的GridView控件实现数据的分页显示与批量更新功能。通过示例代码展示了如何设置GridView的各项属性以实现良好的用户体验,并在后端逻辑中通过遍历与条件判断完成数据的批量修改。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

页面代码Default.aspx GridView 部分

<asp:GridView ID="gvMain" runat="server" AllowPaging="True" AutoGenerateColumns="False"
            DataSourceID="ObjectDataSource2" Height="144px" CellPadding="0" ForeColor="#333333"
            GridLines="None" OnRowDataBound="gvMain_RowDataBound"
            HorizontalAlign="Center">
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#EFF3FB" />
            <Columns>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <input id="chbCheckAll" οnclick="GetAllCheckBox(this)" type="checkbox" />全选
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="chbCheck" runat="server" />
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Center" Width="100px" />
                </asp:TemplateField>
                <asp:BoundField DataField="Id" HeaderText="编号" SortExpression="Id">
                    <ItemStyle HorizontalAlign="Center" Width="100px" />
                </asp:BoundField>
                <asp:BoundField DataField="Title" HeaderText="标题" SortExpression="Title">
                    <ItemStyle Width="300px" />
                </asp:BoundField>
                <asp:BoundField DataField="Author" HeaderText="作者" SortExpression="Author">
                    <ItemStyle Width="300px" />
                </asp:BoundField>
                <asp:TemplateField>
                    <HeaderTemplate>
                        类别
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("Category.Name") %>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle Width="150px" />
                </asp:TemplateField>
                <asp:CommandField ShowEditButton="True">
                    <ItemStyle Width="100px" />
                </asp:CommandField>
            </Columns>
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>

 

<asp:DropDownList ID="ddlCategory" runat="server" DataTextField="Name" DataValueField="Id"
            Height="20px" Width="144px" DataSourceID="ObjectDataSource1">
        </asp:DropDownList>

<asp:Button ID="btnModify" runat="server" OnClick="btnModify_Click" Text=" 修 改" />

 

 

后置代码Default.aspx.cs

 protected void btnModify_Click(object sender, EventArgs e)
    {

        用foreach循环

        if (gvMain.Rows.Count > 0)      
        {
            foreach (GridViewRow rows in gvMain.Rows)
            {

                CheckBox chbSelected = (CheckBox)rows.Cells[0].FindControl("chbCheck");
                if (chbSelected.Checked)
                {

                    //调用修改的方法
                    BookManager.UpdateBookCategoryById(Convert.ToInt32(ddlCategory.SelectedItem.Value), Convert.ToInt32(rows.Cells[1].Text));     //ddlCategory.SelectedItem.Value,是获取DropDownList的值

                                           //rows.Cells[1].Text,获取的是GridView中编号那一列的值

               }
            }

 

 

              用for循环

              //for(int i=0;i<this.gvMain.Rows.Count;i++)

              //{

              //      CheckBox chbSelected = (this.gvMain.Rows[i].Cells[0].FindControl("chbCheck")) as CheckBox;

              //      if (chbSelected.Checked == true)
              //          {

              //                    BookManager.UpdateBookCategoryById(Convert.ToInt32(ddlCategory.SelectedItem.Value), Convert.ToInt32(this.gvMain.Rows[i].Cells[0].Text.ToString()));   

              //           }

              //}

 


            Response.Redirect("Default.aspx");
        }

 

 

业务逻辑层BookManager

public static int UpdateBookCategoryById(int categoryId, int Id)

{

     return BookDAL.UpdateBookCategoryById(categoryId, Id);

}

 

 

数据访问层BookDAL

public static int UpdateBookCategoryById(int categoryId, int Id)
        {

            string connString="server=ZHANGYUHONG;database=BookShop;uid=sa;pwd=sa";
            string sql = string.Format("update Books set CategoryId={0} where Id={1}", categoryId, Id);
            SqlConnection connection =new SqlConnection(connString);

            SqlCommand command=new SqlCommand(sql,connection);

            connection.Open();

            int count = command.ExecuteCommand(sql);

            connection.Close();
            return count;
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值