如何在 Telerik RadGrid ASP.NET Ajax 上按数字排序而不是按字符串排序?
我有一个 RadGrid,其中有一个列,损失金额,我希望能够对其进行排序。目前它确实排序,但仅按字符串值排序。我的列类型为 GridNumericColumn。我该怎么做呢?
下面列出了我的代码。我还设置了列文本的格式以显示货币。我注意到的一件事是我从 Format 方法返回一个字符串值。难道是这个原因吗?
列:
<rad:GridNumericColumn DataField="Loss Amount"
UniqueName="Loss Amount"
HeaderText="Loss Amount"
SortExpression="Loss Amount" \>
<HeaderStyle Width="140">
<ItemStyle Width="140" HorizontalAlign="Right"ForeColor="Maroon" />
</rad:GridNumericColumn>`
NeedDataSource 事件:
protected void grdCustomerAssignments_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
GetAssignments();
grdCustomerAssignments.DataSource = Assignments;
}
private void GetAssignments()
{
if (Assignments == null)
Assignments = new DataTable();
SPList list = SPContext.Current.Web.Lists["Listings"];
SPView view = GetCustomerView();
SPListItemCollection items = list.GetItems(view);
Assignments = items.GetDataTable();
foreach (DataColumn column in Assignments.Columns)
{
column.ColumnName = XmlConvert.DecodeName(column.ColumnName);
}
Assignments.AcceptChanges();
}
ItemDataBound 事件:
protected void grdCustomerAssignments_ItemDataBound(object sender, GridItemEventArgs e)
{
FormatCurrency(item["Loss Amount"].Text);
}
protected string FormatCurrency(string text)
{
if (text.Length > 3)
{
string result = String.Empty;
char[] tmpArray = text.ToCharArray();
Array.Reverse(tmpArray);
string tmpString = new String(tmpArray);
while (tmpString.Length > 3)
{
string threeChars = tmpString.Substring(0, 3);
result = String.Concat(result, threeChars, ",");
tmpString = tmpString.Remove(0, 3);
}
result = String.Concat(result, tmpString);
tmpArray = result.ToCharArray();
Array.Reverse(tmpArray);
text = new String(tmpArray);
}
return String.Concat("$ ", text);
}
I have a RadGrid that has a column, Loss Amount, which I would like to be able to sort. Currently it does sort, but only by string value. I have the column type as GridNumericColumn. How can I do it?
My code is listed below. I am also formatting the column text to show the currency. One thing I did notice is that I am returning a string value from the Format method. Could this be the reason?
Column:
<rad:GridNumericColumn DataField="Loss Amount"
UniqueName="Loss Amount"
HeaderText="Loss Amount"
SortExpression="Loss Amount" \>
<HeaderStyle Width="140">
<ItemStyle Width="140" HorizontalAlign="Right"ForeColor="Maroon" />
</rad:GridNumericColumn>`
NeedDataSource Event:
protected void grdCustomerAssignments_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
GetAssignments();
grdCustomerAssignments.DataSource = Assignments;
}
private void GetAssignments()
{
if (Assignments == null)
Assignments = new DataTable();
SPList list = SPContext.Current.Web.Lists["Listings"];
SPView view = GetCustomerView();
SPListItemCollection items = list.GetItems(view);
Assignments = items.GetDataTable();
foreach (DataColumn column in Assignments.Columns)
{
column.ColumnName = XmlConvert.DecodeName(column.ColumnName);
}
Assignments.AcceptChanges();
}
ItemDataBound Event:
protected void grdCustomerAssignments_ItemDataBound(object sender, GridItemEventArgs e)
{
FormatCurrency(item["Loss Amount"].Text);
}
protected string FormatCurrency(string text)
{
if (text.Length > 3)
{
string result = String.Empty;
char[] tmpArray = text.ToCharArray();
Array.Reverse(tmpArray);
string tmpString = new String(tmpArray);
while (tmpString.Length > 3)
{
string threeChars = tmpString.Substring(0, 3);
result = String.Concat(result, threeChars, ",");
tmpString = tmpString.Remove(0, 3);
}
result = String.Concat(result, tmpString);
tmpArray = result.ToCharArray();
Array.Reverse(tmpArray);
text = new String(tmpArray);
}
return String.Concat("$ ", text);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
另请检查源字段是否为数字数据类型,而不是字符串。您可以轻松测试启用编辑功能 - 如果您编辑某个项目,并且尝试将字符串值分配给 Telerik 数字编辑器,则会引发异常。
Also check that the source field is of a numeric data type, and it is not string. You can test that easily enabling editing - if you edit an item, an exception will be thrown if a string value is attempted to be assigned to the Telerik numeric editor.