Get the value of a cell when row changes in DataGridView

H

Henry Jones

I am using VB.NET VS 2005 and I have a datagridview bound to a table. This
table has rate information on bank loans. The fiels are ProductName,
MinDeposit, Rate, and PreviousRate

On the grid I have maybe 5 rows. I would like to change the MinDeposit
field and then call a routine to chane the ProductName. Here is an example
of the current data

'$5K', 5000, 5.34, 5.00 - This means that the Name is "$5K" a minimum
Deposit of $5,000, the current interest rate is 5.34% and the previous
interest rate is 5.00%

What I would like to do is when I leave each row, if the the MinDeposit
changes to 75000, I run a routine that changes the name to "$75K". The
routine works like it should.

When I leave each row I fire the dataviewgrid1.RowLeave event and I have the
following code:

Dim intDeposit As Single =
dgTiers.Rows(dataGridView1.CurrentRow.Index).Cells("MinDeposit").Value



This value doesn't change to the newly typed value in the cell. Why is
this? I thought if I am leaving the row, the value is still available.

How can I iterate through the grid and process each value?



Thanks for any help
 
R

RobinS

If I'm understanding correctly, you're trying to change how
the data is displayed, but not how it's stored, so if the
user puts in "75000", you want it displayed as "75K", but
stored as 75000. And when it's brought in from the database,
you want to convert it from 75000 to 75K. When it's put back,
you want to translate it the other way.

Try capturing the CellFormatting and CellParsing events.

CellFormatting happens between the data source and the screen.
CellParsing happens between the screen and the data source.

Try this:

AddHandler myGrid.CellFormatting, AddressOf OnCellFormatting
AddHandler myGrid.CellParsing, AddressOf OnCellParsing

Private Sub OnCellFormatting(ByVal object as Sender, _
ByVal e as DataGridViewCellFormattingEventArgs)

If e.ColumnIndex = myGrid.Columns("columnIcareAbout") Then
'setting this to true signals the grid that this
' column is being dynamically updated. If you don't
' do this, you will get an infinite loop if you have
' also set the column to have its size automatically
' determined.
e.FormattingApplied = True

'get the row being populated; format this cell
Dim row as DataGridViewRow = myGrid.Rows(e.RowIndex)
If e.Value = "75000" Then
e.Value = "75K"
End If
End If
End Sub

Private Sub OnCellParsing(ByVal object as Sender, _
ByVal e as DataGridViewCellParsingEventArgs)

If e.ColumnIndex = myGrid.Columns("columnIcareAbout") Then
'get the row being populated; format this cell
Dim row as DataGridViewRow = myGrid.Rows(e.RowIndex)
If e.Value = "75K" Then
e.Value = "75000"
End If
End If
End Sub

That should get you part of the way there.
Robin S.
 
H

Henry Jones

Thanks, this is what I was looking for.


RobinS said:
If I'm understanding correctly, you're trying to change how
the data is displayed, but not how it's stored, so if the
user puts in "75000", you want it displayed as "75K", but
stored as 75000. And when it's brought in from the database,
you want to convert it from 75000 to 75K. When it's put back,
you want to translate it the other way.

Try capturing the CellFormatting and CellParsing events.

CellFormatting happens between the data source and the screen.
CellParsing happens between the screen and the data source.

Try this:

AddHandler myGrid.CellFormatting, AddressOf OnCellFormatting
AddHandler myGrid.CellParsing, AddressOf OnCellParsing

Private Sub OnCellFormatting(ByVal object as Sender, _
ByVal e as DataGridViewCellFormattingEventArgs)

If e.ColumnIndex = myGrid.Columns("columnIcareAbout") Then
'setting this to true signals the grid that this
' column is being dynamically updated. If you don't
' do this, you will get an infinite loop if you have
' also set the column to have its size automatically
' determined.
e.FormattingApplied = True

'get the row being populated; format this cell
Dim row as DataGridViewRow = myGrid.Rows(e.RowIndex)
If e.Value = "75000" Then
e.Value = "75K"
End If
End If
End Sub

Private Sub OnCellParsing(ByVal object as Sender, _
ByVal e as DataGridViewCellParsingEventArgs)

If e.ColumnIndex = myGrid.Columns("columnIcareAbout") Then
'get the row being populated; format this cell
Dim row as DataGridViewRow = myGrid.Rows(e.RowIndex)
If e.Value = "75K" Then
e.Value = "75000"
End If
End If
End Sub

That should get you part of the way there.
Robin S.
 
R

RobinS

You're welcome, I'm glad it was helpful.

I learned a lot of neat stuff from the book
"Data Binding with Windows Form 2.0" by Brian Noyes.
It has a large chapter on the DataViewGrid control.

Robin S.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top