Click here to Skip to main content
14,699,340 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have used different types of code to select data from gridview by using chechkbox to update the selected data on gridview by using c# code.

What I have tried:

my gridview code.
<div class="widget-content">
    <asp:GridView ID="GridNewPins" AutoGenerateColumns="false" GridLines="None" runat="server" CssClass="table table-striped table-bordered">
            <asp:TemplateField ItemStyle-Width="40px">
                    <asp:CheckBox ID="chkboxSelectAll" runat="server" onclick="CheckAllEmp(this);" />
                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                    <asp:CheckBox ID="chkSelect" runat="server" />

    <asp:HiddenField ID="hdpinslno" runat="server" Value='<%# Eval("Pinslno") %>' />
    this is not working and i don't understand how can i use this code.
            <asp:BoundField DataField="PinSlNo" HeaderText="PinSlNo" />
            <asp:BoundField DataField="PlanName" HeaderText="PlanName" />
            <asp:BoundField DataField="Pin" HeaderText="Pin" />
            <asp:BoundField DataField="PinId" HeaderText="PinId" />
            <asp:BoundField DataField="Status" HeaderText="Status" />
            <asp:BoundField DataField="GenerateDate" HeaderText="GenerateDate" />
            <asp:BoundField DataField="PinValue" HeaderText="PinValue" />
<!-- /widget-content -->
<div class="form-actions">
    <asp:Button runat="server" ID="btnActive" class="btn btn-success" Text="DeActive" OnClick="btnActive_Click" /></div>

my c# code..
protected void btnActive_Click(object sender, EventArgs e)
    string msg = "Reg no. of ";
        foreach (GridViewRow row in this.GridNewPins.Rows)
            CheckBox chkSelect = (CheckBox)row.FindControl("chkSelect");
            if (chkSelect != null && chkSelect.Checked)
                // string obj = ((HiddenField)this.GridNewPins.Rows[row.RowIndex].FindControl("hdpinslno")).Value.ToString().Trim();
                // I have tried this code but it's working.

                string obj = ;    // how can call selected line here for update data
                DbCommand cmd = DataAccess.CreateCommandText();
                cmd.CommandText="update MemberVsPinTransfer set CancelPin = 1 where PinSlNo = @pinno";
                cmd.Parameters.Add(DataAccess.CreateParameter(cmd, "pinno", DbType.String, obj));
                int i = DataAccess.ExecuteNonQuery(cmd);
                if (i > 0)
                    msg += obj + ", ";
        msg += "Deactivated is successful";
        msg = "there is something problem try again";
Updated 3-Jun-20 1:18am

1 solution

Try using the grid's data keys:
<asp:GridView ID="GridNewPins" AutoGenerateColumns="false" DataKeyNames="Pinslno" ...
foreach (GridViewRow row in GridNewPins.Rows)
    CheckBox chkSelect = (CheckBox)row.FindControl("chkSelect");
    if (chkSelect != null && chkSelect.Checked)
        object pinno = GridNewPins.DataKeys[row.RowIndex]["Pinslno"];
GridView.DataKeyNames Property (System.Web.UI.WebControls) | Microsoft Docs[^]
GridView.DataKeys Property (System.Web.UI.WebControls) | Microsoft Docs[^]
DataKey Class (System.Web.UI.WebControls) | Microsoft Docs[^]
Maciej Los 3-Jun-20 6:39am
Member 14743579 3-Jun-20 7:19am
not working
Richard Deeming 3-Jun-20 7:23am
Wow, what a helpful comment - NOT!

How about providing some actual details of the problem? Remember, we can't see your screen, access your computer, or read your mind.
Member 14743579 3-Jun-20 7:32am
same problem arise in if condition chkselect.checked show false when i debug the code.
Richard Deeming 3-Jun-20 7:36am
That's the sort of information you should have included in your question. The only indication of a problem in your question was that the hidden field wasn't working.

If the checkbox is never selected, that would suggest you're re-binding the grid every time the page loads.
protected void Page_Load(object sender, EventArgs e)
That will reset the state of all of the checkboxes. Instead, you should check the IsPostBack property and only bind the grid on the initial load:
protected void Page_Load(object sender, EventArgs e)
    if (!Page.IsPostBack)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900