Академический Документы
Профессиональный Документы
Культура Документы
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.
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>
/// </summary>
{
//
//
//
// 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]
/// <summary>
/// </summary>
class TestObject
void M()
{
OneValue = 2,
TwoValue = 3
};
list.Add(test1);
dataGridView1.DataSource = list;
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.
/// </summary>
void M()
//
//
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = title;
dataGridView1.Rows[n].Cells[1].Value = dateTimeNow;
//
//
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]
/// </summary>
void M()
//
// DateTime is just for this example, and the Cells[1] part just
// takes the second cell for this example.
//
//
//
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)
//
//
SetupButtons();
/// <summary>
/// Custom method that sets the Enabled property of a button.
/// </summary>
void SetupButtons()
//
//
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.
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>
{
get
DataGridViewHeaderBorderStyle.None :
DataGridViewHeaderBorderStyle.Raised;
Topic Notes
DataGridView property sheet See the author's notes about DataGridView properties.
[DataGridView Property Notes - dotnetperls.com]
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]
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.