請問STRINGGRID可以做排序嗎? |
尚未結案
|
answer
一般會員 ![]() ![]() 發表:17 回覆:10 積分:5 註冊:2004-05-14 發送簡訊給我 |
|
answer
一般會員 ![]() ![]() 發表:17 回覆:10 積分:5 註冊:2004-05-14 發送簡訊給我 |
|
lijenyun
一般會員 ![]() ![]() 發表:3 回覆:19 積分:9 註冊:2003-06-22 發送簡訊給我 |
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { StringGrid1->Cells[1][1] = "Acer"; StringGrid1->Cells[1][2] = "IBM"; StringGrid1->Cells[1][3] = "Compaq"; StringGrid1->Cells[1][4] = "BenQ"; StringGrid1->Cells[2][1] = "1000"; StringGrid1->Cells[2][2] = "5000"; StringGrid1->Cells[2][3] = "3000"; } //--------------------------------------------------------------------------- void __fastcall TForm1::StringGrid1MouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { int ACol, ARow; StringGrid1->MouseToCell(X, Y, ACol,ARow); GetSort(StringGrid1, ACol); } //--------------------------------------------------------------------------- void TForm1::GetSort(TStringGrid *GenStrGrid, int ThatCol) { const char TheSeparator = '@'; // Define the Separator int CountItem = GenStrGrid->RowCount; // Give the number of rows in the StringGrid TStringList *MyList = new TStringList; //Create the List try { MyList->Sorted = False; for (int I=1; I<=CountItem - 1;I ) { MyList->Add(GenStrGrid->Rows[I]->Strings[ThatCol] TheSeparator GenStrGrid->Rows[I]->Text); } MyList->Sort(); //Sort the List for (int K = 1 ; K<=MyList->Count;K ) { String MyString = MyList->Strings[(K - 1)]; //Take the String of the line (K – 1) int ThePosition = MyString.Pos(TheSeparator); //Find the position of the Separator in the String //Eliminate the Text of the column on which we have sorted the StringGrid} String TempString = MyString.SubString(ThePosition 1,MyString.Length()); MyList->Strings[(K - 1)] = ""; MyList->Strings[(K - 1)] = TempString; } for (int J = 1; J<=CountItem - 1;J ) GenStrGrid->Rows[J]->Text = MyList->Strings[(J - 1)]; //Refill the StringGrid } __finally { delete MyList; } }參考網址 http://www.undu.com/Articles/990502f.html 發表人 - lijenyun 於 2004/05/19 23:24:56 發表人 - lijenyun 於 2004/05/19 23:26:17 |
answer
一般會員 ![]() ![]() 發表:17 回覆:10 積分:5 註冊:2004-05-14 發送簡訊給我 |
|
harrypp
中階會員 ![]() ![]() ![]() 發表:7 回覆:124 積分:61 註冊:2006-08-31 發送簡訊給我 |
用這方法會比較有效率
<textarea class="cpp" rows="10" cols="60" name="code"> /////////////////////////////////////////////// ;;共12個欄位 20筆資料 ;;若想改第三個欄位為鍵值 for (I=0;I<20;I ) { AnsiString* strData, Key; strData=new AnsiString[12]; strData[I]=..... //輸入資料 Key=strData[5]; //以第五個欄位為鍵值 MyList->AddObject(Key, (TObject *)strData); } MyList->Sort(); //排序 /////////////////////////////////////////////// ;;若想改第三個欄位做排序 for (I=0;I<20;I ) { (TObject *)sTemp=MyList->Objects[I]; MyList->Strings[I]=sTemp[3]; //以第三個欄位為鍵值 } MyList->Sort(); //排序 /////////////////////////////////////////////// ;;讀出排序後的資料 for (I=0;I<20;I ) { (TObject *)sTemp=MyList->Objects[I]; for (J=0; J<12; J ) MyList->Strings[I]=sTemp[J]; } </textarea> |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |