0

Have gridview that I have populate from a database, and now I would like right align all the numeric items in the gridview, but If IsNumeric(row.Cells(i).Text) statment takes extremely long time is there any other way to fix this?

Code:

Takes long time any other way align number to the right

             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
Cris
  • 12,799
  • 5
  • 35
  • 50
Lime3003
  • 123
  • 4
  • 17
  • Did you try the specific TryParse for the datatype of your column? – Steve Mar 20 '14 at 12:02
  • 4
    You should know the type of the columns in the table (in the database). Why are you using `IsNumeric()` to work out something you already know? – dav1dsm1th Mar 20 '14 at 12:04
  • @dav1dsm1th is right, unless this column contains mixed values (some string and some numbers). Please explain – Steve Mar 20 '14 at 12:07
  • First of all I can´t get to each column that´s why I'm doing this and second of all the gridview is dynamic so there is sometimes more "columns" that is numeric. – Lime3003 Mar 20 '14 at 12:10
  • Are you binding the GridView to a generic List or a DataTable? – Magnus Mar 20 '14 at 12:38
  • `Dim dataTable As New DataTable dvData = New DataView(dataTable) gwResult.DataSource = dvData gwResult.DataBind()` – Lime3003 Mar 20 '14 at 12:43

2 Answers2

1

You are using this code to identify the numeric values after binding DataSource, which will add extra time to analyse the grid data. Try using the same code on RowDataBound event of the gridview.

Bharadwaj
  • 2,535
  • 1
  • 22
  • 35
  • Why would that make it faster?? – Lime3003 Mar 20 '14 at 12:19
  • Because for all row, `RowDataBound` will be executed (if it exists). Then you can check the value on each row call while binding. In your case the grid is bonded and then looping through each row and each cell to analyse the value. – Bharadwaj Mar 20 '14 at 12:22
  • But my problem is that, for ex that column 0 is string, column 1 is numeric, column 2 is string, so I can't put one row as numeric then I would rather use column but I haven¨t figure out how to fix that gridview gives you the columns. – Lime3003 Mar 20 '14 at 12:26
  • In row data bound event you can access each cell and its value too. http://msdn.microsoft.com/en-us/library/System.Web.UI.WebControls.GridViewRow(v=vs.110).aspx – Bharadwaj Mar 20 '14 at 12:34
1

Your best option would be to find out which columns are numeric before you bind the grid to the datasource. In the RowDataBound event of the GridView you can then just check if the current column being databound is in the numericColumns collection or not.

'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
Magnus
  • 45,362
  • 8
  • 80
  • 118