Howto bind CheckBox to the datagrid/ Then update the database field when the checkbox is clicked.

Discussion in 'ASP .Net Datagrid Control' started by Joey Pang, Jun 10, 2005.

  1. Joey Pang

    Joey Pang Guest

    I am trying to update the database field when the checkbox is clicked.

    I am trying to modified the following solution but.. got stuck on the
    ItemCommand Events where
    Dim prodchknew As CheckBox = e.Item.FindControl("chk")
    cannot find the control.

    I am trying to put the ItemCommand Code into ItemBound Event but seems to be
    stuck on the above line ..
    Anyone got ideas?

    Thanks a bunch,
    Joey


    Solution:
    webform1.aspx
    <asp:datagrid id="DataGrid1"
    style="Z-INDEX: 101; LEFT: 9px; POSITION: absolute; TOP: 15px"
    runat="server" AutoGenerateColumns="False"
    DataKeyField="Productid">
    <Columns>
    <asp:TemplateColumn HeaderText="ProductName">
    <ItemTemplate>
    <asp:CheckBox id=chk runat="server"
    Text =<%# DataBinder.Eval(Container.DataItem, "ProductName") %>
    checked='<%# DataBinder.Eval(Container.DataItem, "Discontinued") %>'>
    </asp:CheckBox>

    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:ButtonColumn ButtonType="PushButton"
    CommandName="Update" Text="Update"></asp:ButtonColumn>
    </Columns>
    </asp:datagrid>



    webform1.asp.vb
    Step 1:Simple Binding with the DataGrid. The Products Table has the Field
    Discontinued (DataType:bit) Based on the value 0/1 of the Discontinued field
    uncheck/check the Checkbox.
    Dim myconnection As SqlConnection
    Dim myda As SqlDataAdapter
    Dim mycmd As SqlCommand
    Dim ds As DataSet
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    If Not Page.IsPostBack Then
    bindData()
    End If
    End Sub
    Sub bindData()
    myconnection = New
    SqlConnection("Server=localhost;uid=sa;password=;database=northwind;")
    myda = New SqlDataAdapter("Select * from Products where
    productid<4", myconnection)
    ds = New DataSet()
    myda.Fill(ds, "AllTables")
    DataGrid1.DataSource = ds
    DataGrid1.DataBind()
    End Sub



    Step 2: If the user check's/uncheck's the original value of the Checkbox and
    clicks the button the new checkbox value should be stored in the Database
    table.
    Private Sub DataGrid1_ItemCommand(ByVal source As Object, _
    ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
    DataGrid1.ItemCommand
    Dim prodchknew As CheckBox = e.Item.FindControl("chk")
    Dim updval As String
    If prodchknew.Checked Then
    updval = "1"
    Else
    updval = "0"
    End If

    If e.CommandName = "Update" Then
    myconnection = New
    SqlConnection("Server=localhost;uid=sa;password=;database=northwind;")
    Dim strsql As String = "Update Products set Discontinued='" &
    updval & "'" & _
    " where Productid=" & DataGrid1.DataKeys(CInt(e.Item.ItemIndex))
    ' Response.Write(strsql)
    mycmd = New SqlCommand(strsql, myconnection)
    myconnection.Open()
    mycmd.ExecuteNonQuery()
    bindData()
    End If
    End Sub




    Sushila S. Patel, Microsoft .NET MVP
     
    Joey Pang, Jun 10, 2005
    #1
    1. Advertising

  2. Joey Pang

    Kim Quigley Guest

    Can you post your ItemDataBound method?

    Whenever I have to put code in that method, I always forget to make sure
    that the current item isn't a header or footer:
    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
    ListItemType.AlternatingItem Then

    End If


    "Joey Pang" <> wrote in message
    news:%23ERL$...
    >I am trying to update the database field when the checkbox is clicked.
    >
    > I am trying to modified the following solution but.. got stuck on the
    > ItemCommand Events where
    > Dim prodchknew As CheckBox = e.Item.FindControl("chk")
    > cannot find the control.
    >
    > I am trying to put the ItemCommand Code into ItemBound Event but seems to
    > be stuck on the above line ..
    > Anyone got ideas?
    >
    > Thanks a bunch,
    > Joey
    >
    >
    > Solution:
    > webform1.aspx
    > <asp:datagrid id="DataGrid1"
    > style="Z-INDEX: 101; LEFT: 9px; POSITION: absolute; TOP: 15px"
    > runat="server" AutoGenerateColumns="False"
    > DataKeyField="Productid">
    > <Columns>
    > <asp:TemplateColumn HeaderText="ProductName">
    > <ItemTemplate>
    > <asp:CheckBox id=chk runat="server"
    > Text =<%# DataBinder.Eval(Container.DataItem, "ProductName") %>
    > checked='<%# DataBinder.Eval(Container.DataItem, "Discontinued") %>'>
    > </asp:CheckBox>
    >
    > </ItemTemplate>
    > </asp:TemplateColumn>
    > <asp:ButtonColumn ButtonType="PushButton"
    > CommandName="Update" Text="Update"></asp:ButtonColumn>
    > </Columns>
    > </asp:datagrid>
    >
    >
    >
    > webform1.asp.vb
    > Step 1:Simple Binding with the DataGrid. The Products Table has the Field
    > Discontinued (DataType:bit) Based on the value 0/1 of the Discontinued
    > field uncheck/check the Checkbox.
    > Dim myconnection As SqlConnection
    > Dim myda As SqlDataAdapter
    > Dim mycmd As SqlCommand
    > Dim ds As DataSet
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Load
    > If Not Page.IsPostBack Then
    > bindData()
    > End If
    > End Sub
    > Sub bindData()
    > myconnection = New
    > SqlConnection("Server=localhost;uid=sa;password=;database=northwind;")
    > myda = New SqlDataAdapter("Select * from Products where
    > productid<4", myconnection)
    > ds = New DataSet()
    > myda.Fill(ds, "AllTables")
    > DataGrid1.DataSource = ds
    > DataGrid1.DataBind()
    > End Sub
    >
    >
    >
    > Step 2: If the user check's/uncheck's the original value of the Checkbox
    > and clicks the button the new checkbox value should be stored in the
    > Database table.
    > Private Sub DataGrid1_ItemCommand(ByVal source As Object, _
    > ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
    > DataGrid1.ItemCommand
    > Dim prodchknew As CheckBox = e.Item.FindControl("chk")
    > Dim updval As String
    > If prodchknew.Checked Then
    > updval = "1"
    > Else
    > updval = "0"
    > End If
    >
    > If e.CommandName = "Update" Then
    > myconnection = New
    > SqlConnection("Server=localhost;uid=sa;password=;database=northwind;")
    > Dim strsql As String = "Update Products set Discontinued='" &
    > updval & "'" & _
    > " where Productid=" & DataGrid1.DataKeys(CInt(e.Item.ItemIndex))
    > ' Response.Write(strsql)
    > mycmd = New SqlCommand(strsql, myconnection)
    > myconnection.Open()
    > mycmd.ExecuteNonQuery()
    > bindData()
    > End If
    > End Sub
    >
    >
    >
    >
    > Sushila S. Patel, Microsoft .NET MVP
    >
     
    Kim Quigley, Jun 10, 2005
    #2
    1. Advertising

  3. Joey Pang

    Kim Quigley Guest

    This page has a good example for doing something when a checkbox in a
    datagrid is changed:

    http://www.geekswithblogs.net/sgreenberger/archive/2004/11/12/14871.aspx

    This is basically what it says:

    In the aspx page, modify the checkbox tag by adding the OnCheckedChanged
    attribute, and setting it to the name of the sub that you just created
    above. You'll also need autopostback set to true.
    <ItemTemplate>
    <asp:CheckBox id="CheckBox1" runat="server" AutoPostBack="True"
    OnCheckedChanged="Check_Clicked">asp:CheckBox>
    >



    Now when the checkbox is clicked, the Check_Clicked event will be
    fired. Add the following code to the Check_Clicked procedure:

    Protected Sub Check_Clicked(ByVal sender As Object, ByVal e As EventArgs)
    Dim ck1 As CheckBox = CType(sender, CheckBox)
    Dim dgItem As DataGridItem = CType(ck1.NamingContainer,
    DataGridItem)
    'now we've got what we need!
    Label1.Text = "You selected row " & dgItem.Cells(0).Text
    End Sub
     
    Kim Quigley, Jun 10, 2005
    #3
  4. Joey Pang

    Joey Pang Guest

    Thank you so much Kim... :D Let met try it out :D
    "Kim Quigley" <> wrote in message
    news:...
    > This page has a good example for doing something when a checkbox in a
    > datagrid is changed:
    >
    > http://www.geekswithblogs.net/sgreenberger/archive/2004/11/12/14871.aspx
    >
    > This is basically what it says:
    >
    > In the aspx page, modify the checkbox tag by adding the OnCheckedChanged
    > attribute, and setting it to the name of the sub that you just created
    > above. You'll also need autopostback set to true.
    > <ItemTemplate>
    > <asp:CheckBox id="CheckBox1" runat="server" AutoPostBack="True"
    > OnCheckedChanged="Check_Clicked">asp:CheckBox>
    >>

    >
    >
    > Now when the checkbox is clicked, the Check_Clicked event will be
    > fired. Add the following code to the Check_Clicked procedure:
    >
    > Protected Sub Check_Clicked(ByVal sender As Object, ByVal e As EventArgs)
    > Dim ck1 As CheckBox = CType(sender, CheckBox)
    > Dim dgItem As DataGridItem = CType(ck1.NamingContainer,
    > DataGridItem)
    > 'now we've got what we need!
    > Label1.Text = "You selected row " & dgItem.Cells(0).Text
    > End Sub
    >
    >
    >
     
    Joey Pang, Jun 13, 2005
    #4
  5. Joey Pang

    Joey Pang Guest

    It worked!! Thank you Thank you Kim :D :D :D You save my day...

    "Kim Quigley" <> wrote in message
    news:...
    > This page has a good example for doing something when a checkbox in a
    > datagrid is changed:
    >
    > http://www.geekswithblogs.net/sgreenberger/archive/2004/11/12/14871.aspx
    >
    > This is basically what it says:
    >
    > In the aspx page, modify the checkbox tag by adding the OnCheckedChanged
    > attribute, and setting it to the name of the sub that you just created
    > above. You'll also need autopostback set to true.
    > <ItemTemplate>
    > <asp:CheckBox id="CheckBox1" runat="server" AutoPostBack="True"
    > OnCheckedChanged="Check_Clicked">asp:CheckBox>
    >>

    >
    >
    > Now when the checkbox is clicked, the Check_Clicked event will be
    > fired. Add the following code to the Check_Clicked procedure:
    >
    > Protected Sub Check_Clicked(ByVal sender As Object, ByVal e As EventArgs)
    > Dim ck1 As CheckBox = CType(sender, CheckBox)
    > Dim dgItem As DataGridItem = CType(ck1.NamingContainer,
    > DataGridItem)
    > 'now we've got what we need!
    > Label1.Text = "You selected row " & dgItem.Cells(0).Text
    > End Sub
    >
    >
    >
     
    Joey Pang, Jun 13, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nu2ASP.NET
    Replies:
    2
    Views:
    656
    Nu2ASP.NET
    Nov 18, 2005
  2. =?Utf-8?B?RG90TmV0RGV2?=

    DataGrid and embeded Checkbox..How to find if checkbox clicked

    =?Utf-8?B?RG90TmV0RGV2?=, Oct 6, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    827
    David Wier
    Oct 6, 2006
  3. Mufasa
    Replies:
    7
    Views:
    415
    Alexey Smirnov
    Sep 4, 2007
  4. .NETn00b
    Replies:
    0
    Views:
    190
    .NETn00b
    Nov 19, 2005
  5. Replies:
    0
    Views:
    290
Loading...

Share This Page