how to Count the filled rows in the DataGrid

Hi,

Please can some one help me to know "How many rows are the datagrid filled with?"

Thanks a lot

[298 byte] By [ams_ashraf] at [2007-12-28]
# 1

Use the .rowcount property of the datagrid

or

the .count property of the datasource of the datagrid

bnaveke at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2
Use the currencymanager to get the row count

dim cm as currencymanager = ctype(me.bindingcontext(datagrid1.datasource),currencymanager)
dim intRowCount as integer = cm.count

KenTucker at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3

Thanks to your reply

but there is no "Rowcount" property for the datagrid

and also there is no "Count" property for the DataSource

PS. I'm using vb.net 2003

thanks again

ams_ashraf at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4

Dear Ken Tucker,

Thanks for your reply

I tried it but it gives me only 1 row count in all cases

I have around 400 rows

Is there some more code I must write it ?!!

Thank U too much

ams_ashraf at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5
.rowcount is for the datagridview which is v2.0 only. there's .visiblerowcount, but that's not what you want. the table in your datagrid has a rows.count property.
weirdbeardmt at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 6

I get 77 with this example.

Dim conn As SqlConnection
Dim strConn As String
Dim da As SqlDataAdapter
Dim ds As New DataSet

strConn = "Server = .; Database = NorthWind; Integrated Security = SSPI;"


conn = New SqlConnection(strConn)
da = New SqlDataAdapter("Select * From Products", conn)
da.Fill(ds, "Products")
DataGrid1.DataSource = ds.Tables("Products")
Dim cm As CurrencyManager
cm = CType(Me.BindingContext(DataGrid1.DataSource), CurrencyManager)
Dim intRows As Integer = cm.Count
Me.Text = String.Format("Number of rows {0}", intRows)

KenTucker at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 7
Ken, couldn't you just do

dim int rows as integer = ds.tables("Products").rows.count

save having to get a new CM.

weirdbeardmt at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 8
Yes it would work. I choose the currency manager because it comes up with the number of rows if you are bound to an arraylist, datatable, dataset, or dataview.
KenTucker at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 9

Thanks every body

this code is working now

Dim MyRows As Integer = MyDataGrid.DataSource.tables("MyTable").rows.count

ams_ashraf at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 10

public static int dataTableRowCount(DataTable dt)

{

int dtRowCount = 0;

foreach(DataRow myRow in dt.Rows)

if (myRow.RowState != DataRowState.Deleted)

dtRowCount ++;

return dtRowCount;

}

CarlosValenzuela at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...