By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,362 Members | 1,311 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,362 IT Pros & Developers. It's quick & easy.

number of Columns in DataGrid (not DataSource!)

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
13 Replies

P: n/a
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
Nov 21 '05 #2

P: n/a
"Cor Ligthert [MVP]" <no************@planet.nl> schrieb:
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

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #3

P: n/a
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
Nov 21 '05 #4

P: n/a
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?

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:Ou**************@TK2MSFTNGP12.phx.gbl...
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

Nov 21 '05 #5

P: n/a
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..

Nov 21 '05 #6

P: n/a
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?

"dincerozturan" <di************@bizitek.com.tr> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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..

Nov 21 '05 #7

P: n/a
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
Nov 21 '05 #8

P: n/a
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
Nov 21 '05 #9

P: n/a
How do you mean "You can do that withouth mapping?"?
Just do a MyDataGrid.DataSource = MyDataView etc...
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:OQ**************@TK2MSFTNGP12.phx.gbl...
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

Nov 21 '05 #10

P: n/a
> 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.C olums.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
Nov 21 '05 #11

P: n/a
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.

Nov 21 '05 #12

P: n/a
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...

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:e1**************@TK2MSFTNGP10.phx.gbl...
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.C olums.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

Nov 21 '05 #13

P: n/a
dim col as integer
col = Datagrid1.visiblecolumncount

I hope this is too little, way too late

"DraguVaso" wrote:
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

Nov 21 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.