Solved

easy dataGrid question

Posted on 2006-10-23
14
274 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
ID: 17788394
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
ID: 17788471
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
ID: 17788574
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:sacul
ID: 17788913
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
ID: 17788954
amend the "Handles DataGridView.CellContentClick" for them name of your datagrid.
0
 
LVL 1

Author Comment

by:sacul
ID: 17789165
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
ID: 17789474
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
 
LVL 1

Author Comment

by:sacul
ID: 17790062
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
ID: 17790085
this is very strange, although I'm using 2005 but should be the same ...
0
 
LVL 34

Expert Comment

by:Sancler
ID: 17790723
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
ID: 17791333
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
ID: 17792326
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
ID: 17796474
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
ID: 17796539
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

697 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