Help creating columns for a datagrid!

J

Jeff

I am stuck on trying to generate two columns headers for a datagrid on
form load. I can use a datatable as the datasource and get the results
I want, but I want to set different column widths and don't see a way
to do this through the datagrid properties. I am not using a database
connection, I just want to set column headers on the datagrid. I am
somewhat new to vb.net. Here is the code I am using. This code works
when the tableStyle.MappingName = an actual table in a database, but
not a datatable in a dataset without a database.

'Create DataGridTableStyle
Dim tableStyle As New DataGridTableStyle
tableStyle.MappingName = Me.DataSet1.Tables(0).TableName

'Create DataGridColumnStyle for each column
Dim column As New DataGridTextBoxColumn
column.MappingName = "Names"
column.MappingName = Me.DataSet1.Tables(0).Columns(0).ColumnName
column.HeaderText = "Names" column.Width = 100
tableStyle.GridColumnStyles.Add(column)

column = New DataGridTextBoxColumn
column.MappingName = "Description"
column.MappingName = Me.DataSet1.Tables(0).Columns(1).ColumnName
column.HeaderText = "Description"
column.Width = 250
tableStyle.GridColumnStyles.Add(column)

'Add tableStyle to datagrid
Me.DataGrid1.TableStyles.Clear()
Me.DataGrid1.TableStyles.Add(tableStyle)

What I am trying to do is create a datagrid that I can populate with
Active Directory users and descriptions. Maybe I am not going about it
the right way with a datagrid. If this will not work can someone give
me advice on what I should be using?

Thanks,
Jeff
 
C

Chris

Jeff said:
I am stuck on trying to generate two columns headers for a datagrid on
form load. I can use a datatable as the datasource and get the results
I want, but I want to set different column widths and don't see a way
to do this through the datagrid properties. I am not using a database
connection, I just want to set column headers on the datagrid. I am
somewhat new to vb.net. Here is the code I am using. This code works
when the tableStyle.MappingName = an actual table in a database, but
not a datatable in a dataset without a database.

'Create DataGridTableStyle
Dim tableStyle As New DataGridTableStyle
tableStyle.MappingName = Me.DataSet1.Tables(0).TableName

'Create DataGridColumnStyle for each column
Dim column As New DataGridTextBoxColumn
column.MappingName = "Names"
column.MappingName = Me.DataSet1.Tables(0).Columns(0).ColumnName
column.HeaderText = "Names" column.Width = 100
tableStyle.GridColumnStyles.Add(column)

column = New DataGridTextBoxColumn
column.MappingName = "Description"
column.MappingName = Me.DataSet1.Tables(0).Columns(1).ColumnName
column.HeaderText = "Description"
column.Width = 250
tableStyle.GridColumnStyles.Add(column)

'Add tableStyle to datagrid
Me.DataGrid1.TableStyles.Clear()
Me.DataGrid1.TableStyles.Add(tableStyle)

What I am trying to do is create a datagrid that I can populate with
Active Directory users and descriptions. Maybe I am not going about it
the right way with a datagrid. If this will not work can someone give
me advice on what I should be using?

Thanks,
Jeff

The code you show looks good. It's probably a problem with how you are
creating the datatable.

Dim T as new Datatable("MyTableName")
T.Columns.Add(...)
T.Columns.Add(...)

'Create DataGridTableStyle
Dim tableStyle As New DataGridTableStyle
tableStyle.MappingName = "MyTableName"
......

There is no reason you have to add it to a dataset in this example. You
may want to for other reasons. But naming the table as I show here is
the key.

Chris
 
J

Jeff

Thanks Chris,

Can you elaborate a little on the T.Columns.add(...)? I looked at MSDN
and came up with the following for the datatable: This still doesn't
work for me. I feel like I am so close but missing something!

Dim T As New DataTable("MyDataTable")
Dim myDataColumn As DataColumn

myDataColumn = New DataColumn
myDataColumn.DataType = System.Type.GetType("System.String")
myDataColumn.ColumnName = "Names"
myDataColumn.ReadOnly = True
myDataColumn.Unique = True
T.Columns.Add(myDataColumn)

myDataColumn = New DataColumn
myDataColumn.DataType = System.Type.GetType("System.String")
myDataColumn.ColumnName = "Description"
myDataColumn.ReadOnly = True
myDataColumn.Unique = True
T.Columns.Add(myDataColumn)
 
C

Chris

Jeff said:
Thanks Chris,

Can you elaborate a little on the T.Columns.add(...)? I looked at MSDN
and came up with the following for the datatable: This still doesn't
work for me. I feel like I am so close but missing something!

Dim T As New DataTable("MyDataTable")
Dim myDataColumn As DataColumn

myDataColumn = New DataColumn
myDataColumn.DataType = System.Type.GetType("System.String")
myDataColumn.ColumnName = "Names"
myDataColumn.ReadOnly = True
myDataColumn.Unique = True
T.Columns.Add(myDataColumn)

myDataColumn = New DataColumn
myDataColumn.DataType = System.Type.GetType("System.String")
myDataColumn.ColumnName = "Description"
myDataColumn.ReadOnly = True
myDataColumn.Unique = True
T.Columns.Add(myDataColumn)

That looks correct. So you add data to the table, bind it to the
datagrid and it isn't working? Make sure after adding the tablestyle
you something like:

DataGrid.DataSource = T

This binds the data to the datagrid. If your still having issues with
it, post more code.

Chris
 
J

Jeff

I found another posting with the code I needed. Thanks for you help!
It creates two columns and some rows at Form_Load.

Dim myDataSet As DataSet

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

MakeParentTable()
Me.DataGrid1.DataSource = myDataSet.Tables(0)

Dim tableStyle As New DataGridTableStyle
tableStyle.MappingName = "AD"
Dim column As New DataGridTextBoxColumn
column.MappingName = "UserNames"
column.HeaderText = "Names"
column.Width = 100
tableStyle.GridColumnStyles.Add(column)


column = New DataGridTextBoxColumn
column.MappingName = "UserDescription"
column.HeaderText = "Description"
column.Width = 300
tableStyle.GridColumnStyles.Add(column)
tableStyle.MappingName = "MyTable"

Me.DataGrid1.TableStyles.Add(tableStyle)
End Sub


Private Sub MakeParentTable()

' Create a new DataTable.
Dim myDataTable As DataTable = New DataTable("MyTable")
' Declare variables for DataColumn and DataRow objects.
Dim myDataColumn As DataColumn
Dim myDataRow As DataRow


' Create new DataColumn, set DataType, ColumnName and add to
' DataTable.
myDataColumn = New DataColumn
myDataColumn.DataType = System.Type.GetType("System.Int32")
myDataColumn.ColumnName = "UserNames"
myDataColumn.ReadOnly = True
myDataColumn.Unique = True
' Add the Column to the DataColumnCollection.
myDataTable.Columns.Add(myDataColumn)


' Create second column.
myDataColumn = New DataColumn
myDataColumn.DataType = System.Type.GetType("System.String")
myDataColumn.ColumnName = "UserDescription"
myDataColumn.AutoIncrement = False
myDataColumn.Caption = "ParentItem"
myDataColumn.ReadOnly = False
myDataColumn.Unique = False
' Add the column to the table.
myDataTable.Columns.Add(myDataColumn)


' Instantiate the DataSet variable.
myDataSet = New DataSet
' Add the new DataTable to the DataSet.
myDataSet.Tables.Add(myDataTable)


' Create three new DataRow objects and add them to the
' DataTable
Dim i As Integer
For i = 0 To 2
myDataRow = myDataTable.NewRow()
myDataRow("UserNames") = i
myDataRow("UserDescription") = "Item " + i.ToString()
myDataTable.Rows.Add(myDataRow)
Next i

End Sub


Here is the link:
http://groups.google.com/group/micr...atacolumn+width&rnum=3&hl=en#bb48f0864fa6b1d4
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top