Hi James,
You can set all the properties in code, including the width of a column.
Here's a sample, stolen mainly from my colleague Bipin Joshi here:
http://www.dotnetbips.com/displayarticle.aspx?id=66
By looking at the properties in the documentation, you can see what else you
can set:
http://msdn.microsoft.com/library/d...ystemwebuiwebcontrolsdatagridmemberstopic.asp
Ken
MVP [ASP.NET]
Toronto
Dim dt As DataTable
Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
CreateGrid()
End Sub
Public Sub CreateGrid()
'declare a new datagrid and set properties
Dim DataGrid1 As New DataGrid
DataGrid1.BorderWidth = Unit.Pixel(2)
DataGrid1.CellPadding = 10
DataGrid1.GridLines = GridLines.Both
DataGrid1.BorderColor = Color.Blue
DataGrid1.ShowHeader = True
DataGrid1.AutoGenerateColumns = False
DataGrid1.SelectedItemStyle.BackColor = Color.Yellow
'add bound columns to the datagrid
Dim datagridcol As New BoundColumn
datagridcol.HeaderText = "Course"
datagridcol.ItemStyle.Width = Unit.Pixel(265)
datagridcol.DataField = "course"
DataGrid1.Columns.Add(datagridcol)
datagridcol = New BoundColumn
datagridcol.HeaderText = "First Name"
datagridcol.ItemStyle.Width = Unit.Pixel(100)
datagridcol.DataField = "firstname"
DataGrid1.Columns.Add(datagridcol)
Dim selectcol As New ButtonColumn
selectcol.ButtonType = ButtonColumnType.LinkButton
selectcol.Text = "Select"
selectcol.CommandName = "Select"
DataGrid1.Columns.Add(selectcol)
'add event handlers
AddHandler DataGrid1.SelectedIndexChanged, _
AddressOf DataGrid1_SelectedIndexChanged
'bind datagrid
DataGrid1.DataSource = GetDataSet()
DataGrid1.DataBind()
'add datagrid to the page
Page.Controls(1).Controls.Add(DataGrid1)
End Sub
Public Sub DataGrid1_SelectedIndexChanged _
(ByVal sender As Object, _
ByVal args As EventArgs)
'write your code here
End Sub
Function GetDataSet() As ICollection
' Create sample data for the DataList control.
dt = New DataTable
Dim dr As DataRow
' Define the columns of the table.
dt.Columns.Add(New DataColumn("firstname", GetType(String)))
dt.Columns.Add(New DataColumn("course", GetType(String)))
' Populate the table with sample values.
dr = dt.NewRow
dr(0) = "Ben"
dr(1) = "English"
dt.Rows.Add(dr)
dr = dt.NewRow
dr(0) = "Ben"
dr(1) = "Geology"
dt.Rows.Add(dr)
dr = dt.NewRow
dr(0) = "Ben"
dr(1) = "Physics"
dt.Rows.Add(dr)
Dim dv As DataView = New DataView(dt)
Return dv
End Function