1

I have a TDBGrid and have a Client dataset associated through a TDataSource. When i try to re-arrange the columns of the grid programtically this works fine when there are one or more columns in the grid.

Lets say, i made all the columns visible to false. So, the grid does not have any columns yet. Now, when i set the Index of column at 11 to 0.

Columns[iColIndx].Index := iNewColIndex;
// Assume iColIndx = 11 and iNewColIndex = 0

Columns[iNewColIndex].Visible := True;
//Making the column visible

The above assignment of column index is not getting reflected in the Grid. If i try setting it for twice or thrice, then it is getting reflected. Am i missing anything?

Frank Schmitt
  • 30,195
  • 12
  • 73
  • 107
Rahul W
  • 833
  • 11
  • 26
  • 1
    Did you try setting `Visible := True;` first, and then setting the `Index`? It may be that the change in order is actually deferred until the grid is actually showing (didn't look at the code, but it's the first thing I'd suspect and the first change I'd try). – Ken White Nov 14 '11 at 20:47
  • I have tried the same, but no luck. The index of the column is not getting changed, but setting the visible is showing the column properly. In this case, if i show another column for which the index is less than the current showing, the order of the columns were getting changed if we see from the column index perspective. Is there any method to force the columns to change the index? I have even tried exposing the ColumnMoved method in the TDBGrid, but it is also of no use. – Rahul W Nov 14 '11 at 21:21
  • Did you try calling `Columns.Update` after changing the index? – Ken White Nov 14 '11 at 21:31
  • ahmm...i solved it...It was my mistake in the first place.. I have assigned the OnDrawDataCell for the grid for drawing the gradients, but i m not calling the DefaultDrawDataCell from there. Moreover, the DefaultDrawing was truned off. After setting the DefaultDrawing and calling the DefaultDrawDataCell from within the DrawDataCell event handler it is working fine now....Thank u very much for the support... – Rahul W Nov 14 '11 at 21:37
  • But interesting thing is that, i couldn't find any reference to setting the column index or updating the field from the DeafaultDrawDataCell...but this damn thing is working now... – Rahul W Nov 14 '11 at 21:45
  • 4
    if you have resolved it, then you should post your answer and accept it... – RBA Nov 14 '11 at 21:51
  • It is acceptable, and even encouraged to answer (and accept) your own quesitons: blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/ – Marjan Venema Nov 15 '11 at 07:08

1 Answers1

1

I solved it. I have assigned OnDrawDataCell of the grid for drawing the gradients, but I was not calling the DefaultDrawDataCell from there. Moreover, the DefaultDrawing property was turned off.

After setting DefaultDrawing and calling DefaultDrawDataCell from within the DrawDataCell event handler, it is working fine now.

NGLN
  • 43,011
  • 8
  • 105
  • 200
Rahul W
  • 833
  • 11
  • 26