locked
Make a checkbox clickable and updateable on grid view Dynamic Data RRS feed

  • Question

  • User311279088 posted

    How do i make a checkbox “is update” clickable on grid view and when user check or uncheck the check box then field in database should update?

    Wednesday, July 27, 2016 4:12 PM

Answers

  • User-359936451 posted

    Well your jumping a little ahead with the Gridview, but you can handle this in the Click event of the check box,

    You will need to read the Gridview row to get the info needed to update the database.

    Make certain that the check box property Enable Postback is set to True.

    This should get you started.

    You will need to add a FindControl something like this to get the check box then test for its value....

     Dim chkBox As CheckBox = DirectCast(row.FindControl("chkSelected"), CheckBox)

    Do this inside a loop that iterates over each Griddview row.

    For Each dr In GridView1.Rows

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 27, 2016 8:50 PM
  • User-2057865890 posted

    Hi Mohsinkhan93,

    You could use CheckBox.CheckedChanged Event.

    <asp:CheckBox ID="chkRow" runat="server" OnCheckedChanged="chkRow_CheckedChanged" AutoPostBack="true"/>
    
    
    protected void chkRow_CheckedChanged(object sender, EventArgs e)
    {
         CheckBox cb = (CheckBox)sender;
         bool var1 = cb.Checked;
         GridViewRow gvr = (GridViewRow)cb.Parent.Parent;
         if(var1)
         {
    //checked } else {
    //unchecked } }

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 28, 2016 6:37 AM
  • User-330204900 posted

    the In Dynamic Data you would get a checkbox automatically if the entity type for that column is of type Boolean. if you want to be able to check any row and update the underlying entity without going into edit mode for that row I believe with DD you will have to add a custom Colum template like this:

    <asp:TemplateField>
    	<ItemStyle CssClass="center" />
    	<ItemTemplate>
    		<asp:CheckBox ID="Invoice" runat="server" />
    	</ItemTemplate>
    </asp:TemplateField>
    

    this code goes in the <Columns> collection.

    you will then need to handle the checkbox changed event.

    The gridview is designed to work via data binding if you are wanting to have the data updated on checkbox change then you may be better looking at a client side framework like Angular or some such.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 28, 2016 10:45 AM

All replies

  • User-359936451 posted

    Well your jumping a little ahead with the Gridview, but you can handle this in the Click event of the check box,

    You will need to read the Gridview row to get the info needed to update the database.

    Make certain that the check box property Enable Postback is set to True.

    This should get you started.

    You will need to add a FindControl something like this to get the check box then test for its value....

     Dim chkBox As CheckBox = DirectCast(row.FindControl("chkSelected"), CheckBox)

    Do this inside a loop that iterates over each Griddview row.

    For Each dr In GridView1.Rows

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 27, 2016 8:50 PM
  • User-2057865890 posted

    Hi Mohsinkhan93,

    You could use CheckBox.CheckedChanged Event.

    <asp:CheckBox ID="chkRow" runat="server" OnCheckedChanged="chkRow_CheckedChanged" AutoPostBack="true"/>
    
    
    protected void chkRow_CheckedChanged(object sender, EventArgs e)
    {
         CheckBox cb = (CheckBox)sender;
         bool var1 = cb.Checked;
         GridViewRow gvr = (GridViewRow)cb.Parent.Parent;
         if(var1)
         {
    //checked } else {
    //unchecked } }

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 28, 2016 6:37 AM
  • User-330204900 posted

    the In Dynamic Data you would get a checkbox automatically if the entity type for that column is of type Boolean. if you want to be able to check any row and update the underlying entity without going into edit mode for that row I believe with DD you will have to add a custom Colum template like this:

    <asp:TemplateField>
    	<ItemStyle CssClass="center" />
    	<ItemTemplate>
    		<asp:CheckBox ID="Invoice" runat="server" />
    	</ItemTemplate>
    </asp:TemplateField>
    

    this code goes in the <Columns> collection.

    you will then need to handle the checkbox changed event.

    The gridview is designed to work via data binding if you are wanting to have the data updated on checkbox change then you may be better looking at a client side framework like Angular or some such.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 28, 2016 10:45 AM