你如何排序数据表给定的列和方向? [英] How do you Sort a DataTable given column and direction?
问题描述
我要度假胜地,在记忆的基础上,一列,方向是从未来的GridView的DataTable。该功能需要是这样的:
公共静态数据表度假区(DataTable的DT,串COLNAME,串方向)
{
数据表dtOut = NULL;
....
}
我需要帮助填写此功能。我想我可以使用SELECT语句,但我不知道怎么样。我不能点击,因为该浏览器的意见,但你能告诉我就地或新的DataTable的解决方案,其中之一。对于人们展示我的三分球,请,我需要一个类似于试制出codeD功能。
怎么样:
// ds.Tables [0] .DefaultView.Sort =au_fname DESC;
公共静态无效度假村(REF DataTable的DT,串COLNAME,串方向)
{
字符串SORTEX pression =的String.Format({0} {1},COLNAME,方向);
dt.DefaultView.Sort = SORTEX pression;
}
我认为方向是ASC或DESC和DT包含一个名为COLNAME列
公共静态数据表度假区(DataTable的DT,串COLNAME,串方向)
{
数据表dtOut = NULL;
dt.DefaultView.Sort = COLNAME ++方向;
dtOut = dt.DefaultView.ToTable();
返回dtOut;
}
或不产生dtOut
公共静态数据表度假区(DataTable的DT,串COLNAME,串方向)
{
dt.DefaultView.Sort = COLNAME ++方向;
dt的= dt.DefaultView.ToTable();
返回DT;
}
I need to resort, in memory, a DataTable based on a column and direction that are coming from a GridView. The function needs to look like this:
public static DataTable resort(DataTable dt, string colName, string direction)
{
DataTable dtOut = null;
....
}
I need help filling in this function. I think I can use a Select statement but I am not sure how. I can't click on Comments because of this browser but you can show me an in-place or new DataTable solution, either one. For the people showing me pointers, please, I need a coded function similar to the one prototyped.
How about:
// ds.Tables[0].DefaultView.Sort="au_fname DESC";
public static void Resort(ref DataTable dt, string colName, string direction)
{
string sortExpression = string.Format("{0} {1}", colName, direction);
dt.DefaultView.Sort = sortExpression;
}
I assume "direction" is "ASC" or "DESC" and dt contains a column named "colName"
public static DataTable resort(DataTable dt, string colName, string direction)
{
DataTable dtOut = null;
dt.DefaultView.Sort = colName + " " + direction;
dtOut = dt.DefaultView.ToTable();
return dtOut;
}
OR without creating dtOut
public static DataTable resort(DataTable dt, string colName, string direction)
{
dt.DefaultView.Sort = colName + " " + direction;
dt = dt.DefaultView.ToTable();
return dt;
}
这篇关于你如何排序数据表给定的列和方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!