Datagrid selected items with checkbox

Discussion in 'ASP .Net Datagrid Control' started by Bnob, Aug 11, 2004.

  1. Bnob

    Bnob Guest

    I read some articles about the way to find the rows having his checkbox
    checked.

    Here is the code

    Private Sub btnModifier_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles btnModifier.Click
    Dim dgItem As DataGridItem
    Dim chkSelected As CheckBox

    For Each dgItem In Me.DataGrid1.Items
    chkSelected = dgItem.FindControl("chkSelect")
    If chkSelected.Checked Then
    '--- my code
    End If

    Next

    end sub

    In my asp page I have this for my datagrid:
    <Columns>
    <asp:TemplateColumn>
    <ItemStyle HorizontalAlign="Center"></ItemStyle>
    <ItemTemplate>
    <asp:CheckBox id="chkSelect" Runat="server"></asp:CheckBox>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>

    My problem is when I click on my btnModifier button, my code never
    execute. The if statment always return false.

    Any idea?

    --
    Ceci est une signature automatique de MesNews.
    Site : http://mesnews.no-ip.com
     
    Bnob, Aug 11, 2004
    #1
    1. Advertising

  2. Hi,

    Here's a sample that seems to do what you want. Perhaps you could try it and
    let us know?

    Private Sub Page_Load _
    (ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles MyBase.Load
    If Not IsPostBack Then
    DataGrid1.DataSource = CreateDataSource()
    DataGrid1.DataBind()
    End If
    End Sub

    Private Sub btnModifier_Click _
    (ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles btnModifier.Click
    Dim dgItem As DataGridItem
    Dim chkSelected As CheckBox
    Label2.Text = ""
    For Each dgItem In Me.DataGrid1.Items
    chkSelected = dgItem.FindControl("chkSelect")
    If chkSelected.Checked Then
    Label2.Text = Label2.Text & _
    chkSelected.UniqueID & "<br>"
    End If
    Next
    End Sub

    Function CreateDataSource() As DataTable
    Dim dt As New DataTable
    Dim dr As DataRow
    dt.Columns.Add(New DataColumn _
    ("IntegerValue", GetType(Int32)))
    dt.Columns.Add(New DataColumn _
    ("StringValue", GetType(String)))
    dt.Columns.Add(New DataColumn _
    ("CurrencyValue", GetType(Double)))
    dt.Columns.Add _
    (New DataColumn("Boolean", GetType(Boolean)))
    Dim i As Integer
    For i = 0 To 8
    dr = dt.NewRow()
    dr(0) = i
    dr(1) = "Item " + i.ToString()
    dr(2) = 1.23 * (i + 1)
    dr(3) = (i = 4)
    dt.Rows.Add(dr)
    Next i
    Return dt
    End Function 'CreateDataSource



    <asp:DataGrid id="DataGrid1" runat="server">
    <Columns>
    <asp:TemplateColumn HeaderText="Boolean Value">
    <ItemTemplate>
    <asp:CheckBox id=chkSelect runat="server" Checked='<%#
    DataBinder.Eval(Container, "DataItem.Boolean") %>' >
    </asp:CheckBox>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Boolean Value">
    <ItemTemplate>
    <asp:Label id="Label1" runat="server">
    <%# IIF(DataBinder.Eval(Container, "DataItem.Boolean"),"Yes","No")
    %>
    </asp:Label>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>
    <P>
    <asp:Label id="Label2" runat="server"></asp:Label></P>
    <P>
    <asp:Button id="btnModifier" runat="server"
    Text="Button"></asp:Button></P>

    Does this help?

    Ken
    Microsoft MVP [ASP.NET]
    Toronto



    "Bnob" <> wrote in message
    news:...
    >I read some articles about the way to find the rows having his checkbox
    >checked.
    >
    > Here is the code
    >
    > Private Sub btnModifier_Click(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles btnModifier.Click
    > Dim dgItem As DataGridItem
    > Dim chkSelected As CheckBox
    >
    > For Each dgItem In Me.DataGrid1.Items
    > chkSelected = dgItem.FindControl("chkSelect")
    > If chkSelected.Checked Then
    > '--- my code
    > End If
    >
    > Next
    >
    > end sub
    >
    > In my asp page I have this for my datagrid:
    > <Columns>
    > <asp:TemplateColumn>
    > <ItemStyle HorizontalAlign="Center"></ItemStyle>
    > <ItemTemplate>
    > <asp:CheckBox id="chkSelect" Runat="server"></asp:CheckBox>
    > </ItemTemplate>
    > </asp:TemplateColumn>
    > </Columns>
    >
    > My problem is when I click on my btnModifier button, my code never
    > execute. The if statment always return false.
    >
    > Any idea?
    >
    > --
    > Ceci est une signature automatique de MesNews.
    > Site : http://mesnews.no-ip.com
    >
     
    Ken Cox [Microsoft MVP], Aug 11, 2004
    #2
    1. Advertising

  3. Bnob

    Bnob Guest

    Ken Cox [Microsoft MVP] avait écrit le 11.08.2004 :
    > chkSelected.Checked


    Sorry
    But the chkSelected.Checked statment is always FALSE!

    --
    Ceci est une signature automatique de MesNews.
    Site : http://mesnews.no-ip.com
     
    Bnob, Aug 11, 2004
    #3
  4. Sorry, I guess I can't help you then because the code I provided works on my
    system.


    "Bnob" <> wrote in message
    news:...
    > Ken Cox [Microsoft MVP] avait écrit le 11.08.2004 :
    >> chkSelected.Checked

    >
    > Sorry
    > But the chkSelected.Checked statment is always FALSE!
    >
    > --
    > Ceci est une signature automatique de MesNews.
    > Site : http://mesnews.no-ip.com
    >
     
    Ken Cox [Microsoft MVP], Aug 11, 2004
    #4
  5. Bnob

    Bnob Guest

    Ken Cox [Microsoft MVP] vient de nous annoncer :
    > Sorry, I guess I can't help you then because the code I provided works on my
    > system.
    >
    >
    > "Bnob" <> wrote in message
    > news:...
    >> Ken Cox [Microsoft MVP] avait écrit le 11.08.2004 :
    >>> chkSelected.Checked

    >>
    >> Sorry
    >> But the chkSelected.Checked statment is always FALSE!
    >>
    >> -- Ceci est une signature automatique de MesNews.
    >> Site : http://mesnews.no-ip.com
    >>


    All is ok

    Thanks

    --
    Ceci est une signature automatique de MesNews.
    Site : http://mesnews.no-ip.com
     
    Bnob, Aug 12, 2004
    #5
  6. What was it?


    >
    > All is ok
    >
    > Thanks
    >
     
    Ken Cox [Microsoft MVP], Aug 13, 2004
    #6
    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. Joel Reinford
    Replies:
    1
    Views:
    646
    Curt_C [MVP]
    Jan 13, 2004
  2. =?Utf-8?B?ZWNvZGVy?=

    Copy checkbox selected datagrid items into new dataset

    =?Utf-8?B?ZWNvZGVy?=, Oct 10, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    2,321
    =?Utf-8?B?ZWNvZGVy?=
    Oct 14, 2005
  3. Bart Van Hemelen
    Replies:
    2
    Views:
    4,692
    Bart Van Hemelen
    Sep 4, 2006
  4. Iain
    Replies:
    3
    Views:
    950
  5. mldardy
    Replies:
    0
    Views:
    980
    mldardy
    Sep 28, 2010
Loading...

Share This Page