DataGrid has me tied up once again!

H

Hexman

I was going along fine in my test to see how to manipulate the
DataGrid (dgTrans) and its columns. I have a single form with one
DataGrid (dgTrans) on it. The code I have in my form load event is:

Dim dt As New DataTable("WorkOrders")

Dim colSelect As New DataColumn
Dim colStatus As New DataColumn
Dim colWO As New DataColumn
Dim colEmployee As New DataColumn

dt.Columns.Add("colSel", GetType(System.Boolean))
dt.Columns.Add(colStatus)
dt.Columns.Add(colWO)
dt.Columns.Add(colEmployee)
dgTrans.DataSource = dt

Dim gcolSelect As New DataGridBoolColumn
With gcolSelect
.Width = 15
.HeaderText = "Select"
.MappingName = "colSel"
End With
Dim gcolStatus As New DataGridTextBoxColumn
With gcolStatus
.Width = 15
.HeaderText = "Status"
.MappingName = "colStatus"
End With
Dim gcolWO As New DataGridTextBoxColumn
With gcolWO
.Width = 40
.HeaderText = "Work Order"
.MappingName = "colWO"
End With
Dim gcolEmployee As New DataGridTextBoxColumn
With gcolEmployee
.Width = 60
.HeaderText = "Employee"
.MappingName = "colEmployee"
End With

Dim ts As New DataGridTableStyle
ts.GridColumnStyles.Add(gcolSelect)
ts.GridColumnStyles.Add(gcolStatus)
ts.GridColumnStyles.Add(gcolWO)
ts.GridColumnStyles.Add(gcolEmployee)
dgTrans.TableStyles.Add(ts)

dt.Rows.Add(New Object() {False, "Open", "WO123123", "Hexman"})
dt.Rows.Add(New Object() {False, "Open", "WO123234", "S. Jones"})
dt.Rows.Add(New Object() {False, "QA", "WO123345", "B. Daley"})
dt.Rows.Add(New Object() {False, "Closed", "WO123456", "K. Genns"})
dt.Rows.Add(New Object() {False, "Open", "WO123567", "Superman"})
----------------------------------------------------------------------------------------------------------------------------------

As you can see I wanted to change the header text and width of the
grid coulumn. They are not set as I expected. The columns when
displayed are "colSel, Column1, Column2, Column3" instead of "Select,
Status, Work Order, Employee". Also the widths are not set.

I've got this working this far from Cor's Website, but I'm now stuck.
I thought I've tried everything. (I'm sure I haven't).

One other issue I'm seeing in the near future is how do I access (as
an example) a datagrid column width. In the above code it is changed
before the columns are added to the grid table styles. After the fact
how do you access that attribute? Something like:

dgTrans.TableStyles(ts).GridColumnStyles(gcolEmployee).width = 42

Need some help here.

Thanks,

Hexman
 
G

Guest

Hexman said:
I was going along fine in my test to see how to manipulate the
DataGrid (dgTrans) and its columns. I have a single form with one
DataGrid (dgTrans) on it. The code I have in my form load event is:

Dim dt As New DataTable("WorkOrders")

Dim colSelect As New DataColumn
Dim colStatus As New DataColumn
Dim colWO As New DataColumn
Dim colEmployee As New DataColumn

dt.Columns.Add("colSel", GetType(System.Boolean))
dt.Columns.Add(colStatus)
dt.Columns.Add(colWO)
dt.Columns.Add(colEmployee)
dgTrans.DataSource = dt

Dim gcolSelect As New DataGridBoolColumn
With gcolSelect
.Width = 15
.HeaderText = "Select"
.MappingName = "colSel"
End With
Dim gcolStatus As New DataGridTextBoxColumn
With gcolStatus
.Width = 15
.HeaderText = "Status"
.MappingName = "colStatus"
End With
Dim gcolWO As New DataGridTextBoxColumn
With gcolWO
.Width = 40
.HeaderText = "Work Order"
.MappingName = "colWO"
End With
Dim gcolEmployee As New DataGridTextBoxColumn
With gcolEmployee
.Width = 60
.HeaderText = "Employee"
.MappingName = "colEmployee"
End With

Dim ts As New DataGridTableStyle
ts.GridColumnStyles.Add(gcolSelect)
ts.GridColumnStyles.Add(gcolStatus)
ts.GridColumnStyles.Add(gcolWO)
ts.GridColumnStyles.Add(gcolEmployee)
dgTrans.TableStyles.Add(ts)

dt.Rows.Add(New Object() {False, "Open", "WO123123", "Hexman"})
dt.Rows.Add(New Object() {False, "Open", "WO123234", "S. Jones"})
dt.Rows.Add(New Object() {False, "QA", "WO123345", "B. Daley"})
dt.Rows.Add(New Object() {False, "Closed", "WO123456", "K. Genns"})
dt.Rows.Add(New Object() {False, "Open", "WO123567", "Superman"})
----------------------------------------------------------------------------------------------------------------------------------

As you can see I wanted to change the header text and width of the
grid coulumn. They are not set as I expected. The columns when
displayed are "colSel, Column1, Column2, Column3" instead of "Select,
Status, Work Order, Employee". Also the widths are not set.

I've got this working this far from Cor's Website, but I'm now stuck.
I thought I've tried everything. (I'm sure I haven't).

One other issue I'm seeing in the near future is how do I access (as
an example) a datagrid column width. In the above code it is changed
before the columns are added to the grid table styles. After the fact
how do you access that attribute? Something like:

dgTrans.TableStyles(ts).GridColumnStyles(gcolEmployee).width = 42

Need some help here.

Thanks,

Hexman

Dim ts As New DataGridTableStyle
ts.GridColumnStyles.Add(gcolSelect)
ts.GridColumnStyles.Add(gcolStatus)
ts.GridColumnStyles.Add(gcolWO)
ts.GridColumnStyles.Add(gcolEmployee)
dgTrans.TableStyles.Add(ts)

You need to add:
ts.MappingName = "WorkOrders"

You have to tell the tablestyle which datatable it is mapping. This is
so you can have several different styles for different tables.

Hope it helps
Chris
 
H

Hexman

Dim ts As New DataGridTableStyle
ts.GridColumnStyles.Add(gcolSelect)
ts.GridColumnStyles.Add(gcolStatus)
ts.GridColumnStyles.Add(gcolWO)
ts.GridColumnStyles.Add(gcolEmployee)
dgTrans.TableStyles.Add(ts)

You need to add:
ts.MappingName = "WorkOrders"

You have to tell the tablestyle which datatable it is mapping. This is
so you can have several different styles for different tables.

Hope it helps
Chris


Thank you Chris. I see I'm going to have to read much more on the
datagrid and datasources. So much to read - so little time.

Adding table style mapping name solved the problem of setting the
attributes of the column, but another issue just reared its head.
Only the first column ("Select") is visible in the grid. Any idea on
this one? (Check for "IsVisible"?)

I see the DataGrid as a most useful control when working with data
sets. I'd like to read "from the ground up" on it. Can you recommend
a book or online source?

Still would like to know how to access the attributes after they have
been established as in:

dgTrans.TableStyles(ts).GridColumnStyles(gcolEmployee).width = 42

Thanks once again,

Hexman
 
C

Chris

Hexman said:
Thank you Chris. I see I'm going to have to read much more on the
datagrid and datasources. So much to read - so little time.

Adding table style mapping name solved the problem of setting the
attributes of the column, but another issue just reared its head.
Only the first column ("Select") is visible in the grid. Any idea on
this one? (Check for "IsVisible"?)

I see the DataGrid as a most useful control when working with data
sets. I'd like to read "from the ground up" on it. Can you recommend
a book or online source?

Still would like to know how to access the attributes after they have
been established as in:

dgTrans.TableStyles(ts).GridColumnStyles(gcolEmployee).width = 42

Thanks once again,

Hexman

Sorry, I learned how to use the datagrid by trial and error... don't
know of any great online resources. As to your problem this is because
of how you created your datacolumn

Dim colSelect As New DataColumn
Dim colStatus As New DataColumn
Dim colWO As New DataColumn
Dim colEmployee As New DataColumn

dt.Columns.Add("colSel", GetType(System.Boolean))
dt.Columns.Add(colStatus)
dt.Columns.Add(colWO)
dt.Columns.Add(colEmployee)
dgTrans.DataSource = dt

First off, you create a colSelect, but never use it. I prefer using
this method to create datacolumns:

dt.Columns.Add("colSel", GetType(System.Boolean))

Now when you created this column, you gave it a name. This is how the
tablestyle knows which column to map, and why it is the only one showing
up. If you give the other 3 columns the correct names, they will show
up also.

Chris
 
H

Hexman

Sorry, I learned how to use the datagrid by trial and error... don't
know of any great online resources. As to your problem this is because
of how you created your datacolumn

Dim colSelect As New DataColumn
Dim colStatus As New DataColumn
Dim colWO As New DataColumn
Dim colEmployee As New DataColumn

dt.Columns.Add("colSel", GetType(System.Boolean))
dt.Columns.Add(colStatus)
dt.Columns.Add(colWO)
dt.Columns.Add(colEmployee)
dgTrans.DataSource = dt

First off, you create a colSelect, but never use it. I prefer using
this method to create datacolumns:

dt.Columns.Add("colSel", GetType(System.Boolean))

Now when you created this column, you gave it a name. This is how the
tablestyle knows which column to map, and why it is the only one showing
up. If you give the other 3 columns the correct names, they will show
up also.

Chris

Thanks for sharing your knowledge. Looks as if I will be learning by
trial and error also, but with the assistance of this newsgroup.

By using your suggestions, I've completed the task I needed to.

Now on to the next task.........

Thanks alot,

Hexman
 

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