Solved

Help with using GridView with checkboxes using XML data souce

Posted on 2011-09-15
4
293 Views
Last Modified: 2012-05-12
Hello,

I included a checkbox in my GridView control to select rows with the code below. However, when I execute the code in Part A, after I select a checkbox, the chk.Checked = True  command doesn’t execute and I end up with blank values for countries.  How do I set the datasource to the Gridview in the server side. I was able to do this when using an ACCESS databse but can’t figure out how to use the xml file as a datasource. The working code with the ACCESS database is in Part C:

Part A: Code to select values in Gridwiew in .aspxvb file

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
        Dim countries As String = ""
        For Each row As GridViewRow In GridView1.Rows
            Dim chk As CheckBox = CType(row.FindControl("CheckBox1"), CheckBox)
            If chk.Checked = True Then
                countries += "'" & row.Cells(2).Text & "',"
            End If
        Next

        MsgBox(countries)
    End Sub

Part B: Code to create the Grid with check boxes in .aspx file
<asp:GridView ID="GridView1" runat="server"
            AllowSorting="True">
            <AlternatingRowStyle BackColor="#CCFF99" />
            <Columns>
             <asp:TemplateField HeaderText="Select" >
                    <ItemTemplate>
                        <asp:Checkbox ID="Checkbox1" runat="server" Text=""  />
                    </ItemTemplate>
                    </asp:TemplateField>
            </Columns>
        </asp:GridView>

Part C: code to load Grid with ACCESS and checkbox
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataSourceID="SqlDataSource" Width="739px" AllowPaging="True"
            AllowSorting="True">
            <AlternatingRowStyle BackColor="#CCFF99" />
            <Columns>
             <asp:TemplateField HeaderText="Select" >
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" Text=""  />
                    </ItemTemplate>
                    </asp:TemplateField>
                <asp:BoundField DataField="COUNTRY" HeaderText="COUNTRY"
                    SortExpression="COUNTRY" />
                <asp:BoundField DataField="AGD4" HeaderText="AGD4" SortExpression="AGD4" />
                <asp:BoundField DataField="FIF" HeaderText="FIF" SortExpression="FIF" />
                <asp:BoundField DataField="NSN" HeaderText="NSN" SortExpression="NSN" />
                <asp:BoundField DataField="NAS" HeaderText="NAS" SortExpression="NAS" />
                <asp:BoundField DataField="SN" HeaderText="SN" SortExpression="SN" />
            </Columns>
        </asp:GridView>
0
Comment
Question by:vcharles
  • 2
  • 2
4 Comments
 
LVL 12

Expert Comment

by:jagssidurala
ID: 36545312
Instead of boundfield for country, place a label to display country text in template field and then
Find country label control like you are finding check box, get country name from label text...............
0
 

Author Comment

by:vcharles
ID: 36546204
I'm sorry I don't follow your explanation. I am not using the code with Boundfield in Part C. I am using the codes in Parts A and B. Can you please send me an example.

Thanks,

Victor
0
 
LVL 12

Accepted Solution

by:
jagssidurala earned 500 total points
ID: 36551183
Change PART C Code like below:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataSourceID="SqlDataSource" Width="739px" AllowPaging="True"
            AllowSorting="True">
            <AlternatingRowStyle BackColor="#CCFF99" />
            <Columns>
             <asp:TemplateField HeaderText="Select" >
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" Text=""  />
                    </ItemTemplate>
                    </asp:TemplateField>
      <asp:TemplateField HeaderText="Select" >
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%#Eval("COUNTRY")%>'  />
                    </ItemTemplate>
                    </asp:TemplateField>
             
                <asp:BoundField DataField="AGD4" HeaderText="AGD4" SortExpression="AGD4" />
                <asp:BoundField DataField="FIF" HeaderText="FIF" SortExpression="FIF" />
                <asp:BoundField DataField="NSN" HeaderText="NSN" SortExpression="NSN" />
                <asp:BoundField DataField="NAS" HeaderText="NAS" SortExpression="NAS" />
                <asp:BoundField DataField="SN" HeaderText="SN" SortExpression="SN" />
            </Columns>
        </asp:GridView>

Change PART A CODE like below:

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
        Dim countries As String = ""
        For Each row As GridViewRow In GridView1.Rows
            Dim chk As CheckBox = CType(row.FindControl("CheckBox1"), CheckBox)
Dim lblCountry As Label = CType(row.FindControl("Label1"), Label
            If chk.Checked = True Then
                countries += "'" & lblCountry.Text & "',"
            End If
        Next

        MsgBox(countries)
    End Sub
0
 

Author Closing Comment

by:vcharles
ID: 36581846
Thank You!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now