Windows DataGrid Enhancments
I spent quite a bit of time this week enhancing the MM .NET Windows Forms DataGrid. Here are the enhancements I've made:
- Added a MultiSelect property that allows developers to specify if a grid should operate in multi-select mode (the default) or single-select mode
- Added a GetSelectedRows() method that returns a DataSet containing a list of currently selected rows.
- Added a SelectRowOnCellClick property that specifies if the entire row should be selected when a grid cell is clicked
- Added a SelectedRowCountChange event to the grid that is automatically fired when the number of selected rows changes. This is useful when you need to enable/disable another UI control (such as an OK button) asitems are selected/deselected.
- Added an AutoResizeColumns property to the grid that specifies if columns should be resized when the DataGrid is resized. You can resize all columns or specify a single column to be resized.
Regarding the last item, I saw a few solutions on the web that set the size of all columns to the same width and resized them equally. I went a bit further and added logic that resizes the columns proportional to their original sizes. All of this took just a bit of doing to make sure the timing was correct, because the size of the row header and presence of the vertical scroll bar must be taken into account when resizing the columns. What's nice about this solution is you don't have to spend all of the usual “pain in the neck” time getting the column sizes just right at design time. You can set them to a value that's close to what you want, and the resizing logic will automatically resize them proportionately to fill the grid.
Did You Know?
Normally, when you add a new item to a project in VS .NET, if the item is not under the project's root directory structure, it makes a copy of the file locally and adds it to the project.
If you would rather add a link to a file rather than making a local copy, in the Add Existing Item dialog, click the down arrow on the Open button and select Link File from the popup menu.
Kevin McNeish
.NET/C# MVP