Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. how to controll cells inside Table1DataGridView

how to controll cells inside Table1DataGridView

Scheduled Pinned Locked Moved Visual Basic
questionjsontutorial
17 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R romo22

    no I need to move text from cell to another cell on the same row once column 0 is checked :confused:

    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #6

    I have no idea what your problem could be, just handle the appropriate event. MSDN doc on DataGridViewCheckBoxCell class says: Typically, check box cell values are intended either for storage, like any other data, or for performing bulk operations. If you want to respond immediately when users click a check box cell, you can handle the DataGridView..::.CellClick event, but this event occurs before the cell value is updated. If you need the new value at the time of the click, one option is to calculate what the expected value will be based on the current value. Another approach is to commit the change immediately, and handle the DataGridView..::.CellValueChanged event to respond to it. To commit the change when the cell is clicked, you must handle the DataGridView..::.CurrentCellDirtyStateChanged event. In the handler, if the current cell is a check box cell, call the DataGridView..::.CommitEdit method and pass in the Commit value. :)

    Luc Pattyn [My Articles] Nil Volentibus Arduum

    1 Reply Last reply
    0
    • R romo22

      no I need to move text from cell to another cell on the same row once column 0 is checked :confused:

      J Offline
      J Offline
      JohnPayton
      wrote on last edited by
      #7

      What Luc is suggesting is correct, use an event handler for the DataGridView to trigger the action you want to occur. Is the DataGridView populated by a data table or array? Do you wish to save the changes back to the database? But to just populate the second cell with the third cell's contents just use something like this. 'Declare a couple of variables to hold the row number and contents of the third cell Dim pRowNo as Integer Dim pstrContents as String 'Get the ROW number pRowNo = Val(DataGridView.SelectedCells(0).RowIndex.ToString()) 'It's here where you test if Cell 0 is checked, then if so the remainder (below) is actioned. 'Store contents of cell three pstrContents = DataGridView.Rows(pRowNo ).Cells(2).Value 'Insert contents into cell two DataGridView.Rows(gintROWNO).Cells(1).Value = pstrContents (The above can be shortened, but I did it this way so you can step through to check the value being copied to "pstrContents" prior to the insert into the second cell) It would be here you save any changes back to the data source and refresh the grid. Does this help?

      R 1 Reply Last reply
      0
      • J JohnPayton

        What Luc is suggesting is correct, use an event handler for the DataGridView to trigger the action you want to occur. Is the DataGridView populated by a data table or array? Do you wish to save the changes back to the database? But to just populate the second cell with the third cell's contents just use something like this. 'Declare a couple of variables to hold the row number and contents of the third cell Dim pRowNo as Integer Dim pstrContents as String 'Get the ROW number pRowNo = Val(DataGridView.SelectedCells(0).RowIndex.ToString()) 'It's here where you test if Cell 0 is checked, then if so the remainder (below) is actioned. 'Store contents of cell three pstrContents = DataGridView.Rows(pRowNo ).Cells(2).Value 'Insert contents into cell two DataGridView.Rows(gintROWNO).Cells(1).Value = pstrContents (The above can be shortened, but I did it this way so you can step through to check the value being copied to "pstrContents" prior to the insert into the second cell) It would be here you save any changes back to the data source and refresh the grid. Does this help?

        R Offline
        R Offline
        romo22
        wrote on last edited by
        #8

        ok but under where I should put this code. you said use an event handler, I'm sorry I don't how to use this feature where I can find the event handler is it something like this

        Private Sub Button1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.TextChanged

        End Sub
        
        J 1 Reply Last reply
        0
        • R romo22

          ok but under where I should put this code. you said use an event handler, I'm sorry I don't how to use this feature where I can find the event handler is it something like this

          Private Sub Button1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.TextChanged

          End Sub
          
          J Offline
          J Offline
          JohnPayton
          wrote on last edited by
          #9

          Hi romo22, An event handler is just an event that is called when a user clicks on or otherwise actions a control, such as your DataGridView. Clicking on the DataGridView, will trigger the On_Click event. The first line of code after the declaration gets the ROW number of the grid, which in turn is used to access the data from the cells. For example...... Private Sub DataGridView_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView.Click 'Declare a couple of variables to hold the row number and contents of the third cell Dim pRowNo as Integer Dim pstrContents as String 'Get the ROW number pRowNo = Val(DataGridView.SelectedCells(0).RowIndex.ToString()) 'It's here where you test if Cell 0 is checked, then if so the remainder (below) is actioned. 'Store contents of cell three pstrContents = DataGridView.Rows(pRowNo ).Cells(2).Value 'Insert contents into cell two DataGridView.Rows(gintROWNO).Cells(1).Value = pstrContents End Sub This should get you started, you will find some great examples using Google. Remember, this will place the data in the cell but will not save ot to your data source.

          R 1 Reply Last reply
          0
          • J JohnPayton

            Hi romo22, An event handler is just an event that is called when a user clicks on or otherwise actions a control, such as your DataGridView. Clicking on the DataGridView, will trigger the On_Click event. The first line of code after the declaration gets the ROW number of the grid, which in turn is used to access the data from the cells. For example...... Private Sub DataGridView_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView.Click 'Declare a couple of variables to hold the row number and contents of the third cell Dim pRowNo as Integer Dim pstrContents as String 'Get the ROW number pRowNo = Val(DataGridView.SelectedCells(0).RowIndex.ToString()) 'It's here where you test if Cell 0 is checked, then if so the remainder (below) is actioned. 'Store contents of cell three pstrContents = DataGridView.Rows(pRowNo ).Cells(2).Value 'Insert contents into cell two DataGridView.Rows(gintROWNO).Cells(1).Value = pstrContents End Sub This should get you started, you will find some great examples using Google. Remember, this will place the data in the cell but will not save ot to your data source.

            R Offline
            R Offline
            romo22
            wrote on last edited by
            #10

            yes it's work as super :-D :-D :-D :-D :-D but I wonder how can make cell 1 clear once checked is false where I can add the if and else function. I will try by myself first :rose::rose::rose::rose: thanks so much I spent six hours trying with myself finally I'm done with it oh my God

            Private Sub Table1DataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Table1DataGridView.CellContentClick
            'Declare a couple of variables to hold the row number and contents of the third cell

                Dim pRowNo As Integer
                Dim pstrContents As String
            
                'Get the ROW number
                pRowNo = Val(Table1DataGridView.SelectedCells(0).RowIndex.ToString())
            
                'It's here where you test if Cell 0 is checked, then if so the remainder (below) is actioned.
                '    If Table1DataGridView.Rows(0).Cells(0).Value = True Then
                'Store contents of cell three
                pstrContents = Table1DataGridView.Rows(pRowNo).Cells(2).Value
            
                'Insert contents into cell two
                Table1DataGridView.Rows(pRowNo).Cells(1).Value = pstrContents
                '    End If
            End Sub
            
            R 1 Reply Last reply
            0
            • R romo22

              yes it's work as super :-D :-D :-D :-D :-D but I wonder how can make cell 1 clear once checked is false where I can add the if and else function. I will try by myself first :rose::rose::rose::rose: thanks so much I spent six hours trying with myself finally I'm done with it oh my God

              Private Sub Table1DataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Table1DataGridView.CellContentClick
              'Declare a couple of variables to hold the row number and contents of the third cell

                  Dim pRowNo As Integer
                  Dim pstrContents As String
              
                  'Get the ROW number
                  pRowNo = Val(Table1DataGridView.SelectedCells(0).RowIndex.ToString())
              
                  'It's here where you test if Cell 0 is checked, then if so the remainder (below) is actioned.
                  '    If Table1DataGridView.Rows(0).Cells(0).Value = True Then
                  'Store contents of cell three
                  pstrContents = Table1DataGridView.Rows(pRowNo).Cells(2).Value
              
                  'Insert contents into cell two
                  Table1DataGridView.Rows(pRowNo).Cells(1).Value = pstrContents
                  '    End If
              End Sub
              
              R Offline
              R Offline
              romo22
              wrote on last edited by
              #11

              Hi JohnPayton I tried to add if function but it doesn't work with me the code without if function work prefect :thumbsup:

              Private Sub Table1DataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Table1DataGridView.CellContentClick
              'Declare a couple of variables to hold the row number and contents of the third cell

                  Dim pRowNo As Integer
                  Dim pstrContents As String
                   
                  'Get the ROW number
                  pRowNo = Val(Table1DataGridView.SelectedCells(0).RowIndex.ToString())
              
                 ' here I try to add if 
              

              If Table1DataGridView.Rows(pRowNo).Cells(0).Value = True Then

                      'It's here where you test if Cell 0 is checked, then if so the remainder (below) is actioned.
                      '    If Table1DataGridView.Rows(0).Cells(0).Value = True Then
                      'Store contents of cell three
                      pstrContents = Table1DataGridView.Rows(pRowNo).Cells(2).Value
                    
                      'Insert contents into cell two
                      Table1DataGridView.Rows(pRowNo).Cells(1).Value = pstrContents
                     
              
                  Else
                       Table1DataGridView.Rows(pRowNo).Cells(1).Value = ""
              
                  End If
              End Sub
              

              I need to add if because once cell 0 checked= false then cell1 = clear or "" empty could please help me with this

              R 1 Reply Last reply
              0
              • R romo22

                Hi JohnPayton I tried to add if function but it doesn't work with me the code without if function work prefect :thumbsup:

                Private Sub Table1DataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Table1DataGridView.CellContentClick
                'Declare a couple of variables to hold the row number and contents of the third cell

                    Dim pRowNo As Integer
                    Dim pstrContents As String
                     
                    'Get the ROW number
                    pRowNo = Val(Table1DataGridView.SelectedCells(0).RowIndex.ToString())
                
                   ' here I try to add if 
                

                If Table1DataGridView.Rows(pRowNo).Cells(0).Value = True Then

                        'It's here where you test if Cell 0 is checked, then if so the remainder (below) is actioned.
                        '    If Table1DataGridView.Rows(0).Cells(0).Value = True Then
                        'Store contents of cell three
                        pstrContents = Table1DataGridView.Rows(pRowNo).Cells(2).Value
                      
                        'Insert contents into cell two
                        Table1DataGridView.Rows(pRowNo).Cells(1).Value = pstrContents
                       
                
                    Else
                         Table1DataGridView.Rows(pRowNo).Cells(1).Value = ""
                
                    End If
                End Sub
                

                I need to add if because once cell 0 checked= false then cell1 = clear or "" empty could please help me with this

                R Offline
                R Offline
                romo22
                wrote on last edited by
                #12

                Payton I still need your help where I can add if function

                J 1 Reply Last reply
                0
                • R romo22

                  Payton I still need your help where I can add if function

                  J Offline
                  J Offline
                  JohnPayton
                  wrote on last edited by
                  #13

                  Morning romo22, Let me understand what you are doing here.... The way I read your code at the moment is If a user clicks on a Row and Cell 0 (checkbox) is Checked, then the contents of Cell 2 are moved into Cell 1 otherwise you clear the contents of Cell 1 Are you using mouse clicks to Check and UnCheck the checkbox in Cell 0 or is it being populated by a data source?

                  R 1 Reply Last reply
                  0
                  • J JohnPayton

                    Morning romo22, Let me understand what you are doing here.... The way I read your code at the moment is If a user clicks on a Row and Cell 0 (checkbox) is Checked, then the contents of Cell 2 are moved into Cell 1 otherwise you clear the contents of Cell 1 Are you using mouse clicks to Check and UnCheck the checkbox in Cell 0 or is it being populated by a data source?

                    R Offline
                    R Offline
                    romo22
                    wrote on last edited by
                    #14

                    "The way I read your code at the moment is If a user clicks on a Row and Cell 0 (checkbox) is Checked, then the contents of Cell 2 are moved into Cell 1 otherwise you clear the contents of Cell 1" yes that what I want to do " Are you using mouse clicks to Check and UnCheck the checkbox in Cell 0 or is it being populated by a data source?" yes I use the mouse clicks to Check and UnCheck the checkbox in Cell 0

                    J 1 Reply Last reply
                    0
                    • R romo22

                      "The way I read your code at the moment is If a user clicks on a Row and Cell 0 (checkbox) is Checked, then the contents of Cell 2 are moved into Cell 1 otherwise you clear the contents of Cell 1" yes that what I want to do " Are you using mouse clicks to Check and UnCheck the checkbox in Cell 0 or is it being populated by a data source?" yes I use the mouse clicks to Check and UnCheck the checkbox in Cell 0

                      J Offline
                      J Offline
                      JohnPayton
                      wrote on last edited by
                      #15

                      I loaded the code below and it works, it's a little simplistic however by playing around with it I'm sure you can improve your skills by making adjustments. :-O Private Sub DataGridView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.Click Dim pRowNo As Integer pRowNo = Val(DataGridView1.SelectedCells(0).RowIndex.ToString()) If DataGridView1.Rows(pRowNo).Cells(0).Value = 1 Then DataGridView1.Rows(pRowNo).Cells(1).Value = DataGridView1.Rows(pRowNo).Cells(2).Value Else DataGridView1.Rows(pRowNo).Cells(1).Value = "" End If End Sub

                      R 1 Reply Last reply
                      0
                      • J JohnPayton

                        I loaded the code below and it works, it's a little simplistic however by playing around with it I'm sure you can improve your skills by making adjustments. :-O Private Sub DataGridView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.Click Dim pRowNo As Integer pRowNo = Val(DataGridView1.SelectedCells(0).RowIndex.ToString()) If DataGridView1.Rows(pRowNo).Cells(0).Value = 1 Then DataGridView1.Rows(pRowNo).Cells(1).Value = DataGridView1.Rows(pRowNo).Cells(2).Value Else DataGridView1.Rows(pRowNo).Cells(1).Value = "" End If End Sub

                        R Offline
                        R Offline
                        romo22
                        wrote on last edited by
                        #16

                        Hi John thank you for standing with me in this code I got this error " Operator '=' is not defined for type 'DBNull' and type 'Integer'."

                        DataGridView1.Rows(pRowNo).Cells(0).Value = 1

                        I tried to do something like this

                        DataGridView1.Rows(pRowNo).Cells(0).Value.Tobit = 1

                        but I still get the same error notice that I'm working on Visual basic Studio 2010 is ther any difference :confused:

                        J 1 Reply Last reply
                        0
                        • R romo22

                          Hi John thank you for standing with me in this code I got this error " Operator '=' is not defined for type 'DBNull' and type 'Integer'."

                          DataGridView1.Rows(pRowNo).Cells(0).Value = 1

                          I tried to do something like this

                          DataGridView1.Rows(pRowNo).Cells(0).Value.Tobit = 1

                          but I still get the same error notice that I'm working on Visual basic Studio 2010 is ther any difference :confused:

                          J Offline
                          J Offline
                          JohnPayton
                          wrote on last edited by
                          #17

                          You just have to test if the value is a null first. Using a IsDBNull function call. :) http://www.freevbcode.com/ShowCode.asp?ID=5810 or http://msdn.microsoft.com/en-us/library/tckcces5(v=vs.71).aspx

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups