[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Select all rows in datagridview

Posted on 2016-10-26
15
Medium Priority
?
40 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 45

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 45

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 45

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 45

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 45

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

650 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