By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,916 Members | 1,068 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,916 IT Pros & Developers. It's quick & easy.

Get the value of a cell when row changes in DataGridView

P: n/a
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
Nov 9 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

"Henry Jones" <he***@yada.comwrote in message
news:Om**************@TK2MSFTNGP04.phx.gbl...
>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


Nov 9 '06 #2

P: n/a
Thanks, this is what I was looking for.
"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:Hq******************************@comcast.com. ..
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.

"Henry Jones" <he***@yada.comwrote in message
news:Om**************@TK2MSFTNGP04.phx.gbl...
>>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).Cell s("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



Nov 10 '06 #3

P: n/a
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.

"Henry Jones" <he***@yada.comwrote in message
news:O4**************@TK2MSFTNGP03.phx.gbl...
Thanks, this is what I was looking for.
"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:Hq******************************@comcast.com. ..
>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.

"Henry Jones" <he***@yada.comwrote in message
news:Om**************@TK2MSFTNGP04.phx.gbl...
>>>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).Cel ls("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




Nov 10 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.