Beruflich Dokumente
Kultur Dokumente
com/dotnet/Article/33748/0/page/2
devx.com
by Wei-Meng Lee
One very common use of the DataGridView control is binding to a table in a database. To illustrate this, I'll
add a Typed DataSet to the current project. In Visual Studio 2005, right-click on the project name in
Solution Explorer and select Add | New Item. Select the DataSet template (use the default name of
DataSet1.xsd) and click Add.
Launch Server Explorer (View | Server Explorer) and navigate to the Northwind sample database
(assuming you have it installed on SQL Server/SQL Server Express). Drag and drop the Customers table
onto the design surface of DataSet1.xsd. Figure 5 shows the creation of the typed dataset.
You have a choice of what to do for your next step. You can either bind the DataGridView control directly to
the table adapter of the Customers table, like this:
1 of 6 16-Jan-17 18:12
101 Ways to Manipulate the DataGridView Control : Page 2 about:reader?url=http://www.devx.com/dotnet/Article/33748/0/page/2
Note that before the code above could work, you'd need to import the namespace as follows (DVG is the
name of my project):
Imports DGV.DataSet1TableAdapters
2 of 6 16-Jan-17 18:12
101 Ways to Manipulate the DataGridView Control : Page 2 about:reader?url=http://www.devx.com/dotnet/Article/33748/0/page/2
The RowIndex and ColumnIndex properties will contain the row and column numbers, respectively, of the
cell currently selected.
The above code snippet shows that the DataView object allows users to add new rows (via the AllowNew
property) in the DataGridView control and also allows rows to be deleted (via the AllowDelete property). In
addition, you can sort the rows by specifying the field(s) and their corresponding sort order (via the Sort
property). Finally, you filter the records by specifying an SQL-like expression using the RowFilter property.
3 of 6 16-Jan-17 18:12
101 Ways to Manipulate the DataGridView Control : Page 2 about:reader?url=http://www.devx.com/dotnet/Article/33748/0/page/2
The above code snippet sorts the rows according to the ContactTitle field (in ascending order), followed by
the Address field (also in ascending order). If you want to sort the Address field in descending order, set it
as follows:
Figure 7shows the rows sorted based on the ContactTitle and Address fields in ascending order, versus that
for ContactTitle in ascending order and Address in descending order.
Sorting Columns
Besides using the DataView object for filtering and sorting, you can also control sorting using the
Sort()method from the DataGridView control itself, as demonstrated below:
Here, you are sorting the rows based on the first column, in descending order (see Figure 8).
Apart from programmatically sorting the columns, the user can also click on the sorting glyph (the
triangular icon displayed next to the column header) to sort the rows either in ascending or descending
order. To prevent a row from being sorted, set the SortMode to NotSortable:
4 of 6 16-Jan-17 18:12
101 Ways to Manipulate the DataGridView Control : Page 2 about:reader?url=http://www.devx.com/dotnet/Article/33748/0/page/2
'---adding columns---
DataGridView1.Columns.Add("ID", "Product ID")
DataGridView1.Columns.Add("Name", "Product Name")
DataGridView1.Columns.Add("Description", "Description")
DataGridView1.Columns.Add("Price", "Price")
Adding Rows
To add a new row to the DataGridView control, use the Add()method from the Rows property:
To add a row and populate it with content, create an instance of the DataGridViewRow class and then use
the CreateCells()method to create a row template:
For i As Integer = 0 To 9
'---create a row---
Dim item As New DataGridViewRow
item.CreateCells(DataGridView1)
With item
.Cells(0).Value = i
.Cells(1).Value = "Product " & i
.Cells(2).Value = "Description of Product " & i
.Cells(3).Value = "99.99"
End With
'---add the row---
DataGridView1.Rows.Add(item)
Next
Alternatively, you can also add a new row by creating an array containing the values for the various fields in
the row:
5 of 6 16-Jan-17 18:12
101 Ways to Manipulate the DataGridView Control : Page 2 about:reader?url=http://www.devx.com/dotnet/Article/33748/0/page/2
For i As Integer = 0 To 9
DataGridView1.Rows.Add(New String() _
{i, _
"Product " & _
i, _
"Description of Product " & i, _
"99.99"})
Next
Figure 10shows the output for the above two code snippets.
6 of 6 16-Jan-17 18:12