Programmatically Changing Column Width in Windows Forms Datagrid
I actually spent sometime in getting all the pieces right and so thought I would mention it on the blog so that many will not have to spend the same time...
Do not get bothered by the length of the code... It is pretty simple... I will explain the same in steps below... The exact problem statement is
" There is a windows form datagrid, which needs to get the data dynamically from "where ever you please". For this I will have to have a DataTable and have that set as the DataSource for the datagrid by dynamically binding it...
To fill this datatable after I get the data (say in form of xml or Web service objects; I need to create corresponding columns and add rows one by one to the same after parsing my data. Now as you make the columns programmatically you also wish to set the column width and other properties programatically... You might also want to format the table to look like you wish programatically... Below code snippet will tell you how to do it..."
NOTE: Do not look at the code from coding standards, maintainability, performance tuning etc perspective... It just merely does the job...
1.) Create a DataTable...
2.) Add necessary columns to it...
3.) Create individual rows for the data gathered... [My data is from Google Web Service]
4.) Bind the data to the datasource of the datagrid
5.) Create new DataGridTableStyle
6.) Set the mapping name of of the tablestyle to be same as your tablename
7.) Create the tablestyle as you wish... This can be kindof skin template you get from somewhere
8.) Add the TableStyle to your DataGrid
9.) Now get back the handle to that TableStyle
10.)Now from the GridColumStyle collection of your just obtained TableStyle, access the particular GridColumnStyle via indexs and modify the values...
11.)Call Refresh method on DataGrid from the just obtained TableStyle...
Below code does the above mentioned steps...
Dim resultTable As New Data.DataTable
Dim currentCount As Integer = 0
Dim counter As Integer
For counter = 0 To 9
Dim resultRow As DataRow
resultRow = resultTable.NewRow()
resultRow.Item(0) = (currentCount + 1).ToString()
resultRow.Item(1) = result.resultElements(currentCount).title.ToString()
resultRow.Item(3) = result.resultElements(currentCount).URL.ToString()
If currentCount >= result.resultElements.Length - 1 Then
currentCount += 1
resultTable.TableName = "myTable"
dgdResults.DataSource = resultTable
Dim myGridTableStyle As New DataGridTableStyle
myGridTableStyle.MappingName = "myTable"
myGridTableStyle.HeaderFont = New System.Drawing.Font _
("Tahoma", 8.25F, FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, System.Byte))
myGridTableStyle.HeaderBackColor = Color.Thistle
myGridTableStyle.HeaderForeColor = Color.Crimson
myGridTableStyle.ReadOnly = True
myGridTableStyle.AllowSorting = False
myGridTableStyle.AlternatingBackColor = Color.Thistle
myGridTableStyle.BackColor = Color.LavenderBlush
myGridTableStyle.ForeColor = Color.DarkBlue
myGridTableStyle.PreferredRowHeight = 20
myGridTableStyle.RowHeaderWidth = 16
Dim tableStyle As DataGridTableStyle = dgdResults.TableStyles("myTable")
tableStyle.GridColumnStyles(0).Width = 20
tableStyle.GridColumnStyles(1).Width = 120
tableStyle.GridColumnStyles(2).Width = 170
tableStyle.GridColumnStyles(3).Width = 250