使用gridview ASP.NET中的复选框更新行

时间:2016-07-01 02:36:05

标签: c# sql asp.net gridview

这是我的GridView代码,我不知道该怎么做。如果选中了checkbos,我该如何更新一行。如果选中了checkbos,当单击“btnGetSelected”时,所选行的“Status”将变为“Approved”。谢谢!

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CellPadding="4" AllowPaging="true" PageIndex="2" OnPageIndexChanging="GridView1_PageIndexChanging" HeaderStyle-BackColor ="CornflowerBlue" BorderWidth="5" BorderColor="CornflowerBlue" Width="100%" CssClass="table-hover" >
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="true" OnCheckedChanged="chkSelect_CheckedChanged" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Transaction Number" HeaderStyle-ForeColor="White">
                <ItemTemplate>
                    <asp:Label ID ="lblmosID" runat="server" Text='<%#Bind ("TransactionID") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle Width="30px" Font-Size="15px" Font-Names="Calibri" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Date" HeaderStyle-ForeColor="White">
                <ItemTemplate>
                    <asp:Label ID ="lblDate" runat="server" Text='<%#Bind ("DateFiled") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle Width="130px" Font-Names="Calibri" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Name" HeaderStyle-ForeColor="White">
                <ItemTemplate>
                    <asp:Label ID ="lblName" runat="server" Text='<%#Bind ("ReqName") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle Font-Names="Calibri" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Company" HeaderStyle-ForeColor="White">
                <ItemTemplate>
                    <asp:Label ID ="lblComp" runat="server" Text='<%#Bind ("ReqCompany") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle Font-Names="Calibri" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Branch" HeaderStyle-ForeColor="White">
                <ItemTemplate>
                    <asp:Label ID ="lblBranch" runat="server" Text='<%#Bind ("ReqBranch") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle Font-Names ="Calibri" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Business Unit" HeaderStyle-ForeColor="White">
                <ItemTemplate>
                    <asp:Label ID ="lblBU" runat="server" Text='<%#Bind ("ReqBU") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle Font-Names="Calibri" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Department" HeaderStyle-ForeColor="White">
                <ItemTemplate>
                    <asp:Label ID ="lblDept" runat="server" Text='<%#Bind ("ReqDept") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle Font-Names="Calibri" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Section" HeaderStyle-ForeColor="White">
                <ItemTemplate>
                    <asp:Label ID ="lblsection" runat="server" Text='<%#Bind ("ReqSection") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle Font-Names="Calibri" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Status" HeaderStyle-ForeColor="White">
                <ItemTemplate>
                    <asp:Label ID ="lblStatus" runat="server" Text='<%#Bind ("TransStatus") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle Font-Names="Calibri" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="">
                <ItemTemplate>
                    <asp:LinkButton ID ="lnkEdit" runat="server" Text="View" PostBackUrl='<%# "Details.aspx?Id=" + Eval("TransactionID") %>'></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <HeaderStyle BackColor="CornflowerBlue" />
    </asp:GridView>
     <br />
     <asp:Button ID="btnGetSelected" runat="server" Text="Approve selected" OnClick="btnGetSelected_Click" CssClass="btn btn-primary" Width="10%" />
     <br />

1 个答案:

答案 0 :(得分:1)

我假设TransactionID是记录的主键。我进一步假设transactionID的数据类型是一个int。如果不是只是改变它是什么。 将GridView的HTML中的属性设置为:

DataKeyNames="TransactionID" 

这会自动为每条记录分配主键。

在背后的代码中:

private void UpdateRecord(int transactionID, string status)
{
    //Put your update code here
}

protected void btnGetSelected_Click(object sender, EventArgs e)
{
    CheckBox chkSelect;
    int transactionID; //Or use whatever datatype this should be

    foreach (GridViewRow gridViewRow in GridView1.Rows)
    {
        chkSelect = (CheckBox)gridViewRow.FindControl("chkSelect");
        transactionID = (int)GridView1.DataKeys[gridViewRow.RowIndex].Value;

        if (chkSelect.Checked)
        {
            UpdateRecord(transactionID, "Approved");
        }
    }
}