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

x
?
Solved

Datagridview selection changed event

Posted on 2010-09-17
25
Medium Priority
?
4,377 Views
Last Modified: 2013-11-07
Hi,
I have a code on DataGridView1_SelectionChanged
    Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged

       
        For Each r As DataGridViewRow In DataGridView1.SelectedRows
            r.Cells(0).Value = True
        Next

        DataGridView1.EndEdit()
        DataGridView1.Refresh()

The problem is that when I bind the grid with datatable its first row is selected by default which I deselect with clear selection on form load but even before that when it is just bound   DataGridView1.SelectionChanged
is called and keeps the forst row selected .Any way of stopping this event to be called everytime I have mouse down or is there any other alternative for selection changed event ?

Cheers
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
  • 11
  • 8
  • 6
25 Comments
 

Author Comment

by:RIAS
ID: 33699924
Any suggestions on how to retain the selected rows.Because everytime I have mouse down on the row the entire previous selection wipes out.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33699928
in form load event
For i as integer=0 to Me.DatagridView.Rows.Count-1
Me.DatagridView.Rows(i).Cells(0).Value=False
next
0
 

Author Comment

by:RIAS
ID: 33699946
Hi,
Tried that but no good ..
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33699954
sorry the checkboxcolun is databound?
0
 

Author Comment

by:RIAS
ID: 33699981
No it is not ..Cheers
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33699993
in which column do you have the checkboxcolumn (in which column Index)
0
 

Author Comment

by:RIAS
ID: 33699996
It is added in datatable.But not a result of any query from database.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33700022
i just confused....
after populating your datagridview please try to set this kind of code
For i as integer=0 to Me.DatagridView.Rows.Count-1
Me.DatagridView.Rows(i).Cells(0).Value=False
next
0
 
LVL 7

Accepted Solution

by:
klakkas earned 2000 total points
ID: 33700056
My friend, you should use a Flag indicating weather or not the "AutoCheck" is enabled.

Example:

Private AllowAutoCheck as Boolean = False

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       'Initialize your grid
       dim Dt as dataTable = DAL.GetmyData()
       DataGridView1.DataSource = Dt

       'Now allow for the auto check
       AllowAutoCheck = True
end Sub

And in the SelectionChanged:

 Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged

        If AllowAutoCheck = True then
                For Each r As DataGridViewRow In DataGridView1.SelectedRows
                    r.Cells(0).Value = True
                Next
                DataGridView1.EndEdit()
                DataGridView1.Refresh()
        End If
End Sub

This way you can enable / disable the AutoCheck feature from anywhere in your code.
0
 

Author Comment

by:RIAS
ID: 33700070
Will this not hamper the performance ..looping the entire grid ?
0
 
LVL 7

Expert Comment

by:klakkas
ID: 33700085
Further, if you only want AutoCheck to be performed by mouse selection only, you can do the following:


    Private Sub DataGridView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
        'Enable autocheck on mouse down
        AllowAutoCheck = True
    End Sub

    Private Sub DataGridView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseUp
        'Disable autocheck on mouse up
        AllowAutoCheck = False
    End Sub

0
 

Author Comment

by:RIAS
ID: 33700111
Hi klakkas,
That worked.Any suggestions on how to preserve the selected rows after having a mouse down on the grid as as soon as I have mouse down it looks that all my priviously selected  rows have disappeared...
 
0
 
LVL 7

Expert Comment

by:klakkas
ID: 33700161
Clicking on any row of the datagrid will Deselect all rows and Select the clicked row.

To avoid this, you can do several things:
1. Keep SHIFT pressed while clicking
2. Keep CTRL pressed while clicking
3. Always remember in your code the selected rows and when the DataGridView1_SelectionChanged event fires, programmatically add these rows to the selected.
0
 

Author Comment

by:RIAS
ID: 33700183
Hi,
Sorry for not making myself clear.
What I want to do is I select couple of rows by mouse dragging.Til here it works fine I have the 2 rows selected with true in my first column.Now i want to select 2 more rows by mouse dragging here --what happens is that I lose my previously selected rows and only have the current ones.
I need to keep those previously selected rows as well
 
0
 
LVL 7

Expert Comment

by:klakkas
ID: 33700192
If during the second mouse dragging you keep the CTRL key pressed, the previous selection will not be lost.

Try it and let me know.
0
 

Author Comment

by:RIAS
ID: 33700208
Is it possible without CTRL key as the second selection can be after some time as well and also this grid is on a tab control so the user might even chage tabs before he selects some new rows.
 
Cheers
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33700228
@Rias
"The problem is that when I bind the grid with datatable its first row is selected by default which I deselect with clear selection on form load but even before that when it is just bound   DataGridView1.SelectionChanged
is called and keeps the forst row selected"
I thing you talking about different Subjects :)

@Rias Said that he has a problem with the first CheckBox which is always selected...
Maybe i missunderstood but the problem is how to diselect this one ...am i right ? Correct me If so....

0
 
LVL 7

Expert Comment

by:klakkas
ID: 33700241
In this case you should go the 3rd suggestion:
3. Always remember in your code the selected rows and when the DataGridView1_SelectionChanged event fires, programmatically add these rows to the selected.

    Dim SelectedIndices As New List(Of Integer)
    Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged


        If AllowAutoCheck = True Then
            For Each r As DataGridViewRow In DataGridView1.SelectedRows
                r.Cells(0).Value = True
                If SelectedIndices.Contains(r.Index) = False Then
                    SelectedIndices.Add(r.Index)
                End If
            Next

            For Each ind As Integer In SelectedIndices
                If DataGridView1.Rows(ind).Selected = False Then
                    DataGridView1.Rows(ind).Selected = True
                End If
            Next

            DataGridView1.EndEdit()
            DataGridView1.Refresh()
        End If
    End Sub


Note that this way, you will never be able to De-Select a row via mouse clicks. In order to De-Select, you will have to programmatically remove the rowIndex from the SelectedIndices List.
0
 

Author Comment

by:RIAS
ID: 33700263
Yes jtoutou you are right.  the problem is how to diselect this one.
Your help and interest is appreciated.Will be right back after lunch break in 1 hour .
Cheers mate
0
 
LVL 7

Expert Comment

by:klakkas
ID: 33700290
@jtoutou

In order to understand better this thread, read RIA S previous one:
http://www.experts-exchange.com/Programming/Languages/.NET/Q_26480217.html
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33700318
I've read it already ..the question is what about when '"multiselection" change ..does the checkboxes return to false after that???
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33700552
ok here we are.....
new form as form1
and a datagridview
Imports System.Text
Imports System.Windows.Forms
Public Class Form1
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim checkCol As New DataGridViewCheckBoxColumn
        checkCol.Name = "Test"
        Dim txtcol As New DataGridViewTextBoxColumn
        Me.DataGridView1.Columns.Insert(0, txtcol)
        Me.DataGridView1.Columns.Insert(1, checkCol)
        Me.DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        For i As Integer = 0 To 10
            Me.DataGridView1.Rows.Add(i, i)
        Next
        For j As Integer = 0 To Me.DataGridView1.Rows.Count - 1
            Me.DataGridView1.Rows(j).Cells(1).Value = False
        Next
    End Sub

    Private Sub DataGridView1_CellMouseLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellMouseLeave
        Dim cbxCell2 As DataGridViewCheckBoxCell

        For Each DGVRows2 In DataGridView1.SelectedRows
            cbxCell2 = CType(DGVRows2.Cells(1), DataGridViewCheckBoxCell)
            If cbxCell2.Value Then
                cbxCell2.Value = False

            End If
        Next
    End Sub

   

   
    Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
        Dim cbxCell As DataGridViewCheckBoxCell

        For Each DGVRows In DataGridView1.SelectedRows
            cbxCell = CType(DGVRows.Cells(1), DataGridViewCheckBoxCell)
            If Not cbxCell.Value Then
                cbxCell.Value = True
            
            End If
        Next
        
    End Sub
End Class

Open in new window

0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33700592
some small corections and i'll comeback...there are mistakes.....!!
0
 

Author Comment

by:RIAS
ID: 33700671
Thanks jtoutou
0
 

Author Comment

by:RIAS
ID: 33700972
Hi klakkas,
Your suggestion  on Dim SelectedIndices As New List(Of Integer) is good but I see a long procedure in maintaing the rows selected and deselected.Will cell formatting help me in this ?

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
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…

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