Row Count in DataGrid

  • Thread starter Thread starter DraguVaso
  • Start date Start date
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
 
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
 
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
 
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
 
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
 
Hi Ken and Palo,

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

My compliments Palo

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

Pieter
 
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
 
Back
Top