Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用排序数据集WPF C#_C#_Sorting_Dataset - Fatal编程技术网

使用排序数据集WPF C#

使用排序数据集WPF C#,c#,sorting,dataset,C#,Sorting,Dataset,假设我有一个带有一个表的数据集。此表有两列:id、name。一些随机值: 0 John 1 Michael 2 Alice 3 Bob 我对数据集进行了排序: dataSet.Tables[0].DefaultView.Sort = "name"; 此外,我还有一个列表框来显示这个排序后的数据集: lb.ItemsSource = dataSet.Tables[0].DefaultView; 它显示了我想要的: Alice Bob John Michael 但是我

假设我有一个带有一个表的数据集。此表有两列:id、name。一些随机值:

  0 John
  1 Michael
  2 Alice
  3 Bob
我对数据集进行了排序:

dataSet.Tables[0].DefaultView.Sort = "name";
此外,我还有一个列表框来显示这个排序后的数据集:

lb.ItemsSource = dataSet.Tables[0].DefaultView;
它显示了我想要的:

 Alice
 Bob
 John
 Michael
但是我还有一个MouseDoubleClick事件,用于listBox显示一个带有id号的MessageBox。这是我的问题-我不明白为什么id号没有排序-例如Alice应该有“2”,但当我双击Alice时,消息框显示“0”(Bob 1,John 2,Michael 3)。那么,如何对数据集进行永久排序呢?(我知道这个例子有点奇怪,但我只是想找到一种方法)

更新

鼠标双击代码:

 MessageBox.Show(dataSet.Tables[0].Rows[lb.SelectedIndex][0].ToString());

您正在如下设置列表框源:

lb.ItemsSource = dataset.Tables[0].DefaultView;
MessageBox.Show(dataset.Tables[0].Rows[lb.SelectedIndex][0].ToString());
。。。您检索的ID如下所示:

lb.ItemsSource = dataset.Tables[0].DefaultView;
MessageBox.Show(dataset.Tables[0].Rows[lb.SelectedIndex][0].ToString());
检索ID时,您没有访问
DefaultView
。您直接访问
DataTable

试试这个:

MessageBox.Show(dataset.Tables[0].DefaultView[lb.SelectedIndex][0].ToString());

你能用代码
MouseDoubleClick
更新问题吗?非常感谢!工作完美!我知道这与DefaultView有关,但我不知道怎么做。谢谢你,你救了我的命:)