Solved

easy dataGrid question

Posted on 2006-10-23
14
282 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 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

724 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