Count of Records

ckeezer

Well-known member
Joined
Jan 16, 2006
Messages
100
Programming Experience
1-3
I have label that displays the total number of records in my dataset. Now I want that count to change when I search for a specific record. My search is done by with the value from my txtLName textbox. How do I update my label with only the number of records returned in the search?

Here is the code that I am using on Form1_Load:
Me.lblRecords.Text = Me.SmartHouseDS1.Customers.Rows.Count
 
How are you doing the search? if you are using the Select method then it returns an array of datarow objects, so you could do this..

VB.NET:
Me.LblRecords.Text = Dr(0).getupperbound.tostring


Where dr is the one dimensional array that get returned from the select method.
 
Here is the code for my search:
VB.NET:
[SIZE=2]SmartHouseDS1.Orders.Clear()
CustomersTableAdapter.FillByLastName(SmartHouseDS1.Customers, txtLName.Text)
OrdersTableAdapter.ClearBeforeFill = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] ro [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] SmartHouseDS.CustomersRow [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] SmartHouseDS1.Customers.Rows
OrdersTableAdapter.FillByCustomerID(SmartHouseDS1.Orders, ro.CustomerID)
[/SIZE]
 
I'll admit that i don't play with tableadapters,i don't like them all that much. However if it's anything like a dataadapter.fill which i'm sure it is it return an integer pertaining to how many records were retrieved..

VB.NET:
Dim i as integer = 0
i = OrdersTableAdapter.FillByCustomerID(SmartHouseDS1.Orders, ro.CustomerID)
 
me.lblrecords.text = i.tostring
 
You need to be more specific than "the number of records returned in the search"

For eaxmple your search consists of:

Fill the customer info
for every matching customer, load the order details

what would you like to count? The number of customer rows? The number of order rows? the number of order rows for this customer? The total number of all rows (e.g. 100 cust each with 10 orders = 1000 rows loaded)?

the MyDataSet.MyDataTable.Rows.Count property can be used in all cases; just decide what you want to count..
 
The search is done on the last name. So if I have a total of 100 records, when I search for smith, I want the count to change to only the number of records containing smith in them.
 
After you have called:

CustomersTableAdapter.FillByLastName(SmartHouseDS1.Customers, txtLName.Text)

If the datatable was cleared by this call (ClearBeforeFill = true) or cleared first (ClearBeforeFill = false) then simply this will give you the number of search results:

If SmartHouseDS1.Customers IsNot Nothing Then lblRecords.Text = SmartHouseDS1.Customers.Rows.Count
 
how would you retrieve current record? Say if I have a setup like this:
Record 2 of 100

The customers.rows.count would give me the 100, but how do you get it to only count the current record. I know that in a dg you use rows.index, but a ds does not offer that.
 
You need to understand that a datatable is just a store of data. Asking me what the current record is is like saying:

"If I declare an array and fill it with values, which value is the current value?"

Can you see how it's a nonsensical question?


You may have a BindingNavigator and BindingSource looking at your data store, and they will be able to tell you which row they are currently looking at, but its not a feature of the datatable itself. If you need me to clarify, do ask! :)
 
I follow you on that. Lets see if I can clearify for you:
I have a bindingsource "CustomersBindingSource", which is looking at my datatable "CustomersDataTable" for its information. When I open my form I want the bindingsource to tell me how many records it has (this part is done) and which record it is currently looking at in the table.
 
Press F2 and type BindingSource
Search

and pick the relevant result that is the actual BindingSource

(if i searched for Exception i would get many results, ArgumentNullException IndexOutOfRangeException IOException OperationCanceledException etc etc.. Pick the right one for you, probably System.Data.Bindingsource)


Have a read of the properties it exposes:

.Current -> a datarowview depicting the current row.
.List.Count -> the number of items in this BS list

the list count may vary: if you have 2 tables that are related, the child table has 100 rows, and the parent table bindingsource is showing a parent that has only 5 children, then ChildBindingSource.List.Count gives 5, but ChildDataTable.Rows.Count returns 100

In non-filtered, non-related scenarios the coutns are the same
 
Back
Top