dcsimg
Results 1 to 17 of 17

Thread: Copy row to databound datagridview

  1. #1

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,118

    Copy row to databound datagridview

    Hi, can someone please teach me how to do this? I am use VS2012 and have dgv's that use dataset as datasource. I drag this two dgvs to userform. I search many solution but can find only for when dgv's are not databound. I have this two dgv's on userform:

    tblFoodMainDatagridview (left)
    tblFoodSelectDatagridview (right)

    I want to click the button and then selected item in left hand side dgv must be copy over to dgv on righ hand side datagridview.

    Name:  Capture_1.jpg
Views: 377
Size:  20.1 KB

    then here is datasets I have:

    Name:  Capture_2.jpg
Views: 293
Size:  5.4 KB


    Thanks,
    Michelle
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,961

    Re: Copy row to databound datagridview

    Are the grid's bound to the same DataTable or different? Depending on the circumstances, it might make sense to bind the one DataTable to two BindingSources and bind them to the two grids. You can set the Filter of each BindingSource so that they each expose different data. You can then move a row from one grid to the other simply by changing the value of the column you are filtering on.

  3. #3

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,118

    Re: Copy row to databound datagridview

    Hi John,

    Thank you for you reply...

    Are the grid's bound to the same DataTable or different?
    uhm... It is bound to dataset? I have create a "datasource" for each dgv (FoodMainDataset and FoodSelectDataset) Then I drag each dataset to userform to create DGV. I know you will say you must not teach me basics but also I am not fully comprehend difference between dataset & datatable - so please be patient for me. I will get there...

    In 2nd picture is everything I have.. 2 datasets, 2 bindingsource, 2 tableadapter, 2 tabledadaptermanager and 1 bindingnavigator.. So I just want to 'copy' item to 2nd dgv, not move... meaning if I select apples and click button then apples must now display in 2nd dgv (but will not be "removed" from 1st dgv) Then when I press save button, then 2nd dgv must save items e.g. apples to database...
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,961

    Re: Copy row to databound datagridview

    How many databases do you have? That's how many DataSets you should have. Notice how a database generally contains multiple tables? What do you suppose a DataSet contains?

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,961

    Re: Copy row to databound datagridview

    If a control is bound then you work with the data source. If you want to copy the selected row from one bound grid to another bound grid then you need to get the item from the first data source, which I've told you how to do, and then add a corresponding item to the second data source. I would expect that you know how to add a row to a DataTable in code, or at least be able to find out, and that's all you need to do here. The fact that it's bound to a grid is irrelevant to that.

  6. #6

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,118

    Re: Copy row to databound datagridview

    Okay I think to understand that part. I have datasets that is contains tables i.e. datatables... So a dataset can have many datatables...

    So to answer your question each grid is bound to own datatable

    Name:  Capture_3.PNG
Views: 222
Size:  5.9 KB
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  7. #7

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,118

    Re: Copy row to databound datagridview

    you need to get the item from the first data source, which I've told you how to do
    the first datasource will be 'tblFoodMain'?

    You say:

    it might make sense to bind the one DataTable to two BindingSources and bind them to the two grids. You can set the Filter of each BindingSource so that they each expose different data. You can then move a row from one grid to the other simply by changing the value of the column you are filtering on
    I have thus 2 datatables that is bind to 2 grids.... John, I dont understand what you mean with filter.. ?
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,961

    Re: Copy row to databound datagridview

    Do you have two databases? If not, get rid of one of those DataSets and configure the other one to contain ALL the tables.
    Quote Originally Posted by schoemr View Post
    I dont understand what you mean with filter.. ?
    If the grids are bound to DataTables that represent different database tables then this is not a case where binding to one DataTable would make sense. That said, if I tell you to set the Filter property of a BindingSource and you don't understand what I mean when I say filter then it should be obvious that the first thing you should do is to read the documentation for that property. This is basically the same issue we've always had. There are obvious things that you can do for yourself that you don't do. I know that I've directed you to the documentation before and here we are again, with you not bothering to read the relevant documentation. No matter who it is, I always expect someone who is asking for help to do what they can for themselves first and if they haven't used the Help menu then they haven't done that.

    Anyway, it doesn't matter in this case because filtering one DataTable is not the way to go. As I have already said, all you need to do is to get the data from the selected row in the first grid, which you can do from the grid row itself, the DataRowView from the DataBoundItem property or the DataRow from the Row property of that. No point going any deeper than you need though, so just use the grid row. It's then a simple case of adding a new row to the data source of the second grid. You can do that via the BindingSource or simply create a DataRow from the second DataTable and Add it.

  9. #9

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,118

    Re: Copy row to databound datagridview

    John,

    get rid of one of those DataSets and configure the other one to contain ALL the tables
    I don't want to change topic because I want to resolve this currently question but I wish to talk about (at some other time) about what you say here above. I have watch many vb videos and I have never see that all tables must be in one dataset... I can post links to that. This is not to say you are wrong, I am just wonder why you say that. I mean, does it matter if I there is two datasets? I have like 50 datasets already (and one database) But if I have to say, I would say that you have good reason for say that... But as I say, I don't want change topic...


    when I say filter then it should be obvious that the first thing you should do is to read the documentation for that property. This is basically the same issue we've always had. There are obvious things that you can do for yourself that you don't do. I know that I've directed you to the documentation before and here we are again, with you not bothering to read the relevant documentation. No matter who it is, I always expect someone who is asking for help to do what they can for themselves first and if they haven't used the Help menu then they haven't done that.
    I think you not understand what I mean.. I know how to filter, I have many place where I do that, what I was mean is 'why' would you say that when I was ask to copy row between DGVs.......

    Anyway, it doesn't matter in this case because filtering one DataTable is not the way to go.
    okay, so no need to fight with me And just so you know I was tired of looking and looking and looking before I ask for help. Sometimes people assume they not do anything but they not know how that person have try lots of hours to get something resolve.


    all you need to do is to get the data from the selected row in the first grid, which you can do from the grid row itself, the DataRowView from the DataBoundItem property or the DataRow from the Row property of that. No point going any deeper than you need though
    I think this I can do... I have copy info from dgvs to textbox before and if it is similar then I should be able to do..

    It's then a simple case of adding a new row to the data source of the second grid. You can do that via the BindingSource or simply create a DataRow from the second DataTable and Add it.
    Here am really stuck... For me its not simple case.. I can not figure out this part..
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  10. #10
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,961

    Re: Copy row to databound datagridview

    Quote Originally Posted by schoemr View Post
    I can not figure out this part..
    So, again, do the obvious. If you want to add a row to a DataTable and you don't know how then search the web for how to add a row to a DataTable. I'm afraid that I just don't understand this apparent inability or reluctance to type a few words into a search engine. It IS a simple case to type "vb.net add row to datatable" into Google. I can understand the issue when you don't know what to search for but I told you what to search for.

  11. #11

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,118

    Re: Copy row to databound datagridview

    I did search MANY time and get things like:

    http://stackoverflow.com/questions/9...tatable-vb-net

    but okay Johan, you obviously don't want to help. Leave it.

    Thanks
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  12. #12
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,961

    Re: Copy row to databound datagridview

    Quote Originally Posted by schoemr View Post
    I did search MANY time and get things like:

    http://stackoverflow.com/questions/9...tatable-vb-net
    Yeah, and? Did you even read it?
    Code:
        Dim R As DataRow = dt.NewRow
        R("Name") = txtName.Text
        dt.Rows.Add(R)
    Oh look, code that does exactly what you want. I am going to leave it. It seems like every time you just ignore things that are right in front of you. If you're not learning from your mistakes then we're both wasting our time.

  13. #13
    New Member
    Join Date
    Aug 2019
    Posts
    1

    Re: Copy row to databound datagridview

    Hi Schoemr,
    Have you solved this problem? I'm having the same problem. If you have a solution, please help me.

    Note: This is a Google translation.

  14. #14

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,118

    Re: Copy row to databound datagridview

    Hi axion, yes I have. But I don't have my computer with me now therefore I can only help you tomorrow... I also use Google translate..
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  15. #15
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,961

    Re: Copy row to databound datagridview

    Ah, this one brings back some memories, doesn't it? It seems that we've progressed somewhat in the last two years or so.

  16. #16
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    2,051

    Re: Copy row to databound datagridview

    this would copy a selected row from DGV1 to DGV2

    I assume the columns in DGV2 are loaded
    in the Button..
    Code:
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim CopyRowIndex As Integer = DataGridView1.CurrentRow.Index
            
            DataGridView2.Rows.Add(DataGridView1.Rows(CopyRowIndex).Cells.Cast(Of DataGridViewCell).Select(Function(c) c.Value).ToArray)
    
        End Sub
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  17. #17

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,118

    Re: Copy row to databound datagridview

    Quote Originally Posted by jmcilhinney View Post
    Ah, this one brings back some memories, doesn't it? It seems that we've progressed somewhat in the last two years or so.
    Well, at least you did not say "fond" memories

    Yes we have.... I am going to keep bug you!
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width