Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

The Baker's Dozen: 13 Productivity Tips for the Windows Forms DataGrid : Page 6

New developers often struggle with the .NET DataGrid when trying to replicate grid functionality from other platforms, while more experienced developers lament the deficiencies of the .NET DataGrid to address end user requirements. These tips will help both newbies and proficient developers work with the DataGrid.


advertisement
Tip 10: Selecting Multiple Rows and Reading Selected Rows
This is a straight-forward tip that has already been mentioned in this article. If you want to select a particular row in a DataGrid, just call the grid's Select() function, passing the row position relative to the current DefaultView. Note that you can select multiple rows in a grid (in advance of an operation that applies to multiple rows). There is always only one single position reported by the grid's Binding Manager.

To determine which row or rows have been selected, call the grid's IsSelected function, passing the row in question. The function ShowSelectedRows in the test form demonstrates an example of finding the selected rows and displaying them in a MessageBox.

Tip 11: Highlighting a Row
The code to highlight a DataGrid row is often found online in forums. In brief, you must define an event for the grid's MouseUp event. In the event, perform a HitTest for the coordinates reported by the MouseEventArgs, and get the row associated with the HitTest. Once you have the row, you can perform a grid SELECT() to highlight the entire row.

System.Drawing.Point pt = new Point(e.X,e.Y); DataGrid.HitTestInfo hti = this.HitTest(pt); if(hti.Type == DataGrid.HitTestType.Cell) { this.CurrentCell = new DataGridCell(hti.Row, hti.Column); this.Select(hti.Row); }

I have to pause for a moment and admit what might be a minority viewpoint: I'm not always a fan of highlighting the current row. So I've added a grid property called lHighlightRow to turn this behavior on and off.

Tip 12: Updating the Grid Caption
A grid caption can provide some general summary information about the contents of the grid. In the example, the caption can display the number of deductions and the total dollar value. You want to define a function to call at the beginning of the grid session, and also any time the RowFilter for the grid's DataSource changes.

In the same manner as you defined a function to color a column, you need to pass a custom function to the grid class, to indicate that any change in the RowFilter should automatically trigger a call to this custom function. The function call in the test form shows a call to SetCaptionFunction, passing the assembly and class name, along with a function called GridCaption, which can be found with the other FormManager function, CheckBalance.

public string GridCaption(DataView dv) { Decimal nDeductionAmt = 0; foreach(DataRowView Dr in dv) nDeductionAmt += Decimal.Parse(Dr["Balance"].ToString()); return dv.Count.ToString() + " deductions with an open balance of " + nDeductionAmt.ToString("C2"); }

Tip 13: Saving Preferences
End users expect software to save any settings they define. I'll demonstrate a quick example of saving column widths to an XML file, so that any column-sizing performed by users is retained when they reload the test form.

In the closing event of the test form, I have a method called SaveGridSettings. It simply scans through the GridColumns for the grid's default TableStyle, takes the widths, and saves them to a DataTable.



for(int nCtr=0; nCtr < oGrid.VisibleColumnCount; nCtr++) this.DtColWidths.Rows[nCtr]["ColumnWidth"] = oGrid.TableStyles[0].GridColumnStyles [nCtr].Width;

The DataTable (which is part of a DataSet) is then written to an XML file, so that when the form loads, the column width settings in the XML file can be used with AddText, AddDropDown, and so on. These settings are called to repopulate the grid. Users (like developers) always appreciate when the software remembers their preferences!

Although not part of the sample code, column settings aren't the only thing that could be saved here. The bookmarks could be saved as well, simply by adding a second table to the DataSet that is written to the XML file and writing a small function to add the saved bookmarks back into the bookmark drop-down list. Other miscellaneous settings (text search, the balance threshold, etc.) could be written out as well. Because a DataSet/XML file can have many DataTables with different structures, there really are very few limits as to the level of detail you can store. I was involved in a .NET application where the user settings XML file contained over 50 DataTables!

Closing Thoughts
The complete source code and project for this article can be pulled down from www.commongroundsolutions.net I hope that anyone who previously discounted the DataGrid might reconsider its capabilities after reading this article. It was only a short time ago that I set out to implement Tip #9 (highlighting a cell based on a condition), so I hope I've saved you a little of the wear and tear I experienced before I found it.

When I started in .NET back in late 2001, I was constantly looking for code samples to help translate my knowledge of other development tools. For many developers, a simple code sample can make all the difference in the world in understanding something. I encourage you to pull down the code and test project, and tweak it to your needs. I plan to maintain and update this library on our Web site (www.commongroundsolutions.net). If you have suggestions or ideas you'd like to share, I'd love to hear from you.



Kevin S. Goff is the founder and principal consultant of Common Ground Solutions, a consulting group that provides custom web and desktop software solutions in .NET, Visual FoxPro, SQL Server, and Crystal Reports. Kevin has been building software applications for 17 years. He has received several awards from the U.S. Department of Agriculture for systems automation. He has also received special citations from Fortune 500 companies for solutions that yielded six-figure returns on investment. He has worked in such industries as insurance, accounting, public health, real estate, publishing, advertising, manufacturing, finance, consumer packaged goods, and trade promotion. In addition, Kevin provides many forms of custom training.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date