Solved

Help with using GridView with checkboxes using XML data souce

Posted on 2011-09-15
4
297 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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