Click here to Skip to main content
13,143,008 members (52,662 online)
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 1 May 2006

A Couple of Tricks for the Datagrid and Table Styles

, 1 May 2006
Rate this:
Please Sign up or sign in to vote.
This article talks about setting readonly on the datagrid along with swtiching between tableStyles and a generic method to access columns by name
Sample Image - DataGrid_TableStyles.gif


When I first started using datagrids in Windows apps, I found myself quite frustrated. There were a bunch of things I wanted to do, and I was pretty sure I could do them, but figuring out how to do them was a problem. So I just thought I would share a few things I have learned about datagrids and table styles.


In one of my first apps I used a datagrid in, I wanted to make the datagrid not allow inserts. It took me a while to figure out how to get that to work. Table styles were another thing that caused me some trouble. In this article, I will talk about what I learned.

The Code

The datagrid is a somewhat underdeveloped tool that we were given to use. I think the datagridview does a much better job in .NET 2.0, but that is a subject for another article. First I would like to talk about making a datagrid read-only. There is a property that allows you do make a datagrid readonly, but it does a bit more than that. There are really three things that happen when you set the readonly property on a datagrid. First is readonly, which doesn't allow you to type or over-type in a cell. Next you cannot insert a new row. Finally, you cannot delete an existing row. There are some cases where you will want to allow edits, but no inserts or deletes. Or even in some cases, you may want the user to edit only certain columns, but not others. These properties exist split out in a DataView. A DataView is the best option for the datasource of the datagrid. The code looks like this:

//DGSource is a dataset with the table we want to display in our datagrid.
DGSource.Tables[0].TableName = "CustomerInfo1";
DataView tmpDV = DGSource.Tables[0].DefaultView;
tmpDV.AllowDelete = false;
tmpDV.AllowEdit = false;
tmpDV.AllowNew = false;
dataGrid1.DataSource = tmpDV;

There are several articles on using TableStyles with DataGrids. I think the most important point is that the MappingName in the TableStyle must match the table name in the dataset. Notice in the above code that I set the TableName property before I do things with the DataView.

Mapping Name property for the tablestyle

Now here is an important point. If you want your application to be able to switch between different tableStyles, all you have to do is switch the tablename property to the different mappingname used in the couple of tablestyles you have added to your datagrid. In the sample app I have included, you will see a dropdown with a couple of tablestyle options to select from.

Finally, there is the issue of knowing where to get certain data when you have different table styles which can move the order of where the data might be in the datagrid. I have written this method, which takes in several parameters to pass back the correct data from the datagrid without having to hardcode column numbers.

private String ReturnDataGridValue(DataGrid p_dgd, Int32 p_row, String 
p_fieldName, String p_tableStyleName)
String ret = null;
DataGridTableStyle tmpTS = p_dgd.TableStyles[p_tableStyleName];
if (tmpTS != null)
  foreach (DataGridColumnStyle tmpDGCS in tmpTS.GridColumnStyles)
    if (tmpDGCS.MappingName == p_fieldName)
ret = p_dgd[p_row,tmpTS.GridColumnStyles.IndexOf(tmpDGCS)].ToString();
  } //foreach

return ret;

I have one last thing to note about datagrids. Often you want them to expand properly when the Windows form is maximized. I have found that you should NOT set a dock property. Instead use the anchor property. Once you have placed the datagrid where you want it, set the anchor property to top, bottom, left, right. This will cause the datagrid to expand properly when you change your form size.


So this was just a quick article about datagrids and tablestyles. I hope you found some of these tricks helpful for the problems you are working with.


  • 1st May, 2006: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
United States United States
I started my programmer career over 21 years ago doing COBOL and SAS on a MVS mainframe. It didn't take long for me to move into windows programming. I started my windows programming in Delphi (Pascal) with a Microsoft SQL server back end. I started working with when the beta 2 came out in 2001. After spending most of my programming life as a windows programmer I started to check out in 2004. I achieved my in April 2005. I have done a lot of MS SQL database stuff. I have a lot of experience with Window Service and Web services as well. I spent three years as a consultant programing in C#. I really enjoyed it and found the switch between and C# to be mostly syntax. In my current position I am programming in C# working on WPF and MSSql database stuff. Lately I have been using VS2015.

On a personal note I am a born again Christian, if anyone has any questions about what it means to have a right relationship with God or if you have questions about who Jesus Christ is, send me an e-mail. ben.kubicek[at]netzero[dot]com You need to replace the [at] with @ and [dot] with . for the email to work. My relationship with God gives purpose and meaning to my life.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170915.1 | Last Updated 1 May 2006
Article Copyright 2006 by kubben
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid