如何使用行值对数据集进行排序? [英] How can I sort dataset using a row value ?

查看:101
本文介绍了如何使用行值对数据集进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集。

I have got a dataset.

DataSet dataSet2 = new DataSet();

            SqlConnection conStr2 = new SqlConnection(ConStr);

            string queryString = "Select * FROM JoinTable";

            SqlDataAdapter daGlobalClass2 = new SqlDataAdapter(queryString, conStr2);

            conStr2.Open();

            daGlobalClass2.Fill(dataSet2, "myDataTable");

            conStr2.Close();





Mytable有5列。其中一个是TableA。现在我想基于TableA列对数据集进行排序。如果TableA的值是最可用的,那么这些行将首先出现。



通过以下方式我可以对数据集进行排序。





Mytable has 5 columns. One of them are TableA. Now I want to sort the dataset on the basis of column TableA. If the value of TableA is "mostusedtable" then those row will come first.

In the following way I can sort dataset.

datatables1.DefaultView.Sort = "TableA";





但是如何根据TableA列的值对此数据集进行排序?



But how can I sort this dataset on the basis of the value of column TableA ?

推荐答案

请阅读文档: 直接在数据表中过滤和排序 [ ^ ]
Please, read the documentation: Filtering and Sorting Directly in Data Tables[^]


如果要对数据表进行排序:



If you want to sort the data table:

DataView view = datatables1.DefaultView;
view.Sort = "TableA";
DataTable sortedTable = view.ToTable();





如果你想对数据网格进行排序:





If you want to sort de datagrid:

    dataGrid1.Sort(dataGrid1.Colums[<index>]);
</index>


您好,



您需要在Select Query中添加另一个计算列







Hi,

You Need to add Another calculated Column in Select Query

like

string queryString = "
select (SELECT p.[Rows] FROM sys.tables t INNER JOIN   sys.indexes i ON t.OBJECT_ID = i.object_id INNER JOIN sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id WHERE t.NAME = tblRes.TableA AND i.OBJECT_ID > 255 AND i.index_id <= 1 ) as RowCount, tblRes.* from JoinTable as tblRes "





然后按RowCount列排序数据表





Then Sort That Data Table By RowCount Column

datatables1.DefaultView.Sort = "RowCount";







谢谢



Siva Rm K




Thanks

Siva Rm K


这篇关于如何使用行值对数据集进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆