Tag Archives: DataGridView

Load Excel records into a DataGridView in C#

This example shows how you can load Excel records into a DataGridView control. You can use similar techniques to load Excel data into other controls such as other kinds of grid controls or a TreeView control. This example uses the … Continue reading

Posted in controls, database, Excel, interoperability | Tagged , , , , , , , , , , , , | 2 Comments

Update a DataTable that is bound to a DataGridView in C#

The example Build a DataTable and bind it to a DataGridView in C# shows how you can bind a DataTable to a DataGridView to display data. This example shows how you can add new data to the display. When the … Continue reading

Posted in controls, database | Tagged , , , , , , , , , , , | Leave a comment

Highlight the DataGridView row that is under the mouse in C#

Someone recently asked me how to change the style of the row under the mouse in a DataGridView control. This example does that. When the program starts, the following code prepares the DataGridView for use. // The style to use … Continue reading

Posted in controls, user interface | Tagged , , , , , , , , , , , | Leave a comment

Format DataGridView columns in C#

This example shows how you can format DataGridView columns to make them display values properly. The control is actually pretty smart and will usually do the right thing if you give it the right data. For example, if a column … Continue reading

Posted in controls | Tagged , , , , , , , , , , , | 2 Comments

Handle DataGridView errors in C#

This example shows how you can handle DataGridView errors when you modify the data in the DataGridView control. The example Build a DataTable and bind it to a DataGridView in C# shows how to use a DataGridView control to display … Continue reading

Posted in controls, database | Tagged , , , , , , , , , , , , , , | 1 Comment

Build a DataTable and bind it to a DataGridView in C#

This example shows how you can build a DataTable and bind it to a DataGridView control. A DataTable is an in-memory representation of a relational database table. It can define columns of particular data types and even enforce uniqueness and … Continue reading

Posted in controls, database | Tagged , , , , , , , , , , , | 15 Comments

Execute ad hoc SQL queries in C#

This example shows how you can execute an ad hoc SQL query at run time. Normally a program uses Recordsets, DataGrids, LINQ to SQL objects, and other database objects and controls to display and manipulate database records. Those techniques tend … Continue reading

Posted in ADO.NET, database, SQL | Tagged , , , , , , , , , , , , , | 3 Comments

Calculate and highlight DataGridView values in C#

This example shows how to calculate and highlight DataGridView values at run time. It uses the following code to initialize its DataGridView control. It sets each item’s description, unit price, and quantity, but not its total cost. // Make some … Continue reading

Posted in controls, user interface | Tagged , , , , , , , , , | 3 Comments

Initialize DataGridView controls with objects in C#

This example shows how to easily initialize DataGridView controls to display the properties of items. This example uses the following OrderItem class. Notice that the constructor calculates the TotalCost from the Quantity and UnitPrice. public class OrderItem { public string … Continue reading

Posted in controls, user interface | Tagged , , , , , , , | Leave a comment

Set DataGridView column styles in C#

You can set DataGridView column styles by setting a column’s DefaultCellStyle property. It determines the column’s appearance including its foreground color, background color, font, alignment, and colors to use when an item is selected. To set a column’s style at … Continue reading

Posted in controls, user interface | Tagged , , , , , , , , , , , | 1 Comment