?
Solved

Select all rows in datagridview

Posted on 2016-10-26
15
Medium Priority
?
38 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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
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 1000 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 1000 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

Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

770 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