Вы находитесь на странице: 1из 8

DataGridView Tips and Secrets in C#

by Sam Allen

Problem. You want to improve the DataGridView in your Windows Forms application. Your
DataGridView has some usability or aesthetic problems. Solution. Explore DataGridView usage and
enhancements in this article.

1. When to use DataGridView


Use it when you need to display information that is easily broken up into columns. This will include
numbers, names, IDs, and other attributes stored in a database or in your program's memory.

2. Using DataSource
We can assign DataSource to a collection or database object. It's better to use the DataSource
property to hook your DataGridView up to a database, or even an object collection. Here we see the
code, but also go a step further and look at some surrounding code.
/// <summary>

/// Used to set up the data table when the user types a query.

/// </summary>

void BuildDataGrid()

dataGridView1.DataSource = GetSearchResults(queryStr);

/// <summary>

/// Connect to the database and then use an adapter to

/// fill a DataTable.

/// </summary>

/// <param name="queryStr">The query supplied by the user.</param>

/// <returns>The results in a table.</returns>

DataTable GetSearchResults(string queryStr)

{
//

// Make a new DataTable.

//

DataTable table = new DataTable();

//

// You will want to declare a new DataAdapter, and then call its fill method

// on the DataTable.

//

return table;

Description of DataSource example. After BuildDataGrid is called, the above code sets the
DataSource property to the results of another function, GetSearchResults. Next, it performs a custom
search. This is custom code that will query a database such as a full-text database for results.

Description of DataTable. It fills a new DataTable. We can use a SqlDataAdapter to fill this
DataTable object which we return. Finally, the results appear in your DataGridView. [C#
SqlDataAdapter Example - dotnetperls.com]

3. What are DataAdapters?


They are objects that uses internal logic to take data from a database and into an object. You will
need to add that and the SQL statements as well, which depend on what database you are using. This
topic is more in the ADO.NET realm.

4. Using DataSource with objects


Here we use a collection with an implementation of IList, which is an interface shared by Lists and
arrays. One great feature is that .NET will read the property names of your collection objects
automatically. Simply create a new List or array of objects, and set the DataSource to this.

/// <summary>

/// The test class for our example.

/// </summary>

class TestObject

public int OneValue { get; set; }

public int TwoValue { get; set; }

void M()
{

TestObject test1 = new TestObject()

OneValue = 2,

TwoValue = 3

};

List<TestObject> list = new List<TestObject>();

list.Add(test1);

list.Add(test2); // Not shown in code

dataGridView1.DataSource = list;

5. How can I hide ugly row headers?


Use RowHeadersVisible. When you create a new DataGridView, there will be ugly row headers with
arrows in the left-most column. These aren't useful for many kinds of applications.

Steps for disabling row headers. Disable row headers by setting RowHeadersVisible to false. This
will provide the appearance in the screenshots, which is more streamlined.

6. How can I make tabbing work?


With StandardTab. This property lets you make sure that when your user tabs around your window,
the tabbing events don't get stuck in the DataGridView. Use StandardTab in the designer to make the
tab key move out of the DataGridView and to the next control.

7. How you can add rows


Here we use the Add method. This method adds rows to the DataGridView programmatically. There
is a collection called Rows on the DataGridView. On the rows collection, there is a method called Add.
It is usually better to modify the DataSource, but sometimes this approach is useful.
/// <summary>

/// Shows example usage of Add method on Rows.

/// </summary>
void M()

//

// n is the new index. The cells must also be accessed by an index.

// In this example, there are four cells in each row.

//

int n = dataGridView1.Rows.Add();

dataGridView1.Rows[n].Cells[0].Value = title;

dataGridView1.Rows[n].Cells[1].Value = dateTimeNow;

//

// The second cell is a date cell, use typeof(DateTime).

//

dataGridView1.Rows[n].Cells[1].ValueType = typeof(DateTime);

dataGridView1.Rows[n].Cells[2].Value = wordCount;

Description of Add method. The code example calls Add on Rows. The DataGridView has a Rows
collection, which is simply a list of rows in the data grid. Add is an instance method on this collection.
It returns an index of the newly added row.

Description of row modification. The code modifies the new row. Add will give us the index of the
new row, so we can modify that row in-place using the regular syntax.

Using ValueType on DataGridView. The code example sets ValueType on cells. Every cell in the
data grid has a ValueType property. Usually, you don't need to worry about this. But if you want to
specify a DateTime column, you can change it by assigning it to a type, using typeof. [C# DateTime
Tips and Tricks - dotnetperls.com]

8. How you can locate the current cell


Here we use the CurrentCellAddress property. It returns the cell coordinates, which are also called
its location or Point. You can specify X or Y or both, but in this following example, we only take the Y
coordinate of the current cell. The current cell is also the selected cell, which usually has a blue
highlight.
/// <summary>

/// Shows example usage of how to get the current cell.

/// </summary>

void M()

//

// Go from Y coordinate to a selected cell's value.

// DateTime is just for this example, and the Cells[1] part just
// takes the second cell for this example.

//

int yCoord = dataGridView1.CurrentCellAddress.Y; // You can get X if you need it.

DateTime thisDate = (DateTime)dataGridView1.Rows[yCoord].Cells[1].Value;

9. How you can deal with double-clicks


Here we can use the CellDoubleClick event and check RowIndex. In the event handler for
CellDoubleClick, call the function you use for when an item is to be used. Note that you must check
for e.RowIndex equals -1, which indicates that the column headers were double-clicked and not a
regular cell.
void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)

//

// Do something on double click, except when on the header.

//

if (e.RowIndex == -1)

return;

ProceedOpen();

Note on using events in Visual Studio. As a reminder, click on the DataGridView in the designer,
and then look at the Property pane, which is on the right bottom corner usually. Then click on the
lightning bolt and scroll down to the CellDoubleClick entry. That's all you have to do to make the
event here.

10. How you can change controls when the selection moves
He we need to listen for SelectionChanged and change controls based on the selection. In the
functions called in your SelectionChanged code, check for CurrentCellAddress to figure out what was
selected and where the selection moved.
void dataGridView1_SelectionChanged(object sender, EventArgs e)

//

// When our selection changes, call the function SetupButtons

// to make sure "openButton" is always in a valid state.

//

SetupButtons();

/// <summary>
/// Custom method that sets the Enabled property of a button.

/// </summary>

void SetupButtons()

//

// Disable openButton when there are no items shown.

//

openButton.Enabled = (dataGridView1.RowCount > 0);

Description of SelectionChanged event handler. The code uses an event handler. This is
triggered whenever the selection changes in the DataGridView. This is convenient because you will
want to change the display when the selection is moved.

Using RowCount expression. The code checks RowCount. When RowCount is zero, then nothing is
selected, and our user probably can't proceed in your window. This allows us to disable or enable
buttons based on what is selected.

Having instance responses. The user interface will respond instantly when selection is changed.
People love it when their programs work better than they have come to expect.

11. How can I make the columns expand?


Set Columns.AutoSizeMode to Fill and the cells will expand to fill the horizontal area. Then, in the
designer, set some cells to fixed or percentage width. Just allow one column to fill up the remainder.
You will need to do a bit of manual tweaking.

12. How you can improve the appearance on Vista


We can improve the look with some custom conditional code. Windows Vista's DataGridView looks
best when it has no border, but Windows XP's looks best with a border. The code returns the
appropriate border attributes based on what operating system is running.
public partial class Form1 : Form

public Form1()

InitializeComponent();

dataGridView1.ColumnHeadersBorderStyle = ProperColumnHeadersBorderStyle;

/// <summary>

/// Remove the column header border in the Aero theme in Vista,

/// but keep it for other themes such as standard and classic.

/// </summary>

static DataGridViewHeaderBorderStyle ProperColumnHeadersBorderStyle

{
get

return (SystemFonts.MessageBoxFont.Name == "Segoe UI") ?

DataGridViewHeaderBorderStyle.None :

DataGridViewHeaderBorderStyle.Raised;

13. Misc. hints


These properties and approaches here were useful to the author at some point, but are not really
important or may not be the best way to do something. If something is really interesting or popular,
the author will try to expand it.

Topic Notes

DataGridView property sheet See the author's notes about DataGridView properties.
[DataGridView Property Notes - dotnetperls.com]

dataGridView1_PreviewKeyDow Can be used to "intercept" KeyDown events for easier processing.


n See the article on PreviewKeyDown.
[C# PreviewKeyDown Event - dotnetperls.com]

dataGridView1.Columns[0]. Use to draw that glyph array image.


HeaderCell.SortGlyphDirection Remember to remove the glyph arrow in ColumnHeaderMouseClick.

BackgroundColor Setting this to 'Window' often looks best.


Looks professional when you adjust this.

SelectionMode FullRowSelect enum value looks the best when you are displaying simple
result rows.

ColumnHeaderMouseClick This event is used to capture when a header is clicked. Sometimes you may
need to modify the direction that the sort glyph is pointing in the column
squares.
You can check e.ColumnIndex on the event parameter to find out which of
the column headers was clicked.

Font DataGridView inherits the font from its containing form, and you can set
the font to Segoe UI.
[C# Segoe UI in Windows Forms - dotnetperls.com]

Using Excel You can use your DataGridView with Microsoft Excel.
You need to add Excel Interop C# code.
[C# Excel Interop Use - dotnetperls.com]

14. Detailed tutorial on DataGridView


If you are confused by some aspects of DataGridView that aren't shown here, such as the Columns
collections, how to make alternating row colors, or how to use SqlDataAdapter and similar objects, try
the DataGridView tutorial. [C# DataGridView Tutorial - dotnetperls.com]

15. Summary
Here we saw how you can improve the usability and appearance of the DataGridView in your
program with these methods. No one loves DataGridView, but a good one can really make your
program shine. DataGridView is ideal in .NET for viewing information from databases or object
collections.

Вам также может понравиться