Solved

Select all rows in datagridview

Posted on 2016-10-26
15
35 Views
Last Modified: 2016-10-26
Hello,

Need to set boolean value to true all the rows in datagridview.
Tried SelectAll method but,it does not put check or true value in the checkbox column.

The selection clolumn is added in datagridview as follows:

 If dg.Name <> "DgHelpUsers" Then
                .Columns.Add("Sel", GetType(Object))
                .Columns("Sel").SetOrdinal(0)
            End If
0
Comment
Question by:RIAS
[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
15 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41859942
Select will select things - it is not the same as setting a value in a check box.  They are two different things altogether.

I think you will have to loop through all the rows in the datagridview and explicitly set the value of the cheeckbox to whatever you want.
0
 

Author Comment

by:RIAS
ID: 41859943
Is there any other way apart from looping thr the rows in datagrdiview
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41859946
If the source is a database you could run an update query and then repopulate the grid.

Built in functionality will do things with the gridview.  You want to change the contents - which is your data and a bit away from the base functionality the gridview needs.
0
More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

 
LVL 4

Expert Comment

by:Elizabeth Anderson
ID: 41860006
0
 

Author Comment

by:RIAS
ID: 41860007
Tried SelectAll method but,it does not put check or true value in the checkbox column.
0
 

Author Comment

by:RIAS
ID: 41860010
Basically want to avoid looping through all the rows as its taking a lot of time.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41860016
>>Basically want to avoid looping through all the rows as its taking a lot of time.
Bear in mind:  Sometimes what one wants one can't have.

What sort of time is it taking?  How many rows?  Can you modify the underlying data then refresh the GUI?
0
 

Author Comment

by:RIAS
ID: 41860018
Andy,

I have rows like 50K.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41860024
And it works with reasonable performance?

Do you know about virtual mode ?
0
 

Author Comment

by:RIAS
ID: 41860039
It works but,just need a better way.
No not tried virtual mode.
0
 
LVL 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 250 total points
ID: 41860128
I use DatatGridViews extensively, and often have a need to do what you're asking. I've not yet found a better way other than as suggested - loop through all rows and set the value of your column as needed, or add a column to the data source that can be set via SQL before loading the DGV.

With that said: In your code above, are you adding a column to your DGV, or to a DataTable? Your code does not translate over to a DataGridView, but rather to a DataTable. The line below:

.Columns.Add("Sel", GetType(Object))

gives an error when I try to add that to a DataGridView. The second argument of that for a DGV column is the HeaderText value, not the DataType.

If you're adding a column to a Datatable, and you want that column to be "selected" (i.e. a checkbox) then you could do this:

Dim col As New DataColumn
col.Name = "SomeName"
col.DataType = GetType(Boolean)
col.DefaultValue = True
YourDataTable.Columns.Add col

This would add a Boolean column, and by default that column would be checked.
0
 

Author Comment

by:RIAS
ID: 41860164
I add column like this :
  Public Sub BindDatagridview(ByVal dt As DataTable, ByVal dg As DataGridView)
        Dim dv As DataView
        With dt
            'Outstanding  check List_MouseDown
            Dim SQL As String = String.Empty
            'Initialize the image column.
            If dg.Name <> "DgHelpUsers" Then
                .Columns.Add("Sel", GetType(Object))
                .Columns("Sel").SetOrdinal(0)
            End If
            If dt.Rows.Count <> 0 Then
                dg.DataSource = dt
                dg.Rows(0).Selected = False
                dg.ClearSelection()
                For inti = 0 To dg.DataSource.columns.count - 1
                    dg.Columns(inti).SortMode = DataGridViewColumnSortMode.Programmatic
                Next
                If dg.Name <> "DgHelpUsers" Then
                    With dg.Columns("Sel")
                        .ToolTipText = "Selection"
                        .HeaderText = ""
                        .DefaultCellStyle.ForeColor = Color.Transparent
                        .Width = 15
                    End With

                End If
            Else
                'The datatable is empty so load only the columns
                dv = New DataView(dt)
                'add code for sorting as original defaultview
                dg.DataSource = dv
            End If
        End With
    End Sub

Open in new window

0
 

Author Comment

by:RIAS
ID: 41860167
Scott,
The requirement is to keep it unselected on load then there is a button for selectAll.
Selectall will select all rows on the grid
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 250 total points
ID: 41860195
>>No not tried virtual mode.

Learn about it and try it.  You can display millions of records in a fraction of a second.  (Because you don't actually display millions, only those that are visible).
0
 

Author Closing Comment

by:RIAS
ID: 41860263
Cheers!
0

Featured Post

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

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