如何对数据集中的数据进行排序 [英] How to sort data in DataSet
本文介绍了如何对数据集中的数据进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我试图通过此代码对DataSet中的数据进行排序,但失败了,为什么?
Hi guys,
I tried to sort data in DataSet by this code but it failed, why ?
topicsEditDataSet.Tables[0].DefaultView.Sort = "TopicID DESC";
任何机构都有想法吗?
any body has any idea please ?
推荐答案
您不应使用DefaultView属性,而应创建一个新的DataView.创建新文件的原因是;
将属性设置为"DefaultView"时,它不起作用是因为您必须访问DefaultView而不是DataSet才能获得排序的结果.操作DefaultView通常不是一个好主意.
基本上,创建DataView类的新实例,并在设置排序后将其传递给它,然后使用它.
You shouldn''t use the DefaultView property instead you should create a new DataView. The reason for creating a new is;
it doesn''t work when setting the property to "DefaultView" is because you have to access the DefaultView, not the DataSet to get the sorted results. Manipulating the DefaultView is usually not a good idea.
Basically, create a new instance of the DataView class and pass that around after you set the sort, and work with that.
//open the connection
sqlConnection.Open();
//create a new sqladapter and set its command object with our sqlcommand
SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlCommand);
//create a new dataset to hold our data
DataSet ds = new DataSet();
//fill the dataset with the result of our query from the specified command
sqlAdapter.Fill(ds);
//create a new dataview from our table in our dataset at index 0
DataView dv = new DataView(ds.Tables[0]);
//apply our row filter. get only records with a lastname of ''Callahan''
dv.RowFilter = "Name = ''Brown''";
//close our connection
sqlConnection.Close();
//return our dataview
return dv;
供参考,请参阅此链接;
排序数据表 [
for a reference see this link;
Sort datatable[^]
Link1 [链接2 [ ^ ]可能会为您提供帮助.
Link1[^] and Link2[^] might help you.
这篇关于如何对数据集中的数据进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文