Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridView default behavior in Column Header
 

DataGridView default behavior in Column Header

I glanced through MSDN for default behavior for clicking -1, -1 cell but found nothing. The default behavior is to select all cells where I want to perform a custom action without selecting all cells and if CurrentCell is set keep it selected.

I was hoping that there might be either a setting to turn off selecting all cells prior to loading the DataGridView or a property to tell the DataGridView not to select all cells in an event of the DataGridview.

Thanks for any insight or suggestions


KSG
Kevininstructor  Monday, August 24, 2009 8:56 PM
Hi Kevininstructor,

It is hard to change this built-in action when you click the cell(-1, -1). If you handle the CellClick event, It will select all cells before firing the event. The following code is trying to clear selection and reselect the previous selected cell.
Public Class Form1
Private dt As DataTable = New DataTable()
Private selectedCell As DataGridViewSelectedCellCollection

Public Sub New()
InitializeComponent()
dt.Columns.Add("Colum1")
dt.Columns.Add("Colum2")
For i As Integer = 0 To 20
dt.Rows.Add(i, "Item" & i.ToString())
Next
DataGridView1.DataSource = dt
DataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect
End Sub

Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
If e.ColumnIndex = -1 And e.RowIndex = -1 Then
DataGridView1.ClearSelection()
For i As Integer = 0 To selectedCell.Count - 1
selectedCell(i).Selected = True
Next
End If

End Sub

Private Sub DataGridView1_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.CurrentCellChanged
selectedCell = DataGridView1.SelectedCells
End Sub
End Class

This solution is not perfect, you can see all the cells being selected and then unselected. Just like a flicker on the screen.

If I misunderstood you, please feel free to tell me.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Thursday, August 27, 2009 3:49 AM
Not sure I understand your question. You are clicking in the header of the DataGridView and it is selecting all cells?
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Tuesday, August 25, 2009 4:10 PM
Not sure I understand your question. You are clicking in the header of the DataGridView and it is selecting all cells?
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!

Image of what I am trying to do
Here is a stand-along VB.NET project showing what I want to do with MultiSelect off and want to do the same with MultiSelect on so that the user does not lose their selections.

When MultiSelect is set to True if the user had say five cells selected and clicks the cell in the upper left hand corner as shown in figure one all cells are selected and I am asking if there is anyway to circumvent this default behavior.

Thanks for taking the time to assist.
KSG
Kevininstructor  Wednesday, August 26, 2009 2:48 PM
Hi Kevininstructor,

It is hard to change this built-in action when you click the cell(-1, -1). If you handle the CellClick event, It will select all cells before firing the event. The following code is trying to clear selection and reselect the previous selected cell.
Public Class Form1
Private dt As DataTable = New DataTable()
Private selectedCell As DataGridViewSelectedCellCollection

Public Sub New()
InitializeComponent()
dt.Columns.Add("Colum1")
dt.Columns.Add("Colum2")
For i As Integer = 0 To 20
dt.Rows.Add(i, "Item" & i.ToString())
Next
DataGridView1.DataSource = dt
DataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect
End Sub

Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
If e.ColumnIndex = -1 And e.RowIndex = -1 Then
DataGridView1.ClearSelection()
For i As Integer = 0 To selectedCell.Count - 1
selectedCell(i).Selected = True
Next
End If

End Sub

Private Sub DataGridView1_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.CurrentCellChanged
selectedCell = DataGridView1.SelectedCells
End Sub
End Class

This solution is not perfect, you can see all the cells being selected and then unselected. Just like a flicker on the screen.

If I misunderstood you, please feel free to tell me.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Thursday, August 27, 2009 3:49 AM
Hi Kevininstructor,

It is hard to change this built-in action when you click the cell(-1, -1). If you handle the CellClick event, It will select all cells before firing the event. The following code is trying to clear selection and reselect the previous selected cell.
Public Class Form1
Private dt As DataTable = New DataTable()
Private selectedCell As DataGridViewSelectedCellCollection

Public Sub New()
InitializeComponent()
dt.Columns.Add("Colum1")
dt.Columns.Add("Colum2")
For i As Integer = 0 To 20
dt.Rows.Add(i, "Item" & i.ToString())
Next
DataGridView1.DataSource = dt
DataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect
End Sub

Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
If e.ColumnIndex = -1 And e.RowIndex = -1 Then
DataGridView1.ClearSelection()
For i As Integer = 0 To selectedCell.Count - 1
selectedCell(i).Selected = True
Next
End If

End Sub

Private Sub DataGridView1_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.CurrentCellChanged
selectedCell = DataGridView1.SelectedCells
End Sub
End Class

This solution is not perfect, you can see all the cells being selected and then unselected. Just like a flicker on the screen.

If I misunderstood you, please feel free to tell me.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira, you gave me the proper path and more important the idea that the built-in behavior can not be changed with a standard DataGridView. My guess is for an elegant solution I would need to inherit from DataGridView so that I might override the full selection when pressing cell -1,-1 but do not think it is worth the effort. Anyways a flicker is no problem with this in mind. Thanks!

KSG
Kevininstructor  Thursday, August 27, 2009 1:25 PM

You can use google to search for other answers

Custom Search

More Threads

• how to show records as per in else condition in sql
• Binding DataGridView to XML node or document
• Datagrid - Capturing Updates To Cells
• ForEach and ByRef in LINQ
• Datagridview embedded in a usercontrol loses DefaultCellStyle.BackColor after load
• Creating a Table Adapter from Access DB wrong date format
• DataBinding fails on reload problem
• tables
• BindingSource.EndEdit not saving to datasource.
• Detect if Ctrl is pressed when a ColumnHeader is clicked