We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


Manipulating data from datagrid, datatables, Using Current RowIndex

Medium Priority
Last Modified: 2008-03-03
I have a vb.net windows app that uses a tabcontrol have a main form and a search page in 2 tabs.  I previously set up to pass the entire contents of a search to the main form.  I now need to pass only the row selected to the main form.  My code is working fine to pass the table, rebind etc.  Is there an easy way to pass a dataset or table containing only the selected row just as I'm currently doing for multiple rows.  

Currently, I am doing something like:

        Dim dtSearch As DataTable = dsSrchGrid.Tables(0).Copy

I would like to have the one selected row in dsSrchGrid.Tables(0)

Watch Question


There might be better ways depending on what controls you are using however....

You can get a collection of rows using the SELECT method; for example if your search table had fields ID and Description then you could try

dim dtSearch as New DataTable
dim foundRows() as datarow
dim sql as string
dim dr as datarow
dim i as integer

sql = "ID = 10"    ' searching for an ID of 10
foundrows = dsSrchGrid.Tables(0).Select(sql)
for i = 0 to foundrows.Length - 1
    dr = dtSearch.NewRow
    dr("ID") = foundrows(i).Item("ID")
    dr("Description" = foundrows(i).Item("Description")


Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


I know I've asked you this before

More or less as an aside, can I ask why you are filling two datasets (or even two datatables in the same dataset) with records from the same database source?  Might it not be easier to have a dataview that you filter (or not) depending on whether you want to show "all" the data or the "search" data, and bind your controls to that.  You could then avoid the necessity for copying tables.

and you said

In regard to switching to a dataview.  I sure would like to make this work as oppose to switching at this point but If i had to, so be it.

but, it seems to me, you really are making your app (and coding it) much more complicated than it needs to be.

So far as I can see from this and other postings all your various datasets/datatables are just sub-sets of each other.  If you had one dataset (and its datatable/s) somewhere which was accessible to all your form/s (e.g. in a data Module), you could have different dataviews based on a single table to which you could bind different forms' controls.  If you want one form to show all the data then the dataview for that form need not be filtered at all.  If you wanted another form filtered to show only records between IDs 10 and 100, you could filter the dataview for that form with "ID > 10 AND ID < 100".  If you wanted yet another form to show just a single record, you could filter the dataview for that with "ID = 85".  And so on, with more or less infinite variety: eg "Surname = 'Smith'".  An advantage of this approach is when, on any of these forms, a user edits a record, that edit gets back to the single table and so is reflected straightaway in all the other forms.  And, as I say, I think (after an initial learning curve - with which we would help, and it's not rocket science anyway) you'd find things a lot easier.

But it's your app ;-)




Yes you showed me the way to swap the datatable from search tab to that which was being bound to controls on another tab.  That worked beautifully.  The problem now is they only want to send the selected row to the controls on the tab. They have also made some other changes regarding what can be in the various datasets but let me skip that.  First The grid dataset and the main form dataset have identical columns in them.  One is not guaranteed to be a subset of the other and that is the reason I do not see a filter working.  Assuming I might be on the right track, what I think would be easiest to do is to create a data table containing the one selected row and then use the logic you provided earlier that worked so well.   Unless you clearly think it ill advised,  I would certainly not turn down an example of selecting the entire row and adding it to a datatable which I would use to replace the one bound to the controls on main form.?
and it is nice to hear from you again.

OK.  Here it is in outline

        'make an empty copy of the source table
        Dim dtSearch As DataTable = dsSrchGrid.Tables(0).Clone
        'get the single row from the source table
        Dim dr As DataRow = dsSrchGrid.Tables(0).Rows(<selectedRowNumber>)
        'put it in the new table
        'empty the dataset
        'put the new table in the dataset



I forgot to answer your question.  I could not see how to make the filter work.  Program is building a dynamic query based on several selections they make on the screen.  It is actually a stored procedure.  It looked to me like the filtering would be too limiting so I created a search tab and did select exactly the same elements for the grid that are used on the form.  The original plan called for the passing of the the entire search result to that which is bound to the form.  With youur earlier help, it worked great.  Now they feel they need to do them one at a time on this screen, hence I want to change it.  Unless it is just to slow and does not seem that way now, I would like to use the same idea but with only one row.  Next week, I may be asked to put it back to original way.  I believe I am learning but have more to learn before I could debate with you about which way is most efficient.  Selecting the row from the grid or datatable behind the grid and creating a datatable exactly like I was doing with entire search result would be path of least resistance for me.      

So long as you're happy.  By the way, but I expect you noticed, this line


should have read


Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.