?
Solved

How to maintain row position in datagridview when cells are edited

Posted on 2007-10-14
7
Medium Priority
?
702 Views
Last Modified: 2013-11-26
Using a bound datagridview.

I need sample code for a datagridview to maintain row position after editing column cells.
After filling about 20 rows with data, the datagridview moves up several rows when a cell is clicked or edited.
0
Comment
Question by:dave_sky
  • 3
  • 3
7 Comments
 
LVL 21

Expert Comment

by:surajguptha
ID: 20075801
Are you using a windows data grid or a web datagrid?
0
 

Author Comment

by:dave_sky
ID: 20075825
I am using a windows application.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20077366
Dave,

I don't understand what your code is doing, and what you need it to do.  You could keep track of the current cell address, and reset it back:

Me.DataGridView1.CurrentCell = Me.DataGridView1(oldColumnIndex, oldRowIndex)

Bob
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 

Author Comment

by:dave_sky
ID: 20098637
Bob,

Here is the code below in form_load that is causing the problem.
There are several self calculating columns, that when edited causes the datagridview to cycle or change row position (several rows)

Ay ideas where I could move the code...

  'Compute(Unit Price)
      Dim dc As New DataColumn
      dc.Expression = "(UnitPrice*Markup)+Credit+Labor"
      dc.ReadOnly = True
      dc.ColumnName = "UPGRADE_PRICE"
      dc.DataType = System.Type.GetType("System.Decimal")
      MonarchSQLDataSet.Tables("tblUpgradesDetail").Columns.Add(dc)

      'Compute(Extension)
      dc = New DataColumn()
      dc = New DataColumn()
      dc.Expression = "(UPGRADE_PRICE*Quantity)"
      dc.ReadOnly = True
      dc.ColumnName = "EXT"
      dc.DataType = System.Type.GetType("System.Decimal")
      MonarchSQLDataSet.Tables("tblUpgradesDetail").Columns.Add(dc)

      ' Old SubTotal before adding Sales Tax.
      dc = New DataColumn("SalesTax", System.Type.GetType("System.Decimal"), "(Sum(Child.EXT) + IsNull(Shipping,0)) * IsNull(TaxRate/100,0)")
      dc.ReadOnly = True
      MonarchSQLDataSet.Tables("tblUpgrades").Columns.Add(dc)
      txtSalesTax.DataBindings.Add(New Binding("Text", TblUpgradesBindingSource, "SalesTax", True))
      txtSalesTax.DataBindings("Text").FormatString = "C"

      'Compute SubTotal Sum(Child.EXT) * (1 + IsNull(TAX/100,0))
      dc = New DataColumn("ST", System.Type.GetType("System.Decimal"), "Sum(Child.EXT)")
      dc.ReadOnly = True
      MonarchSQLDataSet.Tables("tblUpgrades").Columns.Add(dc)
      SubTotalTextBox.DataBindings.Add(New Binding("Text", TblUpgradesBindingSource, "ST", True))
      SubTotalTextBox.DataBindings("Text").FormatString = "C"

      'Compute Total
      dc = New DataColumn("TOT", System.Type.GetType("System.Decimal"), "ST+isnull(Shipping,0)+isnull(SalesTax,0)")
      dc.ReadOnly = True
      MonarchSQLDataSet.Tables("tblUpgrades").Columns.Add(dc)
      TotalTextBox.DataBindings.Add(New Binding("Text", TblUpgradesBindingSource, "TOT", True))
      TotalTextBox.DataBindings("Text").FormatString = "C"

0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 20101571
Dave,

I don't see where the current row or current cell is changed, but you could always store the current cell, do the processing, and then set the current cell back to the previous value:

Dim previousCell As DataGridViewCell = Me.DataGridView1.CurrentCell

' Do some processing

Me.DataGridView1.CurrentCell =  previousCell

Bob

0
 

Author Comment

by:dave_sky
ID: 20102338
Bob,
I tried the code example with no luck in Form_Load and MyGrid_CellEndEdit.
It looks like I am going to have to try and figure out how to take the column calculations out of Form_Load and move them to a Button_Click event and only load the data in Form_Load.
I assume that this is beyond the scope of EE.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20102888
I have seen some wild and strange things over the years, so I don't know what EE's "scope" is really limited to, other than the membership guidelines (like doing homework, flaming, etc.).

It is difficult to say with any certainty what the "best" course of action is without understanding your process better.

Bob
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Screencast - Getting to Know the Pipeline
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

809 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