Solved

easy dataGrid question

Posted on 2006-10-23
14
243 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:sacul
  • 7
  • 5
  • 2
14 Comments
 
LVL 10

Expert Comment

by:Kinger247
Comment Utility
Only by code by calculating what you need the cell to be and update it like:

DataGridView.Rows(4).Cells("VAT").Value = snfNewVAT
0
 
LVL 1

Author Comment

by:sacul
Comment Utility
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
 
LVL 10

Assisted Solution

by:Kinger247
Kinger247 earned 100 total points
Comment Utility
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
 
LVL 1

Author Comment

by:sacul
Comment Utility
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
 
LVL 10

Expert Comment

by:Kinger247
Comment Utility
amend the "Handles DataGridView.CellContentClick" for them name of your datagrid.
0
 
LVL 1

Author Comment

by:sacul
Comment Utility
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
 
LVL 10

Expert Comment

by:Kinger247
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 1

Author Comment

by:sacul
Comment Utility
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
 
LVL 10

Expert Comment

by:Kinger247
Comment Utility
this is very strange, although I'm using 2005 but should be the same ...
0
 
LVL 34

Expert Comment

by:Sancler
Comment Utility
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
 
LVL 1

Author Comment

by:sacul
Comment Utility
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
 
LVL 34

Accepted Solution

by:
Sancler earned 200 total points
Comment Utility
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
 
LVL 1

Author Comment

by:sacul
Comment Utility
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
 
LVL 1

Author Comment

by:sacul
Comment Utility
I felt a little stingy so I signed up for premium services and up'ed the pts a little

Thanks again,
Zac
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now