Ago
Ago

Reputation: 765

TDBGrid Disable autosort after insert

This is my problem : I want to disable tdbgrid auto-sort after inserting a new record. I want tdbgrid perform sort ONLY IF the user ask them (clicking the column title).

I have a table named f4400 and has two columns, sec_code and sec_desc(both are varchar). Let say that may table contains these records:

sec_code          sec_desc
1 , section 1     xxxxxxxxxxxxxxxxx
2 , section 2     xxxxxxxxxxxxxxxxx
3 , section 3     xxxxxxxxxxxxxxxxx

when I query f4400 and put it in the tdbgrid, both looked the same. When I insert a new record, let say ("11", "section 11"), my grid would now look like these :

sec_code          sec_desc
1 , section 1     xxxxxxxxxxxxxxxx 
2 , section 2     xxxxxxxxxxxxxxxx 
3 , section 3     xxxxxxxxxxxxxxxx 
11 , section 11   xxxxxxxxxxxxxxxx

so far so good. now, I will sort my grid by sec_code desc. My tdbgrid will now look like this:

3 , section 3     xxxxxxxxxxxxxxxx
2 , section 2     xxxxxxxxxxxxxxxx
1 , section 1     xxxxxxxxxxxxxxxx
11 , section 11   xxxxxxxxxxxxxxxx

now, I will insert a new record let say ("21", "section 21"), tdbgrid will gave me a result of :

sec_code          sec_desc
3 , section 3     xxxxxxxxxxxxxxxx
2 , section 2     xxxxxxxxxxxxxxxx
21 , section 21   xxxxxxxxxxxxxxxx
1 , section 1     xxxxxxxxxxxxxxxx
11 , section 11   xxxxxxxxxxxxxxxx

The result I wanted is :

sec_code          sec_desc
3 , section 3     xxxxxxxxxxxxxxxx
2 , section 2     xxxxxxxxxxxxxxxx
1 , section 1     xxxxxxxxxxxxxxxx
11 , section 11   xxxxxxxxxxxxxxxx
21 , section 21   xxxxxxxxxxxxxxxx

The row position of "sec_code 21" is my problem. I inserted it at the bottom of the tdbgrid so i want to see "sec_code 21" at the bottom of the grid.

I tried using tdbgrid.datasource.dataset.sort := '' but it will give a an output like this:

sec_code          sec_desc
1 , section 1     xxxxxxxxxxxxxxxx
11 , section 11   xxxxxxxxxxxxxxxx
2 , section 2     xxxxxxxxxxxxxxxx
21 , section 21   xxxxxxxxxxxxxxxx
3 , section 3     xxxxxxxxxxxxxxxx

I tried using tdbgrid.datasource.dataset.IndexFielName := '' but it will give a an output like this:

sec_code          sec_desc
1 , section 1     xxxxxxxxxxxxxxxx
2 , section 2     xxxxxxxxxxxxxxxx
3 , section 3     xxxxxxxxxxxxxxxx
11 , section 11   xxxxxxxxxxxxxxxx
21 , section 21   xxxxxxxxxxxxxxxx

The position of "sec_code 21" is not my problem anymore, but, the positions of "sec_code 1,2,3 and 11" are not in there positions i wanted. i sorted sec_code 1,2,3 and 11 descending so i want to see sec_code 1,2,3 and 11(not including sec_code 21) to be sorted descending.

hope you understand my problem and would try to suggest how can i give solution to that. It's been headache to me for about 3-4 nights.

Thanks in advance

Upvotes: 0

Views: 1042

Answers (1)

Ken White
Ken White

Reputation: 125707

The sort order in TDBGrid is determined by the index order in the underlying dataset.

Your sec_code column is apparently a string/char, and is therefore being sorted as one by the index you have on that column; that explains why the sort order you want isn't the one you're getting.

If you append a row at the end of the dataset, and want it to stay there, you need to set the IndexName to '' (meaning there's no index order).

If you want them to sort in actual numeric order (1, 2, 3, 11, 21), you need to convert them to numeric.

The final output you're looking for makes no sense, though. You're looking for a descending sort of the values 1, 2, 3, 11, 21 and expecting it to come out 3, 2, 1, 11, 21. There's no logic there. A valid expectation on numeric types 2ould be 21, 11, 3, 2, 1; a valid expectation for character types would be 3, 21, 2, 11, 1. There's no combination that would give you the results you're looking for except for an actual insertion order, where there is no index in effect and you specifically added the rows in the 3, 2, 1, 11, 21 sequence.

Upvotes: 2

Related Questions