Formatting Windows.Forms.DataGrid
Now I know that there are a ton of third party datagrid's out there that make formatting them much, much easier then Microsoft's, but they can also be quite expensive for the licenses so there is a good chance that you will be stuck with using the built-in grid. I'm going to take this time to share some of the knowledge that I have obtained while recently working with formatting this datagrid.
Now, I have read many an article on this subject, and I am going to repeat what probably most of those authors and possibly someone reading this might feel; formatting the windows.forms.datagrid is a right pain in the a**.
I have worked with two third-party grids, and they make this far easier by building in a "format" property to either the column or table object. I'm sure Microsoft has it's reasons for not doing this but, I don't know exactly why.
Instead of babbling on how about I plop in some sample code with explanations:
First of course is add a datagrid to a Windows Form (I am assuming that you know how to create a Windows Application), name it what ever you want.
Before we proceed I will mention something that I have not seen in other articles on this subject; You MUST have data in order to format the grid. You cannot just format and see the changes without having a datasource assigned to the grid.
I'll be using a SQL Table in this example. I created the following table:
tbCostImpact
Columns:
Type ; y2005 ; y2006 ; y2007 ; y2008
Values
Product Mix Current ; 10000 ; 15000 ; 20000 ; 25000
Product Mix Future ; 5000 ; 10000 ; 15000 ; 20000
Cost/Savings Increase ; +5000 ; +5000 ; +5000 ; +5000
Now I'm going to show a bunch of code, but I'll put comments around where I feel it is important. I am assuming that you have placed a Datagrid on a Windows or Web Form.
' I created a sub to populate and format the grid which I named dgCostImpact
Public Sub FormatdgCostImpact()
' Create a connection object (place your own values where it's in italics)
Dim conn As New SqlConnection("Data Source=servername;Initial Catalog=database;User ID=userid;pwd=password")
' Create the command, dataadapter, and dataset objects
Dim cmd As New SqlCommand("SELECT * FROM tbCostImpact", conn)
Dim da As SqlDataAdapter
da = New SqlDataAdapter(cmd)
Dim ds As New DataSet
' Fill the dataset
da.Fill(ds, "CostImpact")
' Now the fun begins with the datagrid formatting
' First declare a new DatagridTableStyle object (later this will be assigned to the datagrid)
Dim ts As New DataGridTableStyle
' This mapping name refers to the table in the dataset
ts.MappingName = "CostImpact"
' Next declare DataGridColumn objects, there are many different types of DataGridColumn
' I am using TextBox because it applies here, check out msdn for the other types
Dim col1 As New DataGridTextBoxColumn
Dim col2 As New DataGridTextBoxColumn
Dim col3 As New DataGridTextBoxColumn
Dim col4 As New DataGridTextBoxColumn
Dim col5 As New DataGridTextBoxColumn
' Here is where the formatting properties are set
With col1
.MappingName = "Type" ' Column Name
.HeaderText = "Type" ' Column Header
.Width = 175 ' Column Width
.TextBox.Enabled = True ' Enable the column
End With
' The above is repeated for each column
With col2
.MappingName = "y2005"
.HeaderText = "2005"
.Width = 50
.TextBox.Enabled = True
End With
With col3
.MappingName = "y2006"
.HeaderText = "2006"
.Width = 50
.TextBox.Enabled = True
End With
With col4
.MappingName = "y2007"
.HeaderText = "2007"
.Width = 50
.TextBox.Enabled = True
End With
With col5
.MappingName = "y2008"
.HeaderText = "2008"
.Width = 50
.TextBox.Enabled = True
End With
' Once the column properties have been set it is now time to add the formatted
' columns to the GridColumnStylesObject
With ts
.GridColumnStyles.Add(col1)
.GridColumnStyles.Add(col2)
.GridColumnStyles.Add(col3)
.GridColumnStyles.Add(col4)
.GridColumnStyles.Add(col5)
' Add some color formatting to the GridTableStyles object (just to pretty up the appearance)
.BackColor = System.Drawing.Color.LightSkyBlue
.HeaderBackColor = System.Drawing.Color.AliceBlue
End With
' Now just add the table style to the table, then set the table
' datasource to be the dataset from earlier
dgCostImpact.TableStyles.Add(ts)
dgCostImpact.DataSource = ds.Tables(0)
End Sub
And that's it, hopefully I have been able to shed some light on this subject for people, I know that I was having some troubles with it, but once you have done it a couple of times you will start to realize that yes it is a lot of code for simple formatting, but really it isn't that difficult.
Now, I have read many an article on this subject, and I am going to repeat what probably most of those authors and possibly someone reading this might feel; formatting the windows.forms.datagrid is a right pain in the a**.
I have worked with two third-party grids, and they make this far easier by building in a "format" property to either the column or table object. I'm sure Microsoft has it's reasons for not doing this but, I don't know exactly why.
Instead of babbling on how about I plop in some sample code with explanations:
First of course is add a datagrid to a Windows Form (I am assuming that you know how to create a Windows Application), name it what ever you want.
Before we proceed I will mention something that I have not seen in other articles on this subject; You MUST have data in order to format the grid. You cannot just format and see the changes without having a datasource assigned to the grid.
I'll be using a SQL Table in this example. I created the following table:
tbCostImpact
Columns:
Type ; y2005 ; y2006 ; y2007 ; y2008
Values
Product Mix Current ; 10000 ; 15000 ; 20000 ; 25000
Product Mix Future ; 5000 ; 10000 ; 15000 ; 20000
Cost/Savings Increase ; +5000 ; +5000 ; +5000 ; +5000
Now I'm going to show a bunch of code, but I'll put comments around where I feel it is important. I am assuming that you have placed a Datagrid on a Windows or Web Form.
' I created a sub to populate and format the grid which I named dgCostImpact
Public Sub FormatdgCostImpact()
' Create a connection object (place your own values where it's in italics)
Dim conn As New SqlConnection("Data Source=servername;Initial Catalog=database;User ID=userid;pwd=password")
' Create the command, dataadapter, and dataset objects
Dim cmd As New SqlCommand("SELECT * FROM tbCostImpact", conn)
Dim da As SqlDataAdapter
da = New SqlDataAdapter(cmd)
Dim ds As New DataSet
' Fill the dataset
da.Fill(ds, "CostImpact")
' Now the fun begins with the datagrid formatting
' First declare a new DatagridTableStyle object (later this will be assigned to the datagrid)
Dim ts As New DataGridTableStyle
' This mapping name refers to the table in the dataset
ts.MappingName = "CostImpact"
' Next declare DataGridColumn objects, there are many different types of DataGridColumn
' I am using TextBox because it applies here, check out msdn for the other types
Dim col1 As New DataGridTextBoxColumn
Dim col2 As New DataGridTextBoxColumn
Dim col3 As New DataGridTextBoxColumn
Dim col4 As New DataGridTextBoxColumn
Dim col5 As New DataGridTextBoxColumn
' Here is where the formatting properties are set
With col1
.MappingName = "Type" ' Column Name
.HeaderText = "Type" ' Column Header
.Width = 175 ' Column Width
.TextBox.Enabled = True ' Enable the column
End With
' The above is repeated for each column
With col2
.MappingName = "y2005"
.HeaderText = "2005"
.Width = 50
.TextBox.Enabled = True
End With
With col3
.MappingName = "y2006"
.HeaderText = "2006"
.Width = 50
.TextBox.Enabled = True
End With
With col4
.MappingName = "y2007"
.HeaderText = "2007"
.Width = 50
.TextBox.Enabled = True
End With
With col5
.MappingName = "y2008"
.HeaderText = "2008"
.Width = 50
.TextBox.Enabled = True
End With
' Once the column properties have been set it is now time to add the formatted
' columns to the GridColumnStylesObject
With ts
.GridColumnStyles.Add(col1)
.GridColumnStyles.Add(col2)
.GridColumnStyles.Add(col3)
.GridColumnStyles.Add(col4)
.GridColumnStyles.Add(col5)
' Add some color formatting to the GridTableStyles object (just to pretty up the appearance)
.BackColor = System.Drawing.Color.LightSkyBlue
.HeaderBackColor = System.Drawing.Color.AliceBlue
End With
' Now just add the table style to the table, then set the table
' datasource to be the dataset from earlier
dgCostImpact.TableStyles.Add(ts)
dgCostImpact.DataSource = ds.Tables(0)
End Sub
And that's it, hopefully I have been able to shed some light on this subject for people, I know that I was having some troubles with it, but once you have done it a couple of times you will start to realize that yes it is a lot of code for simple formatting, but really it isn't that difficult.

0 Comments:
Post a Comment
<< Home