|
-
Sep 25th, 2002, 08:16 AM
#1
Thread Starter
Fanatic Member
Format column width in datagrid
I added the following 2 lines to public class of a form:
Dim dsPick As New DataSet()
Dim dtPick As New DataTable("Pick")
I then added a grid to a form and in the form load() event added the following code:
dsPick.Tables.Add(dtPick)dtPick.Columns.Add(New DataColumn("Code",
GetType(String)))
dtPick.Columns.Add(New DataColumn("Description", GetType(String)))
dtPick.Columns.Add(New DataColumn("Quantity", GetType(String)))
Me.datagrid1.DataSource = dtPick
When the user clicks a button on the form the following code is executed. The code adds a record.
Public Sub AddToTable()
Dim row As DataRow
row = dtPick.NewRow()
row(0) = "TEST STRING 1"
row(1) = "TEST STRING 2"
row(2) = "TEST STRING 3"
dtPick.Rows.Add(row)
End Sub
At this stage everything works fine but all the columns are of equal width.
Can anyone tell me how I go about setting the column widths in code?
-
Sep 25th, 2002, 10:19 AM
#2
Member
Basically, you must create ColumnStyles and add it to a TableStyle. Then, add the TableStyle to your datagrid.
Here is your code with some added code to set the column width.
Public Class Form1
Inherits System.Windows.Forms.Form
Dim dsPick As New DataSet()
Dim dtPick As New DataTable("Pick")
Dim dgts As New DataGridTableStyle()
Dim dgcs1 As New DataGridTextBoxColumn()
Dim dgcs2 As New DataGridTextBoxColumn()
Dim dgcs3 As New DataGridTextBoxColumn()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dsPick.Tables.Add(dtPick)
dtPick.Columns.Add(New DataColumn("Code", GetType(String)))
dtPick.Columns.Add(New DataColumn("Description", GetType(String)))
dtPick.Columns.Add(New DataColumn("Quantity", GetType(String)))
Me.DataGrid1.DataSource = dtPick
dgcs1.MappingName = "Code"
dgcs1.Width = 75
dgcs2.MappingName = "Description"
dgcs2.Width = 125
dgcs3.MappingName = "Quantity"
dgcs3.Width = 75
dgts.GridColumnStyles.Add(dgcs1)
dgts.GridColumnStyles.Add(dgcs2)
dgts.GridColumnStyles.Add(dgcs3)
dgts.MappingName = "Pick"
Me.DataGrid1.TableStyles.Add(dgts)
Me.DataGrid1.Refresh()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim row As DataRow
row = dtPick.NewRow()
row(0) = "TEST STRING 1"
row(1) = "TEST STRING 2"
row(2) = "TEST STRING 3"
dtPick.Rows.Add(row)
End Sub
End Class
Hope this helps
Harold Hoffman
-
Sep 25th, 2002, 12:49 PM
#3
New Member
Add a table style in design mode by selecting datagrid's tablestyle property. when u have added new table style goto to coulumn style collection on same sheet and add required no of columns.
in run time u have only to map your table style to your datatable object and columns to tables column
for example
DataGrid.TableStyles(0).MappingName = "Pick"
DataGrid.TableStyles(0).GridColumnStyles(0).MappingName = "Code"
DataGrid.TableStyles(0).GridColumnStyles(1).MappingName = "Description"
this will help u to set width and other column attributes at design time
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|