Solved

Help with selecting all row in DataGridView using VB.NET

Posted on 2013-10-27
10
357 Views
Last Modified: 2013-10-27
Hi,

If My DataGrid  contains

BEL
CAN
USA

When I click on the All button, how do I obtain

BEL,CAN,USA

Thanks,

Victor
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
  • 6
  • 4
10 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39603867
Hi Victor;

Seeming you want all rows you will need to enumerate through all the rows of the DataGrid and build the string from the column that has BEL, CAN, USA in it.
0
 

Author Comment

by:vcharles
ID: 39604034
Hi,


Can you please send the code on how to achieve this,

Thanks,

Victor
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39604047
How do you fill the DataGrid?
What is the source of the data?
What is the Column name that the data is stored in?

Please post your code.
0
Technology Partners: 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!

 

Author Comment

by:vcharles
ID: 39604082
Hi,

I am using the following code to load the Grid.

   Dim dtset1 As New DataSet
        Dim fs1 As System.IO.FileStream
        fs1 = New System.IO.FileStream(Application.StartupPath + "\Country.xml", IO.FileMode.Open)
        dtset1.ReadXml(fs1)
        fs1.Close()
        Me.C1TrueDBGrid3.DataSource = dtset1.Tables(0)
        Me.C1TrueDBGrid3.Columns(1).Visible = False
        C1TrueDBGrid3.DataSource = dt.DefaultView.ToTable(True, New String() {"Country"})

Victor
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39604130
Hi Victor;

This code sample should give you what you need.

' Used to build the string with Country
Dim sb As New StringBuilder()
' A comma delimited string with country codes
Dim countryStr As String = String.Empty

' Enumerate through all the rows selecting the country column
For Each c In C1TrueDBGrid3.Rows
    ' Build the comma delimited string
    sb.Append(c.Cells("Country").Value + ", ")
Next

' If the sb object has any values remove the last comma
If sb.Length <> 0 Then
    countryStr = sb.ToString().Substring(0, sb.Length - 2)
End If

Open in new window

0
 

Author Comment

by:vcharles
ID: 39604171
I'm afraid all the rows were not selected, the DataGrid also contains a column with checkboxes, could that be why the code does not work? Code below is in button click event to display data in the Grid.

Private Sub CheckControl()
        Dim CheckBoxCol As New DataGridViewCheckBoxColumn
        With CheckBoxCol
            .Name = "CheckBoxColumn"
            .HeaderText = "Select"
            .Width = 40
        End With
        Me.C1TrueDBGrid3.Columns.Insert(0, CheckBoxCol)
    End Sub
0
 

Author Comment

by:vcharles
ID: 39604209
Hi,

Below is the code that worked with a third party Grid (ComponentOne), but
getting error message related to splits for the DataGrid.

Error: Splits is not a member of DataGridView

 Dim count As Integer = Me.C1TrueDBGrid3.Splits(0).Rows.Count

        Me.C1TrueDBGrid3.ClearSelection()
        'Loop through rows and add each to the selected rows collection

        For i = 0 To count - 1
            Me.C1TrueDBGrid3.SelectedRows.Add(i)
        Next
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39604276
What happens when you use the code I posted?
0
 

Author Comment

by:vcharles
ID: 39604365
You code works, but I was not clear in my question, I also would like to select all the rows when I press the all button and clear all the row when I click on the clear button. If necessary I can create another post.

Thanks.
0
 

Author Closing Comment

by:vcharles
ID: 39604377
Thank You!
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

759 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