ReadOnly WinForms DataGrid

Mon 3 April, 2006

Windows Forms’ DataGrid control allows users, by default, to modify the data shown and even to create new records.

What if we want to make a grid that only shows data, without the power to edit o make new data?

For that, one of the things we can make is modifying the default properties of the DefaultView from the table we’re going to feed the grid with. For instance, let’s say we have a DataTable dtData, acting as the grid DataSource. If we modify the AllowDelete, AllowEdit and AllowNew properties from the default view of the table, like this:

dtData.DefaultView.AllowDelete = false;
dtData.DefaultView.AllowEdit = false;
dtData.DefaultView.AllowNew = false;
grdData.DataSource = this.dtDatos;

Colorized by: CarlosAg.CodeColorizer

the grdData data grid won’t allow users to modify, delete or add new records.

In the other hand, if we want to select the whole row of the grid with just clicking on any of its cells, we have to write the following code on the CurrentCellChanged datagrid event.

private void grdData_CurrentCellChanged(object sender, EventArgs e)
{
    grdData.Select(grdData.CurrentRowIndex)
;
}

Colorized by: CarlosAg.CodeColorizer

Quite simply, this code tells the datagrid to select the entire row, knowing which row is the one thanks to the CurrentRowIndex property.

Comments »

The URI to TrackBack this entry is: http://codecruncher.blogsome.com/2006/04/03/15/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>