0

有我从数据库中填充的gridview,现在我想右对齐gridview中的所有数字项,但是如果IsNumeric(row.Cells(i).Text)statment需要很长时间,还有其他方法可以解决这个问题吗?

代码:

以任何其他方式将数字向右对齐需要很长时间

             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
4

2 回答 2

1

您最好的选择是在将网格绑定到数据源之前找出哪些列是数字的。如果RowDataBound发生这种情况,GridView您可以检查当前正在数据绑定的列是否在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 Function
于 2014-03-20T12:50:26.100 回答
1

您正在使用此代码来识别绑定后的数值DataSource,这将增加额外的时间来分析网格数据。尝试RowDataBound在 gridview 的事件上使用相同的代码。

于 2014-03-20T12:16:43.170 回答