0

here is my GridView code, i dont know how to do it. how can i update a row if the checkbos is selected. if the checkbos is selected, the "Status" of the selected row will change to "Approved" when the "btnGetSelected" is clicked. thanks!

<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 Answer 1

1

I assume TransactionID is the primary key for the record. I further assumed the datatype for transactionID is an int. If not just change it to whatever it is. Set the property in the HTML of the GridView as:

DataKeyNames="TransactionID" 

This will automatically assign the primary key to each record.

In the code behind:

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");
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.