Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Help with using GridView with checkboxes using XML data souce

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

792 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