?
Solved

Help with selecting all row in DataGridView using VB.NET

Posted on 2013-10-27
10
Medium Priority
?
368 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 64

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 64

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
Google Certified Professional - Cloud Architect

This course (1 of 3) is designed to help students who are interested in Google Cloud Platform (GCP) to become familiar with the platform, navigate the console and learn its capabilities. It will also prepare students for the Google Cloud Architect certification exam.

 

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 64

Accepted Solution

by:
Fernando Soto earned 2000 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 64

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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

718 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