Row Count in DataGrid

D

DraguVaso

Hi,

I want to know the number of Rows (Row Count) of a DataGrid, but without
knowing the DataSource.

So I have to find it directly form the DataGrid, because I can't know in
advance if the DataSource will be a DataSet or a DataView etc, so I can't
use fucntiosn liek this: myDataSet.Tables(0).Rows.Count, myArrayList.Count,
myDataView.Count

It shoulsd really be comething like myDataGrid.Rows.Count ...

Thanks a lot in advance,

Pieter
 
K

Ken Tucker [MVP]

Hi,

If TypeOf DataGrid1.DataSource Is DataTable Then

Me.Text = DirectCast(DataGrid1.DataSource, DataTable).Rows.Count

ElseIf TypeOf DataGrid1.DataSource Is DataView Then

Me.Text = DirectCast(DataGrid1.DataSource, DataView).Count

ElseIf TypeOf DataGrid1.DataSource Is ArrayList Then

Me.Text = DirectCast(DataGrid1.DataSource, ArrayList).Count

ElseIf TypeOf DataGrid1.DataSource Is Collection Then

Me.Text = DirectCast(DataGrid1.DataSource, Collection).Count

Else

Me.Text = "Unknown"

End If



Ken
 
D

DraguVaso

Thanks!!


Ken Tucker said:
Hi,

If TypeOf DataGrid1.DataSource Is DataTable Then

Me.Text = DirectCast(DataGrid1.DataSource, DataTable).Rows.Count

ElseIf TypeOf DataGrid1.DataSource Is DataView Then

Me.Text = DirectCast(DataGrid1.DataSource, DataView).Count

ElseIf TypeOf DataGrid1.DataSource Is ArrayList Then

Me.Text = DirectCast(DataGrid1.DataSource, ArrayList).Count

ElseIf TypeOf DataGrid1.DataSource Is Collection Then

Me.Text = DirectCast(DataGrid1.DataSource, Collection).Count

Else

Me.Text = "Unknown"

End If



Ken
 
P

Palo Mraz

private int GetCount(DataGrid grid)
{
BindingManagerBase mgrBase = grid.BindingContext[grid.DataSource,
grid.DataMember];
return mgrBase.Count;
}

This should work also with custom datasource (e.g. arrays, collections...).


Palo
--
http://dact.lamarvin.com/
AutoComplete component for WinForms applications. Easy to integrate, easier
to use!
http://www.vbinfozine.com/
An ordinary VB developer shares his own successes and failures
 
P

Palo Mraz

Oops, that was VB :)

Private Function GetCount(ByVal grid As DataGrid) As Integer
Dim mgrBase As BindingManagerBase = grid.BindingContext(grid.DataSource,
grid.DataMember);
Return mgrBase.Count;
End Function


--
http://dact.lamarvin.com/
AutoComplete component for WinForms applications. Easy to integrate, easier
to use!
http://www.vbinfozine.com/
An ordinary VB developer shares his own successes and failures
 
C

Cor

Hi Ken and Palo,

I did found Ken's idea nice, but this one is real nice.

My compliments Palo

Cor
 
D

DraguVaso

Great! Thanks!
Do you also knwo something like this for the number of Columns???

Pieter
 
K

Ken Tucker [MVP]

Hi,

It is a much better idea.

Ken
------------
Cor said:
Hi Ken and Palo,

I did found Ken's idea nice, but this one is real nice.

My compliments Palo

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

Top