Solved

Help with selecting all row in DataGridView using VB.NET

Posted on 2013-10-27
10
350 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
Comment Utility
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
Comment Utility
Hi,


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

Thanks,

Victor
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:vcharles
Comment Utility
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
Comment Utility
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
Comment Utility
What happens when you use the code I posted?
0
 

Author Comment

by:vcharles
Comment Utility
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
Comment Utility
Thank You!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 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

8 Experts available now in Live!

Get 1:1 Help Now