且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从字符串获取词法值

更新时间:2023-11-02 23:18:58

为什么不使用DataSet中的排序工具?这样,您不必移动任何行...

MSDN可以帮助:数据集中的筛选和排序 [ ^ ]

Hello Everybody!

I would like to sort a table in a specific way. Let us name the table "Customers" for this purpose. Customer gets its data from Northwind-db.

The way I like to sort the table is the following. First I would like to move marked rows on the top of the table (index: 0, 1, 2, etc). The rest of the table, i.e. the unmarked rows, should be ordered alphabetically like in a lexicon according a vch-column.

In my first try I ordered the table alphabetically and tried to move the marked rows on the top by removing those and insert them on the top.

DataRow selectedRow = sourceTable.Rows.Find(key);
    DataRow newRow = sourceTable.NewRow();
    newRow.ItemArray = selectedRow.ItemArray;
    sourceTable.Rows.Remove(selectedRow);
    sourceTable.Rows.InsertAt(newRow, pos);


This works but caused a problem. I have some child tables to the table "Customers" and each time I remove a row in "Customers" to insert it on the top its child rows are removed as well. I could not find any way to suspend the relations for this piece of code. There are BeginEdit and EndEdit, but those functions just suspend editing conditions not deleting condition. The same is the case for this:

dataSet1.EnforceConstraints = false;
// Perform some operations on the dataset
dataSet1.EnforceConstraints = true;



Maybe I can add an extra sort column to the table "Customers" where each row has some integer value and the marked rows has the least values. Those marked rows would then end up on the top if I set the Sort-property of the DefaultView-object.
The problem now is that I need a lexical value for the unmarked rows. I could not find some function giving me a value for a string. Just compare two strings with string.Compare does not fit my purpose. I would need something like this:

Int iAtanasia = GetInt("Atanasia");
// iAtanasia == 10
    Int iPeter = GetInt("Otto");
//iPeter == 50;



Do someone have a good suggestion how I could solve this. I’m open to other solutions to make the described sorting if you know a way.

Thanxs
/Sahit

Why don''t you use the sort facilities in the DataSet? That way, you don''t have to move any rows...

MSDN can help: Filtering and Sorting in Datasets[^]