Sei sulla pagina 1di 2

DatagridView Tricks

http://www.samuelbosch.com/2009/08/datagridview-tricks.html
This post will entirely be about the .NET DatagridView control. I will show you
how to synchronize the scrolling of two Datagridviews, disable column sorting, d
isable cell selection and focus cues.
To synchronize the scrolling of two DatagridViews you should subscribe to the Sc
roll event of the two Datagridviews and add the following code. What it does is
equalizing the first row index and the scrolled horizontal offset. I did set the
FirstDisplayedScrollingRowIndex because the VerticalScrollingOffset is a readon
ly property.
private void dataGridViewA_Scroll(object sender, ScrollEventArgs e)
{
dataGridViewB.FirstDisplayedScrollingRowIndex = dataGridViewA.FirstDisplayed
ScrollingRowIndex;
dataGridViewB.HorizontalScrollingOffset = dataGridViewA.HorizontalScrollingO
ffset;
}
private void dataGridViewB_Scroll(object sender, ScrollEventArgs e)
{
dataGridViewA.FirstDisplayedScrollingRowIndex = dataGridViewB.FirstDisplayed
ScrollingRowIndex;
dataGridViewA.HorizontalScrollingOffset = dataGridViewB.HorizontalScrollingO
ffset;
}
For the following tricks I created a custom control called DatagridViewGS that d
erives from the DatagridView control.
Disabling column sorting for all columns is done by setting the SortMode to NotS
ortable for every column of the DatagridView. In order to do that I subscribed t
he DataBindingComplete event and added the following code to it
void DatagridViewGS_DataBindingComplete(object sender, DataGridViewBindingComple
teEventArgs e)
{
foreach (DataGridViewColumn c in Columns)
{
c.SortMode = DataGridViewColumnSortMode.NotSortable;
}
}
Cell selection is disabled by subscribing to the CellStateChanged event and sett
ing the Selected property of the cell whose state changed to false. I've also se
t the SelectionMode of the DataGridView to DataGridViewSelectionMode.CellSelect
and the MultiSelect property to false.
void DatagridViewGS_CellStateChanged(object sender, DataGridViewCellStateChanged
EventArgs e)
{
if(e.StateChanged == DataGridViewElementStates.Selected)
e.Cell.Selected = false;
}
After disabling cell selection there where still focus cues. These are the small
dotted lines around the selected elements. To hide these I needed to override S
howFocusCues property of the DataGridView and make it always return false.
protected override bool ShowFocusCues

{
get
{
return false;
}
}
The full code of my custom DatagridView looked like this :
using
using
using
using
using

System;
System.Collections.Generic;
System.Text;
System.Windows.Forms;
System.Drawing;

namespace GisSolved.GUI
{
internal class DatagridViewGS:DataGridView
{
public DatagridViewGS()
{
MultiSelect = false;
SelectionMode = DataGridViewSelectionMode.CellSelect;
CellStateChanged += new DataGridViewCellStateChangedEventHandler(Dat
agridViewGS_CellStateChanged);
DataBindingComplete += new DataGridViewBindingCompleteEventHandler(D
atagridViewGS_DataBindingComplete);
}
// Disable column sorting
void DatagridViewGS_DataBindingComplete(object sender, DataGridViewBindi
ngCompleteEventArgs e)
{
foreach (DataGridViewColumn c in Columns)
{
c.SortMode = DataGridViewColumnSortMode.NotSortable;
}
}
// Disable cell selection
void DatagridViewGS_CellStateChanged(object sender, DataGridViewCellStat
eChangedEventArgs e)
{
if(e.StateChanged == DataGridViewElementStates.Selected)
e.Cell.Selected = false;
}
// Disable focus cues
protected override bool ShowFocusCues
{
get
{
return false;
}
}
}
}
Do you know other useful tricks with DatagridViews or other .NET controls ? Feel
free to write them down in the comments section !

Potrebbero piacerti anche