Manipulating data from datagrid, datatables, Using Current RowIndex

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
         DsRetLog11.Tables.Clear()
         DsRetLog11.Tables.Add(dtSearch)


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


garyinmiami2003Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cubixSoftwareCommented:
Hi

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")
    dtSearch.Add(dr)
Next

DsRetLog11.Tables.Clear()
DsRetLog11.Tables.Add(dtSearch)
0
SanclerCommented:
Gary

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 ;-)

Roger
0
garyinmiami2003Author Commented:
Roger:

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.
   

gary
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

SanclerCommented:
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
        dt.ImportRow(dr)
        'empty the dataset
        DsRetLog11.Tables.Clear()
        'put the new table in the dataset
        DsRetLog11.Tables.Add(dtSearch)

Roger
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
garyinmiami2003Author Commented:
Roger:
1
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.      
0
SanclerCommented:
So long as you're happy.  By the way, but I expect you noticed, this line

        dt.ImportRow(dr)

should have read

        dtSearch.ImportRow(dr)

Roger
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.