我有一个从数据库填充的网格视图,现在我想右对齐网格视图中的所有数字项,但是如果IsNumeric(row.Cells(i).Text)语句花费了非常长的时间,有没有其他方法来解决这个问题?
代码:
任何其他方式都需要很长时间将数字向右对齐
For Each row As GridViewRow In Me.gwResult.Rows
For i As Integer = 0 To headCell - 1
If IsNumeric(row.Cells(i).Text) Then
row.Cells(i).HorizontalAlign = HorizontalAlign.Right
End If
Next
Next发布于 2014-03-20 20:16:43
在绑定DataSource之后,您将使用此代码来标识数值,这将增加额外的时间来分析网格数据。尝试在网格视图的RowDataBound事件上使用相同的代码。
发布于 2014-03-20 20:50:26
最佳选择是在将网格绑定到数据源之前找出哪些列是数值列。在GridView的RowDataBound事件中,您可以只检查数据绑定的当前列是否在numericColumns集合中。
'Find numeric properties of the type beehing databound
Dim numericColumns = New HashSet(GetType(MyDataType).GetProperties() _
.Where(Function(x) IsNumericType(x.PropertyType)))
.Select(Function(x) x.Name))
'And for DataTable it would look like this
Dim numericColumns = New HashSet(dt.Columns.Cast(Of DataColumn)() _
.Where(Function(x) IsNumericType(x.DataType))) _
.Select(Function(x) x.ColumnName))
Private Shared Function IsNumericType(dataType As Type) As Boolean
Dim code = CInt(Type.GetTypeCode(dataType ))
Return code >= 4 AndAlso code <= 15
End Functionhttps://stackoverflow.com/questions/22532261
复制相似问题