PhilipJohnIV

Hi all,

I was wondering if there was an easy way to navigate through dataset rows (this is not a DataGrid). I've used BindingManagerBase before. I am using a stored proc to retrieve data form the database. Here is my sample code...

Code Snippet

Form Load...etc.

'Connects to the database, runs the s/p and Fills the DataSet

MyDataSet = SqlHelper.ExecuteDataset(SQLConnection, CommandType.StoredProcedure, "sp_Select_EmpData_Form")

'Binds my field to the form object

txtpEmpID.Text = MyDataSet.Tables(0).Rows(0)("Emp_ID").ToString

I want to use a button to iterate through the dataset. I know I have to increment the Rows(0) value but I dont know how to do it efficiently.

Thanks!

Philip




Re: .NET Framework Data Access and Storage Navigating through a DataSet

Derek Smyth

Hi

There are two ways, one is to create a binding source for your table and use the whole data binding approach. To make the way your doing to work replace the indexer at Rows(0) with a variable, and in your next and previous buttons increment the indexer (check that it's not less that 0 or greater than the number of rows in the table) and then redisplay your data.

int indexer = 0;

OnNextButton_Click

//do check to make sure indexer isn't greater than the number of rows0

indexer += 1;

txtpEmpID.Text = MyDataSet.Tables(0).Rows(indexer)("Emp_ID").ToString

OnPreviousButton_Click

//do check to make sure indexer isn't greater than the number of rows0

indexer -= 1;

txtpEmpID.Text = MyDataSet.Tables(0).Rows(indexer)("Emp_ID").ToString

Thats the sort of idea.






Re: .NET Framework Data Access and Storage Navigating through a DataSet

PhilipJohnIV

Thanks for the reply. works great. Is there a way that I can only write this once...

Code Snippet
txtpEmpID.Test=MyDataSet.Tables(0).Rows(indexer)("Emp_ID").ToString

and reference it and give it the new indexer value. I have a lot of fields in this form and I dont want to have to retype them.

Thanks for your help!






Re: .NET Framework Data Access and Storage Navigating through a DataSet

Derek Smyth

Hi,

You could create a method that updates all your controls and call that method in all the places you need it. This is where databinding would have been good to use, would make things easier for you. If your interested in using data binding, which is much better than it was in VB6, then have a look at the System.Windows.Forms.BindingSource object. You'd attach the Table to the binding source, and then bind each control to the tables column.

If you need an example I'll have a look and see if I've got any examples.