Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

easy dataGrid question

Posted on 2006-10-23
14
Medium Priority
?
292 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 800 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

618 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