Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Manipulating data from datagrid, datatables, Using Current RowIndex

Posted on 2006-03-28
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)

Question by:garyinmiami2003
  • 3
  • 2

Assisted Solution

cubixSoftware earned 200 total points
ID: 16311117

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

LVL 34

Expert Comment

ID: 16311722

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


Author Comment

ID: 16312383

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.

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 34

Accepted Solution

Sancler earned 1800 total points
ID: 16312710
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


Author Comment

ID: 16312721
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.      
LVL 34

Expert Comment

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


should have read



Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question