4

We display our data on datagrids, bound to a dataset, which is in turn fed from a Progress database on the server. During processing, we need to make a change to the data-set and refresh it's value from the server. So far, all well and good and no problems.

The problem is that when we come back with the new data, we want the selection in the datagrid to remain on the same row it was on before. We've managed this with the following code:

int iPostingPos = dgridPostings.CurrentRow.Index;

// process data on server
dataContTranMatch.RunBoProcedure(dataContTranMatch.BoProcedure, 
transactionMatchingDataSet);
// Reload Data
LoadData();

if (iPostingPos > ttPOSTingsRowBindingSource.Count)
{
    iPostingPos = ttPOSTingsRowBindingSource.Count;
}
if (ttPOSTingsRowBindingSource.Count > 0)
{
    ttPOSTingsRowBindingSource.Position = iPostingPos;
    dgridPostings.Rows[iPostingPos].Selected = true;
}

This works, but we get the selected line jumping about on the screen, which is really annoying the users.

For example, if you select row 7, then run this code, you have row 7 selected, selection then jumps to row 0, then jumps back to row 7. This isn't acceptable.

In an attempt to fix this, we've tried enclosing the above code in the following additional lines:

chTableLayoutPanel1.SuspendLayout();

*DO CODE*

chTableLayoutPanel1.ResumeLayout();

But this didn't help.

So far, the most acceptable solution that we've been able to reach is to change the colour on the selection so that you can't see it, letting it leap about and then putting the colours back as they should be. This makes the flicker more acceptable.

dgridPostings.RowsDefaultCellStyle.SelectionBackColor = 
SystemColors.Window;
dgridPostings.RowsDefaultCellStyle.SelectionForeColor = 
SystemColors.ControlText;

DO CODE

dgridPostings.RowsDefaultCellStyle.SelectionBackColor = 
SystemColors.Highlight;
dgridPostings.RowsDefaultCellStyle.SelectionForeColor = 
SystemColors.HighlightText;

We beleive that the issue is caused by the binding source being temporarily empty as the dataset is refreshed, we then re-navigate one it's got data in it again.

Can anyone offer any ideas on how we can prevent this unpleasent flicker from occuring?

Many thanks

Colin

Colin
  • 1,141
  • 1
  • 9
  • 9
  • 1
    Instead of changing the selection colors, can you temporarily set the selected index to -1 (no selection)? – mellamokb Nov 09 '11 at 11:59
  • Anything's possible ;~). However, would that not give you problems when you try to re-locate the line, because you've not got anything selected? – Colin Nov 09 '11 at 15:12

1 Answers1

2

It may be a bit heavy handed but one option would be to suspend painting of the control. A user asked how to achieve this here: How Do I Suspend Painting For a Control and Its' Children. I've used the selected answer there to achieve something similar.

Community
  • 1
  • 1
robowahoo
  • 1,259
  • 9
  • 10
  • Actually I think that may be what we were trying to do with the SuspendLayout. We'll look into that one a bit. Thank you. – Colin Nov 09 '11 at 15:10
  • Spot on. We needed to add the interop to the using list, but other than that, we could take that solution 'straight out of the box' and it worked great. Many, many thanks! – Colin Nov 09 '11 at 15:32
  • You're welcome, If you think you might do this in a few places I would recommend making the Suspend / Resume extension methods to the Control class. I believe @Zach Johnson suggested that in the comments – robowahoo Nov 09 '11 at 15:35
  • We tried this solution, but in the end didn't like the way that it looked (worked fine, just not quite the effect that we were after). We ended up going with the invisible line aproach (above). Thank you all for your help. – Colin Nov 15 '11 at 15:27