Link to home
Start Free TrialLog in
Avatar of David Svedarsky
David SvedarskyFlag for United States of America

asked on

How to maintain row position in datagridview when cells are edited

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.
Avatar of surajguptha
surajguptha
Flag of United States of America image

Are you using a windows data grid or a web datagrid?
Avatar of David Svedarsky

ASKER

I am using a windows application.
Avatar of Bob Learned
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
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"

ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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