Solved

Help with using GridView with checkboxes using XML data souce

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 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