[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Help with using GridView with checkboxes using XML data souce

Posted on 2011-09-15
4
Medium Priority
?
303 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
[X]
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
  • 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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. 
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

649 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