easy dataGrid question

Hello Experts,

I have a question about datagrids. I made the datagrid editable for the user so they can edit/enter data directly into the grid. I then read the grid into a list that I use to update a database. This all works fine. My question though is, is there a way to dynamically update a cell in a dg? lets say that I have 4 cols and I add 3 number on the first 3, is there a way to get the total in the 4th cell as I am entering the data?

so lets say I enter 5 on the first col, then col #4 has 5 in it as I am tab to col #2, where I enter 3, and now the 4th col is updated to 8.

I need to use it to update tax values as numbers are inserted into the grid. Is there a way to do this?

Thank you,
Zac
LVL 1
saculAsked:
Who is Participating?
 
SanclerConnect With a Mentor Commented:
To determine the cell that has now been selected you use

   myDataGrid.CurrentCell

That, in turn, exposes .RowNumber and .ColumnNumber.

A problem is that you probably really want to know not the cell that has now been selected, but the cell that has just been left.  If that is the case it means that, as the last step in any code in this sub, you need to save the current cell's rownumber and columnnumber so that, next time the sub is visited, you know what the last cell was.  Something on these lines

   Private Sub CellChanged (ByVal sender As Object, ByVal e As System.EventArgs) Handles myDataGrid.CurrentCellChanged
       Static LastRow As Integer = -1
       Static LastCol As Integer = -1
       SelectCase LastCol
          Case 0, 1, 2 'being a column when you want to adjust the total
             'Code to adjust the total in column 3 on LastRow
       End Select
       LastRow = myDataGrid.CurrentCell.RowNumber
       LastCol = myDataGrid.CurrentCell.ColumnNumber
   End Sub

>>
Title: easy dataGrid question
<<

;-)

Roger
0
 
Kinger247Commented:
Only by code by calculating what you need the cell to be and update it like:

DataGridView.Rows(4).Cells("VAT").Value = snfNewVAT
0
 
saculAuthor Commented:
Hello,

Thank you for your response. How can that be automated though? I guess it has to be handled as I am tabbing the datagrid. This is the thing I am not too sure about. I tried to capture the col number through the datagrid as I was navigating but it keeps it at a value of 0 even though I tab through the grid. How can I update it while tabbing?

Thank you,
Zac
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
Kinger247Connect With a Mentor Commented:
I take it your using a textbox to update the values in the grid ?
If so, when you update the cell behind the textbox ... update the total cell.

You can get the column and row to update in the following event:

    Private Sub DataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView.CellContentClick
        iCol = e.RowIndex
        iRow = e.ColumnIndex
    End Sub
0
 
saculAuthor Commented:
Hello,

When I try the code you posted I get an error.

"Private Sub DataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView.CellContentClick"

System.Windows.Forms.DataGridViewCellEventArgs - this gives and error as not being defined.

dgRecevingGrid.CellContentClick - CellContentClick  cannot be found.

Do I have to set a ref or import something for that to work?

Thank you,
Zac
0
 
Kinger247Commented:
amend the "Handles DataGridView.CellContentClick" for them name of your datagrid.
0
 
saculAuthor Commented:
Hello,

I did have it changed before CellContentClick. The event can't be found. I see the list of all the other events but not the 'CellContentClick' one. Same with 'System.Windows.Forms.DataGridViewCellEventArgs', it says it is not declared.

Thank you,
Zac
0
 
Kinger247Commented:
Ok, try this example. when you amend a cell and tab to the next, it will add a percentage sign after the string in the cell you've just tabbed from.
You can amend this code so that it will go and calculate the total , etc.

    Private Sub DataGridView1_CellValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValidated
        Dim CellVal As String = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value

        DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value &= "%"
    End Sub

again change the datagrids name.
0
 
saculAuthor Commented:
Hello,

I am starting to think I am doing somethng wrong. I keep getting errors on this,

.CellValidated

it tells me that the even does not exist. I posted it on imageshack so you can see what I mean,

http://img344.imageshack.us/my.php?image=erroryf4.jpg

I am using VB.NET 2003.

Thank you,
Zac
0
 
Kinger247Commented:
this is very strange, although I'm using 2005 but should be the same ...
0
 
SanclerCommented:
The DataGridView is new in 2005 and is a radically different animal from the DataGrid in 2003.

The event you will need is the datagrid's .CurrentCellChanged.

Roger
0
 
saculAuthor Commented:
Hello,

ahh ok.. that explains it. So using CurrentCellChanged what do I pass instead of

'ByVal e As System.Windows.Forms.DataGridViewCellEventArgs'

DataGridViewCellEventArgs is not a valid member.

Thank you,
Zac
0
 
saculAuthor Commented:
Hello,

Perfect. With a little alteration I was able to get it to work. I kept getting errors on the row -1 on the first one but I fould that my Row can stay the way it is all I need to -1 is the col number.

I will split the pts since the solution that Kinger247 posted also work if you run 05. just in case someone needs to do the same in 05.

Thanks a mill,
Zac
0
 
saculAuthor Commented:
I felt a little stingy so I signed up for premium services and up'ed the pts a little

Thanks again,
Zac
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.