Solved

Help with selecting all row in DataGridView using VB.NET

Posted on 2013-10-27
10
352 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
  • 6
  • 4
10 Comments
 
LVL 62

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 62

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
 

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 62

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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 62

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

863 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now