how to controll cells inside Table1DataGridView
-
no I need to move text from cell to another cell on the same row once column 0 is checked :confused:
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
-
no I need to move text from cell to another cell on the same row once column 0 is checked :confused:
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?
-
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?
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
-
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
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.
-
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.
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 cellDim 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
-
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 cellDim 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
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 cellDim 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
-
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 cellDim 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
-
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?
-
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?
"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
-
"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
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
-
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
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:
-
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:
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