Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

Chapter 19 Data Presentation Using the DataGridView Control 489


To Do This
Sort the records in a data
grid view object at run time

Click the column header that you want to sort by. Visual Studio sorts
the data grid view object alphabetically based on that column.
Reverse the direction
of a data grid view sort at
run time

Click the column header a second time to reverse the direction of the
sort (from A–Z to Z–A).

Change the default column
width for a column in a data
grid view object

In the Properties window, click the Columns property, and then the
ellipsis button. In the Edit Columns dialog box, adjust the Width
property.
Hide column headers in
a data grid view object

Set the ColumnHeadersVisible property to False.

Create an alternating color
scheme for rows within
a data grid view object

Pick a color scheme for alternating rows by using the
AlternatingRowsDefaultCellStyle property. In the CellStyle Builder
dialog box, adjust the BackColor property. The color that you select
will alternate with white.
Change the color of
gridlines in a data grid
view object

Adjust the GridColor property.

Add a second data grid
view object to a form

Drag a second table from the Data Sources window to the form.
(It can be the same table that you used in the first data grid view
object, or a second table within the database .) Resize and customize
the table, taking care to make the form large enough to display all the
database columns and records that your user will want to see. If you
want to add a second navigation bar to the form to provide access
to the table, create a second BindingNavigator control on the form,
and set its BindingSource property to the binding source representing
the new table you created.
Prevent the user from
editing or changing the data
in a data grid view object

Set the data grid view object’s ReadOnly property to True.

Write changes made in the
data grid view object back
to the underlying database

Verify that the data grid view object’s ReadOnly property has
been set to False. Then at run time, use the Save Data button on
the navigation bar to save your changes and update the database.
Alternatively, you can use the table adapter’s Update method or the
Me.TableAdapterManager.UpdateAll method within program code.
Free download pdf