Column Count

  • Thread starter Thread starter Doug Bell
  • Start date Start date
D

Doug Bell

Hi,

I need to create a function that will return the number of columns in a
passed reference to a DataView.

I can find the number of columns in its underlying Data table but have not
been able to find a way of determining the column count in the DataView.

Thanks for any assistance.

Doug
 
I didn't think the two could be different. How do you lose columns when you
apply the table to the dataview?

Chris
 
Chris,
Sorry, I didn't really explain that very well.

What I am trying to do is determine the number of columns displayed in a
datagrid.
The datagrid has a grid style applied to it.

Doug
 
Well This is strange and I don't know what's going on with it, but

Grab a hold of the TableStyles object off your datagrid after the gridstyle
is added. Then look at the Gridcolumnstyles. This holds all your columns,
but for some reason there isn't a count method on it. However it is really
there and I've used it. So the code below will give you the count, you can
also itterate through all the columns there. Anyone say why the Count
method isn't displayed in Intellisense?

Hope it helps

MessageBox.Show(DGrid.TableStyles(0).GridColumnStyles.Count())

Chris
 
Thanks Chris,

That is great! And I was a little surprised there was not a count method but
did not think to try it without it being selectable from the intelisense.
Now I can build a re-usable function to size the last column to the
remaining grid width irespective of the selected grid style.

Doug
 
That's an interesting idea. How are you figuring out how much space is left
on the grid for that column to fill?

Chris
 
An alternate solution is to resize all the column widths to make them larger
proportionally to their current width and the remaining blank space to fill
 
Chris,
Sorry for delay, I had a guy come to fix my garage door.
I am still finding my way with Dot Net but what I am doing is:

1. get number of columns intNumCols
2. get desired width (intDesiredWidth) and Uused width (intUsedWidth)

Dim intDesiredWidth as Integer = MyGrid.Width
For each ctrl as Control In MyGrid.Controls
If TypeOf ctrl Is VScrollBar Then
If ctrl.Visible Then
intDesiredWidth = intDesiredWidth - ctrl.Width
EndIf
Exit For
EndIf
Next

intDesiredWidth=intDesiredWidth +39 'Fudge determined by trial & error 39
worked for me

Dim dv1 As DataView = CType(MyGrid.DataSource, DataView)
Dim stStyle As String = dv1.Table.TableName.ToString
Dim i as Integer = 0
Do While i <intNumCols - 1
intUsedWidth = intUsedWidth +
MyGrid.TableStyles(stStyle).GridColumnStyles(i).Width
i = i + 1
Loop
intDesiredWidth = intDesiredWidth - intUsedWidth

If intDesiredWidth >0 Then
Do Until i = 0
If MyGrid.TableStyles(stStyle).GridColumnStyle(i).Width > 0 Then
'This is the last Visible Column
MyGrid.TableStyles(stStyle).GridColumnStyle(i).Width =
intDesiredWidth
ExitDo
EndIf
i = i - 1
Loop
EndIf


Doug
 
Dough,

When you still are looking for your original question
\\\
Dim x As Integer = dv.Table.Columns.Count
Dim y As String = dv.Table.Columns(0).ColumnName
'and any other information about a column
///

I hope this helps?

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