Results 1 to 17 of 17

Thread: [RESOLVED] Help! updating DGV cells with values from another DGV

  1. #1

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    288

    Resolved [RESOLVED] Help! updating DGV cells with values from another DGV

    Hello!

    Please I need some help, I've been scratching my head for a while trying to figure out this...
    I want to update some DGV cells with the values from another DGV cells, all this on the Load event of my form, something like this:

    Name:  2a.jpg
Views: 22391
Size:  41.1 KB

    I can manually update specific cells on a DGV, my problem is... I don't know how to do this automatically.

    OK, I have 2 DGVs:
    DataGridView2: (the one at the top) it has no bindings, it is just populated at run time.
    DataGridView1: (the one at the bottom) it is bound to a database table.

    DataGridView2 is populated with the following code (No problem here!):
    VB.NET Code:
    1. DataGridView2.Columns.Add(0, "Horario")
    2.             Dim I As Integer
    3.             For Each item As String In ComboBox4.Items 'get the column names from this ComboBox
    4.                 DataGridView2.Columns.Add(I, item)
    5.                 I += 1
    6.             Next
    7.  Dim limite As Integer
    8.             Dim StartTime As TimeSpan = TimeSpan.Parse("08:00") 'starts shift
    9.             Dim EndTime As TimeSpan = TimeSpan.Parse("17:00") 'Ends shift
    10.             Dim duration = EndTime - StartTime
    11.             Dim durationInmins = duration.TotalMinutes
    12.             limite = Math.Abs(durationInmins) / 10
    13.  
    14.             'Generates the time values on column [Horario]
    15.             Dim temp As Date = #01/01/0001 08:00#
    16.             Dim starter As Date = temp
    17.             starter = starter.AddMinutes(-10)
    18.             Dim minu As Double = 10.0
    19.             For s As Integer = 0 To limite
    20.                 starter = starter.AddMinutes(minu)
    21.                 DataGridView2.Rows.Add(starter.ToShortTimeString())
    22.             Next

    My attempt to update the cell values on DataGridView2:
    VB.NET Code:
    1. For Each col As DataGridViewColumn In DataGridView2.Columns
    2.                 For Each row As DataGridViewRow In DataGridView1.Rows
    3.                     Dim time_row As String = row.Cells("Horario_Inicia").Value
    4.                     If row.Cells("Barbero").Value = "Max" Then
    5.                         DataGridView2.Item(2, 2).Value = (row.Cells("Barbero").Value) '(Col, row)
    6.                     Else
    7.                         DataGridView2.Rows.Add()
    8.                     End If
    9.                 Next
    10.             Next

    Please, I'll appreciate any help!
    Last edited by Spybot; Oct 3rd, 2020 at 09:48 PM.

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

    Re: Help! updating DGV cells with values from another DGV

    Quote Originally Posted by Spybot View Post
    it is bounded to a database table.
    For the record, the word is "bound", not "bounded". In this case, you want the past tense of "to bind", which is "bound". "Bounded" is the past tense of "to bound", as in "my dog was so happy to see me that it bounded all the way down the driveway when I got home". It can also be used in the context of a boundary, e.g. "my property is bounded to the south by a stream". The word "bounded" has absolutely nothing to do with binding.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,738

    Re: Help! updating DGV cells with values from another DGV

    As for the issue, you haven't actually told us what it is you want to do. You have shown us a visual example and you have shown us code that doesn't actually do it but you haven't explained what the actual rules are that you are trying to implement. That suggests that you don't actually know what the rules are so it's not surprise that you can't write code to implement them. The code is not the solution to the problem. It is an implementation of the solution. The solution is the logic and, based on your post, you don't actually know what that logic is.

  4. #4

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    288

    Re: Help! updating DGV cells with values from another DGV

    Hi jmc!
    My fault, sorry for the word bounded, unfortunately English is not my native language.

    Yes I know what I want, IŽd like you to help me place the (name) value from DataGridView1 (the bottom one) into the corresponding column(name) with it's corresponding row(Horario) in DataGridView2 (the top one).

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

    Re: Help! updating DGV cells with values from another DGV

    Quote Originally Posted by Spybot View Post
    My fault, sorry for the word bounded, unfortunately English is not my native language.
    No problem. I was informing, not criticising. I've seen many a native speaker get that wrong too and it makes my eye twitch a bit when I see it.
    Quote Originally Posted by Spybot View Post
    IŽd like you to help me place the (name) value from DataGridView1 (the bottom one) into the corresponding column(name) with it's corresponding row(Horario) in DataGridView2 (the top one).
    That's still a very vague description and that people only ever go as far as vague descriptions is the primary reason that people can't come up with the code to do what they want. You should be able write down a set of instructions that you could hand to a complete moron with no prior experience with your project that they could follow to get the correct result every time. If you can't do that then you haven't really thought the problem through fully. If you can do that then you can write the code to implement each step in that algorithm explicitly. Each step in the algorithm would generally require only one line of code and probably three at the absolute most, so implementing each individual step should be very easy. Once you've written the code, you can step through it line by line in the debugger and compare what actually happens to what you expect to happen. If the code doesn't work as expected then you can identify EXACTLY where and how the actual behaviour differs from the expected behaviour. Even if you still don't know how to fix it, at least you can provide us with far more relevant information.

  6. #6

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    288

    Re: Help! updating DGV cells with values from another DGV

    I think I came here asking for help, and you're telling me that I have to answer my self with the correct code? so what's the the point of a forum? isn't it to help others? I can't write the working code because I don't know what the approach would be for this particular task, I thought maybe more experienced people could help, I'm not a professional programmer.
    Anyway, I still need help on this one, but asking me to write the code myself is not gonna work, if I knew the correct lines I wouldn't be here bothering people, asking for help.
    Last edited by Spybot; Oct 3rd, 2020 at 10:29 PM.

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,738

    Re: Help! updating DGV cells with values from another DGV

    Quote Originally Posted by Spybot View Post
    you're telling me that I have to answer my self with the correct code?
    Except I'm not and it's completely disingenuous to claim that I am, but it's a common reaction to my pointing out to someone that they could have made more effort on their own behalf before posting. Many people don't want to admit that so they have to pretend that I'm expecting something unreasonable when I'm not. What I actually said was that, instead of starting with a vague idea of what the code needs to do and trying to go from that to code, you should start by getting a detailed understanding of the steps required to get to your desired result. If you could perform the task manually, e.g. with pen and paper, then you are capable of developing an algorithm for the process. It takes some thought and effort though, because human beings are very good at applying fuzzy logic. That means that you have to put some time and effort into working out what the actual rules are that you're following. That is the stage that most people baulk at. They don't really want to put in that time and effort because it's not sexy. They want to have an idea, write code and se it work, then repeat. That's not software development though. Once you have the algorithm, then you write your code to implement that specifically, rather than your vague idea. That means that the code you write has a far greater chance of working first time and, if it doesn't, you have a far greater chance of working out why for yourself. I don't really see how anyone could not consider that to be a good thing. As I explicitly stated earlier though, even if your code still doesn't work and you still can't work out why, you can then ask a question here but you will be able to provide us with far more detail about the issue. It also means that the issue is far more likely to be localised. I lost count years ago of the number of times people ask about an issue and it's hard to point out where the problem is because it's the whole code. In conclusion, I didn't say that you have to write the correct code yourself and if you bothered to actually read what I posted then you already know that. What I said was that there is far more that you can do for yourself - should do for yourself - before posting. If you want to be an actual software developer, you need to do those things. I don't make the rules here so you don't have to do as I say but if you care about being the best developer you can be, which is what I care about, then you need to.

  8. #8

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    288

    Re: Help! updating DGV cells with values from another DGV

    What I've been noticing about you is that you want people to think and program like you do, I repeat I'm not a professional programmer and will never be, because I don't want to, This is just an informal personal project that I'd like to finish, if you don't want to help me, that's fine with me, maybe someone else will.

    But honestly you don't have to humiliate people like you do, and I'm not the only one in this forum, I've seen this behavior from you
    since long time ago with other members, most of the time you're not a nice person, just because you probably think that you are a very good software developer, it doesn't mean you have to be rude to people that don't have the same knowledge as you.
    In the past, you have helped me lots of times, which I appreciate, unfortunately most of the times your help includes some kind of rudeness which I don't agree with, I'll soon leave this forum because I'm old and tired, just try to be nice to people.

    thanks for your help jmc.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,382

    Re: Help! updating DGV cells with values from another DGV

    jm told you to work out the steps necessary to solve the task. Plain english steps, such as loop through dgv2 rows, take the name from the first column, and the time from the second column, then loop through dgv1 and decide where to put the name by column, and by row (time). That is what it takes to be a programmer. I haven't completed that for you, but i've shown you what you must do before you get any help with code...

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

    Re: Help! updating DGV cells with values from another DGV

    you should rethink the design, I don't think that "Horario" should be a column
    and would put that as the HeaderCell

    here a sample
    you need 3 Checkedlistboxes
    1 DGV and a Button

    Code:
    Public Class HeaderTimes
    
        Private Sub HeaderTimes_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    
            CheckedListBox1.CheckOnClick = True
            CheckedListBox2.CheckOnClick = True
            CheckedListBox3.Enabled = False
            'set Columns
            DataGridView1.Columns.Add("Max", "Max")
            DataGridView1.Columns.Add("Mariam", "Mariam")
            DataGridView1.Columns.Add("kevin", "kevin")
            'set hours
            For i = 0 To 23
                CheckedListBox1.Items.Add("hours " & Format(i, "00"))
            Next
            CheckedListBox1.SetItemChecked(8, True)
            CheckedListBox1.SetItemChecked(9, True)
            CheckedListBox1.SetItemChecked(10, True)
            CheckedListBox1.SetItemChecked(11, True)
            CheckedListBox1.SetItemChecked(19, True)
           
            'set minutes
            With CheckedListBox2
                For i = 0 To 55 Step 5
                    .Items.Add("minutes " & Format(i, "00"))
                Next
                For i = 1 To .Items.Count - 1 Step 3
                    .SetItemChecked(CInt(i), True)
                Next i
            End With
           
    
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            For Each s As String In CheckedListBox1.CheckedItems
                For Each f As String In CheckedListBox2.CheckedItems
                    CheckedListBox3.Items.Add(s.Substring(s.Length - 2) & ":" & f.Substring(f.Length - 2))
                Next
            Next
            'add your selected Hours and minutes to Headercell
            DataGridView1.Rows.Add(CheckedListBox3.Items.Count)
            DataGridView1.RowHeadersWidth = CInt(DataGridView1.RowHeadersWidth * 1.75)
            For x As Integer = CheckedListBox3.Items.Count - 1 To 0 Step -1
                Dim value As String = CheckedListBox3.Items(x).ToString
                DataGridView1.Rows(x).HeaderCell.Value = value
            Next
        End Sub
    End Class
    and here what it looks like
    Name:  HeaderTimes.jpg
Views: 75
Size:  28.9 KB
    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.

  11. #11
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,738

    Re: Help! updating DGV cells with values from another DGV

    Quote Originally Posted by Spybot View Post
    But honestly you don't have to humiliate people like you do
    I don't humiliate people. They do that to themselves. I explain how people should approach a programming problem and people refuse to listen, seemingly because they don't want to admit that they could have done anything wrong in the first place and also because taking my advice would involve extra effort. All I did was explain how you should approach a programming problem. If you're not prepared to do that then that's up to you but if that's the case then why not just say that you're not really prepared to make the effort. If you are prepared to make the effort, why not make the effort instead of arguing with me and then complaining about how I'm humiliating you? It it an objective fact that, if you work out the logic first, formalise that into an algorithm and then write code specifically to implement that algorithm, you'll more often end up with code that does what you want. Don't you think that would be a good thing? I would expect so, so the only reason that I can think of that people wouldn't do it is that it's too much effort. You don't have to make any more effort than you want to but, as someone being asked to volunteer their time to help, I'd like to know how much effort people are prepared to make in order to decide how much effort I'm prepared to make. This is yet another example of someone spending more time arguing with me why they shouldn't do things the proper way than it would have taken to do things the proper way. Out.

  12. #12

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    288

    Re: Help! updating DGV cells with values from another DGV

    Hi CrisE!

    I took your advice and removed "Horario" as a column and moved it to the RowHeader position, I can't use CheckedListBoxes because are not part of my design. I've solve the problem thanks to your advice and .paul's.

    Thank you!

  13. #13

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    288

    Re: Help! updating DGV cells with values from another DGV

    Hi .paul.

    As you said "in Plain english steps" I managed to solved the problem thanks to your guidance, the code I wrote is this:
    VB.NET Code:
    1. For Each col As DataGridViewColumn In DataGridView2.Columns
    2.                 For Each row As DataGridViewRow In DataGridView1.Rows
    3.                     Dim Time_from_DGV1 As String
    4.                     Dim Name_from_DGV1 As String
    5.                     Time_from_DGV1 = row.Cells("Horario_Inicia").Value
    6.                     Name_from_DGV1 = row.Cells("Barbero").Value
    7.                    Dim i As Integer = 0
    8.                     For Each row3 As DataGridViewRow In DataGridView2.Rows
    9.                         If row3.HeaderCell.Value = Time_from_DGV1 Then
    10.                             DataGridView2.Rows(i).Cells(Name_from_DGV1).Value = Name_from_DGV1
    11.                         End If
    12.                         i += 1
    13.                     Next
    14.                 Next
    15.             Next

    Name:  2a.jpg
Views: 52
Size:  33.1 KB

    Thank you!

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,382

    Re: Help! updating DGV cells with values from another DGV


  15. #15
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,382

    Re: Help! updating DGV cells with values from another DGV

    Code:
    For Each col As DataGridViewColumn In DataGridView2.Columns???
                    For Each row As DataGridViewRow In DataGridView1.Rows
                        Dim Time_from_DGV1 As String
                        Dim Name_from_DGV1 As String
                        Time_from_DGV1 = row.Cells("Horario_Inicia").Value
                        Name_from_DGV1 = row.Cells("Barbero").Value
                       Dim i As Integer = 0
                        For Each row3 As DataGridViewRow In DataGridView2.Rows
                            If row3.HeaderCell.Value = Time_from_DGV1 Then
                                DataGridView2.Rows(i).Cells(Name_from_DGV1).Value = Name_from_DGV1
                            End If
                            i += 1
                        Next
                    Next
                Next
    You don't need the column loop...

  16. #16

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    288

    Re: Help! updating DGV cells with values from another DGV

    I just realize that too, thanks anyway .paul.

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

    Re: Help! updating DGV cells with values from another DGV

    Quote Originally Posted by Spybot View Post
    Hi CrisE!

    I took your advice and removed "Horario" as a column and moved it to the RowHeader position, I can't use CheckedListBoxes because are not part of my design. I've solve the problem thanks to your advice and .paul's.

    Thank you!
    well my sample was just a sample, I wanted you to rethink the design,
    looks like you have got it solved.
    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.

Posting Permissions

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



Click Here to Expand Forum to Full Width