0

I have a Gridview that has 4 columns. All in ItemTemplate One column is a CheckBox. This Gridview is binded to a datasource. What I want to do is when the Checkbox is checked update the database with the bit value '1' and if I uncheck the Checkbox to do the same but '0'.

I already know my SQL code to update the database, but I am not sure how to run the event so that it looks at the current row the checkbox is on in the gridview.

Here is the CheckBox Column.

<asp:TemplateField  HeaderText="Pick" ItemStyle-CssClass="hrGrid" HeaderStyle-CssClass="hdrHrBase">
<ItemTemplate>
<asp:CheckBox id="cbViewCustomer" runat="server" OnCheckedChanged="ViewCustomer" onAutoPostBack="true" Checked='<%#(Eval("chosen"))%>'/>        
</ItemTemplate>
</asp:TemplateField>

Do I use EventArgs like this: The problem with this is it updates the database but all the rows. What I am trying to do is update only the current row that is in the Gridview. I am just not sure where to go from here.

protected void ViewCustomer(object sender, EventArgs e)
    {
        string SelectCustomer = "UPDATE tblcustomer Set chosen ='0'";
        NpgsqlCommand changedata = new NpgsqlCommand(SelectCustomer, con);
        con.Open();
        changedata.ExecuteNonQuery();
        con.Close();

    }

Or should I be doing something different?

3
  • Have you tried this? That is a valid solution. Although you should consider if you want to do this immediately... Usually users assume they can edit things as draft and only save when clicking a save button, make sure this is not the case and users understand checking the box will IMMEDIATELY update the object as I have found this can stress people out Commented Feb 13, 2017 at 13:34
  • As it is this updates the entire column with the same value. I just what it to update the current row. Commented Feb 13, 2017 at 14:02
  • What? You need to post the other code which does the update... You can certainly do what you need with an event handler like this Commented Feb 13, 2017 at 14:07

1 Answer 1

1

You can do this with OnRowCommand event of gridview like this:-

Associate a RowCommand event with your gridview:-

<asp:GridView ID="mygridview" runat="server" OnRowCommand="mygridview_OnRowCommand">

Next, associate a CommandName & CommandArgument to your checkbox:-

<asp:TemplateField  HeaderText="Pick" ItemStyle-CssClass="hrGrid" >
   <ItemTemplate>
     <asp:CheckBox id="cbViewCustomer" runat="server" Checked='<%#(Eval("chosen"))%>' 
         CommandName="myCheckbox" CommandArgument="<%# Container.DataItemIndex %>"/>
   </ItemTemplate>
</asp:TemplateField>

In the code behind handle the event:-

protected mygridview_OnRowCommand (object sender, GridViewCommandEventArgs e)
{
     if(e.CommandName == "myCheckbox")
     {
          int rowIndex = Convert.ToInt32(e.CommandArgument);
          GridViewRow row = mygridview.Rows[rowIndex];
          bool ischecked = (row.FindControl("cbViewCustomer") as CheckBox).Checked;
     }
}

You can also do it with checbox checked event, but if you have multiple controls then fire the gridview command event instead of individual events.

Sign up to request clarification or add additional context in comments.

4 Comments

The case returns this error: Error CS8070: Control cannot fall out of switch statement through final case label `case "cbViewCustomer":' (CS8070) (sbmanager)
@TommyMcGee - That was just a C# case statement issue which I gave for example. you can use the updated code instead.
I have been trying your suggestion but I cannot seem to get it to work. No error. just nothing happens. I have OnRowCommand set on the gridview and the CommandName set on the checkbox. But still nothing happens.
@TommyMcGee - Have you debugged the code? Is it hitting the mygridview_OnRowCommand event handler?

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.