Sie sind auf Seite 1von 8

C# DataGridView Tips and Secrets

Principio del formulario

dotnetperls.com

Go
Final del formulario

You want to improve the DataGridView in your Windows Forms application. These improvements can solve usability or aesthetic problems in your DataGridView program. Here we explore DataGridView usage and enhancements in Windows Forms, using the C# programming language.

Use DataGridView
First, you should use DataGridView 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. You can use the DataSource property to hook your DataGridView up to a database, or even an object collection.
Example that sets DataSource [C#] /// <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> 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. SqlDataAdapter Example

Use DataAdapters
In the .NET Framework, a DataAdapater class is an object 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, but these are extremely useful in many programs that use DataGridView.

Use 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.

Example that uses object collection [C#] /// <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;

Hide row headers


You can hide row headers in the DataGridView control by using the RowHeadersVisible property. 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. Disable row headers by setting RowHeadersVisible to false. This will provide the appearance in the screenshots, which is more streamlined.

Tabbing improvements
You can make tabbing work in your DataGridView control by modifying the StandardTab property in the Visual Studio designer view. 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.

Add rows
Here we see how you can add rows to the DataGridView by using the instance Add method in C# code. 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.
Example that adds rows [C#] /// <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. 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. 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. DateTime Examples Users can add rows. With the DataGridView control, users can add rows to your program on their own if you do not disallow it. There is a separate article that focuses on managing user-created data in the DataGridView. DataGridView Add Rows

Configure columns
You will find that Visual Studio provides dialogs that allow you to configure the columns in your DataGridView. Columns are used as templates for how your DataGridView renders columnar data. They do not actually contain the data, just the rules for rendering it to the screen. More information, including a screenshot, is available on this site. DataGridView Columns, Edit Columns Dialog

Locate current cell


Here we see how you can locate the current cell in your DataGridView by using 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.
Example that gets current cell [C#] /// <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; }

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.
Example that handles double-clicking [C#] void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { // // Do something on double click, except when on the header. // if (e.RowIndex == -1) { return; } ProceedOpen(); }

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.

Use SelectionChanged
You can update your program's buttons instantly when the user selects a cell in the DataGridView. Here 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.
Example that uses SelectionChanged [C#] 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); }

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. The user interface will respond instantly when selection is changed. People love it when their programs work better than they have come to expect. Selection options. The DataGridView has many options for adjusting its selection features. One option I have used a lot is the MultiSelect property; this is described in another article. MultiSelect Property (DataGridView)

Expand columns
You can specify that the columns in your DataGridView should expand by setting Columns.AutoSizeMode to Fill. This causes the cells to 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.

Alternating colors
It is also possible to configure your DataGridView so that the row colors alternate. This is useful for reducing errors caused by rows looking too similar to the program's users. Please check out the tutorial on rows and alternating colors. DataGridView Row Colors (Alternating)

Appearance on Vista
Here we see how you can improve the appearance of DataGridView on Windows Vista 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.
Example that improves Vista appearance [C#] 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;

} } }

Properties
Here we note that the DataGridView control has hundreds of complex properties. This site has more information on some of these properties in a separate article, although much of the information is not commonly useful. DataGridView Property Notes

PreviewKeyDown event
Here we look at what the PreviewKeyDown event on the DataGridView control can be used for. When your DataGridView handles events such as KeyDown, the PreviewKeyDown event can be used to filter some of these events, preventing incorrect behavior. This solves complex problems related to key input. PreviewKeyDown Event

VB.NET
Here we remember that the VB.NET programming language can use the DataGridView control in the same way as the C# language. The main difference is that the event handlers in the source code have different declarations and VB.NET programs use different default identifiers for Windows Forms controls. This site has a complete tutorial on DataGridView in VB.NET. DataGridView Usage

More hints
In this section, we look at some miscellaneous tips related to the DataGridView control in Windows Forms and the C# language. These properties and approaches here were

useful to me 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, I will try to expand it. dataGridView1.Columns[0].HeaderCell.SortGlyphDirection You can use this to draw that glyph array image. 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.

Tutorial
Here we note a related tutorial located on this web site. 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. DataGridView Tutorial

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

Das könnte Ihnen auch gefallen