Click here to Skip to main content
15,885,537 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.
ASP.NET
<div class="widget-content">
    <asp:GridView ID="GridNewPins" AutoGenerateColumns="false" GridLines="None" runat="server" CssClass="table table-striped table-bordered">
        <Columns>
            <asp:TemplateField ItemStyle-Width="40px">
                <HeaderTemplate>
                    <asp:CheckBox ID="chkboxSelectAll" runat="server" onclick="CheckAllEmp(this);" />
                </HeaderTemplate>
                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                <ItemTemplate>
                    <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.
-->
                                       
                </ItemTemplate>
            </asp:TemplateField>
            <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" />
        </Columns>
    </asp:GridView>
</div>
<!-- /widget-content -->
<div class="form-actions">
    <asp:Button runat="server" ID="btnActive" class="btn btn-success" Text="DeActive" OnClick="btnActive_Click" /></div>
</div>

my c# code..
C#
protected void btnActive_Click(object sender, EventArgs e)
{
    string msg = "Reg no. of ";
    try
    {
        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";
    }
    catch
    {
        msg = "there is something problem try again";
        bindnewpins();
    }
    bindnewpins();
    Message.Show(msg);
}
Posted
Updated 3-Jun-20 0:18am
v2

1 solution

Try using the grid's data keys:
ASP.NET
<asp:GridView ID="GridNewPins" AutoGenerateColumns="false" DataKeyNames="Pinslno" ...
C#
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[^]
 
Share this answer
 
Comments
Maciej Los 3-Jun-20 6:39am    
5ed!
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)
{
    BindTheGrid();
}
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)
    {
        BindTheGrid();
    }
}

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