number of Columns in DataGrid (not DataSource!)

D

DraguVaso

Hi,

I need a function that gives me the number of Columns shown in a DataGrid.
So I don't need to know the number of columns shown in tha DataSource,
because this number can be completely something else than the number of
columns defined in the currently active TableStyle!

I currently use DataGrid.TableStyles(0).GridColumnStyles.Count, but i know
this won't work when using more than one TableStyle, or having a TableStyle
with another mappingname...

Has anybody any idea?

Maybe a combination of these 2 functions that search first which type of
DataSource I have, if it has a table, and if there is a TableStyle with that
name etc? But it doesn't seem really logical to me, and I really wonder if
there isn't any method that gets it directly from the DataGrid?

Thanks a lot in advance,

Pieter

private void PrintCurrentListName(DataGrid myDataGrid){
CurrencyManager myCM = (CurrencyManager)
BindingContext[myDataGrid.DataSource, myDataGrid.DataMember];
IList myList = myCM.List;
ITypedList thisList = (ITypedList) myList;
Console.WriteLine(thisList.GetListName(null));
}

Private Function GetColumnCount() As Integer
Dim intCount As Integer
If TypeOf Me.DataSource Is DataTable Then
intCount = DirectCast(Me.DataSource, DataTable).Columns.Count
ElseIf TypeOf Me.DataSource Is DataView Then
intCount = DirectCast(Me.DataSource,
DataView).Table.Columns.Count
ElseIf TypeOf Me.DataSource Is ArrayList Then
intCount = DirectCast(Me.DataSource, ArrayList).Count '????
ElseIf TypeOf Me.DataSource Is Collection Then
intCount = DirectCast(Me.DataSource, Collection).Count '????
ElseIf TypeOf Me.DataSource Is DataSet Then
intCount = DirectCast(Me.DataSource,
DataSet).Tables(0).Columns.Count
Else
intCount = 0
End If
Return intCount
End Function
 
C

Cor Ligthert [MVP]

Pieter,

Assuming that you not showing column that you have hided, are styles
collection so this should work for your first collection of
GridColumnStyles.

Dim i As Integer = DataGrid1.TableStyles(0).GridColumnStyles.Count()

I hope this helps,

Cor
 
H

Herfried K. Wagner [MVP]

Cor Ligthert said:
Assuming that you not showing column that you have hided, are styles
collection so this should work for your first collection of
GridColumnStyles.

Dim i As Integer = DataGrid1.TableStyles(0).GridColumnStyles.Count()

Isn't that what the OP is currently using?

| I currently use DataGrid.TableStyles(0).GridColumnStyles.Count
 
C

Cor Ligthert [MVP]

Herfried,

You are right, however than it is in my opinion just changing the index from
the tablestyle to the one that is active.

It are just collection you know (for Pieter).

Cor
 
D

DraguVaso

Well, I do know that it's a collection etc. But the problem is: how can
anybody know on any given moment which TableStyle is active, and if there is
indeed a TableStyle used or not?
 
D

dincerozturan

You can compare the current Name of the Datatable bound to the Grid and
the MappingName property of each TableStyle where exists in the Grid's
tablestyles collection in a foreach loop. Then you can find the active
tablestyle.

Good luck..
 
D

DraguVaso

thanks, but:
- what in case I'm not using a DataTable as Datasource but a DataView? Or a
DataSet? a List? ... ?
- What if I don't have a TableStyle?
 
C

Cor Ligthert [MVP]

Pieter
Well, I do know that it's a collection etc. But the problem is: how can
anybody know on any given moment which TableStyle is active, and if there
is
indeed a TableStyle used or not?
See the answer from dincerozturan

Cor
 
C

Cor Ligthert [MVP]

Pieter,

- what in case I'm not using a DataTable as Datasource but a DataView? Or a
DataSet? a List? ... ?
You can do that withouth mapping?
Can you give us an example?
- What if I don't have a TableStyle?
Use than the count of the columns of the datatable minus those where you
have set the MappingType.Hidden

Cor
 
D

DraguVaso

How do you mean "You can do that withouth mapping?"?
Just do a MyDataGrid.DataSource = MyDataView etc...
 
C

Cor Ligthert [MVP]

How do you mean "You can do that withouth mapping?"?
Just do a MyDataGrid.DataSource = MyDataView etc...
dim i as integer =
DirectCast(MyDataGrid.DataSource,DataView).Table.Colums.Count

and than my note about mapping.hidden in the previous message

However, I thought that in the subject of your message was (*not
DataSource!*)

Cor
 
D

dincerozturan

These are all cases and as I see in your code above you are controlling
those cases. For each case you have to handle the problem in a
different way. My solution was for the case that you have a datatable
and tablestyles associated with it. And also the solution is feasible
for dataview; so that you can access the datatable that the datadaview
is associated with. If you do not use tablestyles and have an array as
a datasource.
 
D

DraguVaso

Well indeed: I don't need to know the number of columns in my dataSource,
but those in my DataGrid. that number can be the same, but isn't alway:
depending on the fact if there is a TableStyle or not etc...
 
G

Guest

dim col as integer
col = Datagrid1.visiblecolumncount

I hope this is too little, way too late
 

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