Adding ColumnStyles to DataGrid

  • Thread starter Thread starter Nathan
  • Start date Start date
N

Nathan

Hi,

I'm trying to add column styles to a datagrid--first time I've done this in
code. I have a datagrid and have added a new table style to it in the
designer. I've set the tablestyle's mapping name to the name of my dataset
table. Then this is my code for adding columns:

With dgdInventory.TableStyles.Item(0).GridColumnStyles
Dim colIsAP As New DataGridBoolColumn()
With colIsAP
.Alignment = HorizontalAlignment.Center
.HeaderText = "A/P"
.Width = 40
.MappingName = "IsActivityPac"
.ReadOnly = True
.NullText = ""
End With
.Add(colIsAP)

Dim colItemNumber As New DataGridTextBoxColumn()
With colItemNumber
.Alignment = HorizontalAlignment.Left
.HeaderText = "Item #"
.Width = 75
.MappingName = "ItemNumber"
.NullText = ""
End With
.Add(colItemNumber)

Dim colItemDescription As New DataGridTextBoxColumn()
With colItemNumber
.Alignment = HorizontalAlignment.Left
.HeaderText = "Description"
.Width = 200
.MappingName = "ItemDescription"
.NullText = ""
End With
.Add(colItemDescription)

End With

.... and so on: I have seven columns that are set up this way. When I load
the form, only the first and last columns show up, no matter what order I
add them. I can successfully do this through the designer, but is something
wrong with my code?

Thanks,
Nathan
 
Nathan,

Assuming that you have added it because you see the first and last columns,
the first thing I think about is with this always the case sensivity of the
mappingnames.

Are they in the correct case?

Cor
 
Thanks, Cor. Yes, the mapping names are in the correct case.

If I comment out the last column addition, then I see the next-to-last
column instead. Whatever columns I have listed first and last are the ones
to show up in the grid.
 
Nathan,

You are sure that your datagrid is not just to small to show them all?

Cor
 
The datagrid is 600 pixels wide. The two columns that show do not show with
the width assigned to them -- they are the default width. Lots of blank
space.
 
Nathan,

You don't know how much I don't like that "With", I tested your sample and
saw it only when I had deleted the "With" and replaced the code as you told
you did.

Have a look at it in your code (my test sample is below, however not
needed)..

"With colItemNumber"

:-)

I hope this helps?

Cor

\\\
Dim dt As New DataTable("Nathan")
dt.Columns.Add("IsActivityPac", GetType(System.Boolean))
dt.Columns.Add("ItemNumber", GetType(System.Int32))
dt.Columns.Add("ItemDescription", GetType(System.String))
For i As Integer = 0 To 9
dt.Rows.Add(dt.NewRow)
Dim dr As DataRow = dt.Rows(i)
dr(0) = True
dr(1) = i
dr(2) = ChrW(i + 65)
Next
dgdInventory.DataSource = dt
Dim dgt As New DataGridTableStyle
dgt.MappingName = "Nathan"

Dim colIsAP As New DataGridBoolColumn
colIsAP.Alignment = HorizontalAlignment.Center
colIsAP.HeaderText = "A/P"
colIsAP.Width = 40
colIsAP.MappingName = "IsActivityPac"
colIsAP.ReadOnly = True
colIsAP.NullText = ""
dgt.GridColumnStyles.Add(colIsAP)

Dim colItemNumber As New DataGridTextBoxColumn
colItemNumber.Alignment = HorizontalAlignment.Left
colItemNumber.HeaderText = "Item #"
colItemNumber.Width = 75
colItemNumber.MappingName = "ItemNumber"
colItemNumber.NullText = ""
dgt.GridColumnStyles.Add(colItemNumber)

Dim colItemDescription As New DataGridTextBoxColumn
colItemDescription.Alignment = HorizontalAlignment.Left
colItemDescription.HeaderText = "Description"
colItemDescription.Width = 200
colItemDescription.MappingName = "ItemDescription"
colItemDescription.NullText = ""
dgt.GridColumnStyles.Add(colItemDescription)
dgdInventory.TableStyles.Add(dgt)
///
 
Urgh! I found the error. It was not because I was using "With", but
because just copied and pasted the With blocks, but forgot to change the
With colName each time. Thanks for your help, or I may not have seen it.
 
Nathan,

I did not say that it was because of with, however using the with gives more
change on this kind of errors, and therefore I don't like it.

You copied it and did not see the error, however I gave you the change to
see that yourself.

:-)

Cor
 

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

Back
Top