Question Datagridview performance issue

rmclean

Member
Joined
Apr 15, 2010
Messages
6
Programming Experience
1-3
I have datagridview that is bound to a datasource (SQL Server table). I added some code to the cell_formatting event that makes text in 2 columns appear stacked (code below). Since I added this formatting, any attempt to resize a column or sort a column is painfully slow. Prior to adding the code, it reacted very quickly to either column resizing or sorting. Any advice on settings or anything else I can check to try and improve performance?
VB.NET:
Private Sub dgvRecords_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgvRecords.CellFormatting

        If e.ColumnIndex = 3 Then

            Dim strValue As String = TryCast(e.Value, String)
            If strValue Is Nothing Then Return

            strValue = CStr(dgvRecords.Item(e.ColumnIndex, e.RowIndex).Value)
            Dim strArray As String = strValue.Replace(" ", Environment.NewLine)
            e.Value = strArray

        End If

        If e.ColumnIndex = 5 Then

            Dim strValue As String = TryCast(e.Value, String)
            If strValue Is Nothing Then Return

            strValue = CStr(dgvRecords.Item(e.ColumnIndex, e.RowIndex).Value)
            Dim strArray As String = strValue.Replace(" ", Environment.NewLine)
            e.Value = strArray

        End If

    End Sub
 
Last edited by a moderator:
I'm not sure how much it will speed things up but that code needs some cleaning up.

1. The first If block tests whether the column index is 3. If that's true, you know the column index can't possibly be 5, so why check it again?
VB.NET:
Private Sub dgvRecords_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgvRecords.CellFormatting
    If e.ColumnIndex = 3 Then
        Dim strValue As String = TryCast(e.Value, String)

        If strValue Is Nothing Then Return

        strValue = CStr(dgvRecords.Item(e.ColumnIndex, e.RowIndex).Value)

        Dim strArray As String = strValue.Replace(" ", Environment.NewLine)

        e.Value = strArray
    ElseIf e.ColumnIndex = 5 Then
        Dim strValue As String = TryCast(e.Value, String)

        If strValue Is Nothing Then Return

        strValue = CStr(dgvRecords.Item(e.ColumnIndex, e.RowIndex).Value)

        Dim strArray As String = strValue.Replace(" ", Environment.NewLine)

        e.Value = strArray
    End If
End Sub
Next, you have EXACTLY the same code in both blocks, so why do you need two blocks? Check both conditions together and do your work if either is True:
VB.NET:
Private Sub dgvRecords_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgvRecords.CellFormatting
    If e.ColumnIndex = 3 OrElse e.ColumnIndex = 5 Then
        Dim strValue As String = TryCast(e.Value, String)

        If strValue Is Nothing Then Return

        strValue = CStr(dgvRecords.Item(e.ColumnIndex, e.RowIndex).Value)

        Dim strArray As String = strValue.Replace(" ", Environment.NewLine)

        e.Value = strArray
    End If
End Sub
Next, why do you need to get the cell value when you've already got it?
VB.NET:
Private Sub dgvRecords_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgvRecords.CellFormatting
    If e.ColumnIndex = 3 OrElse e.ColumnIndex = 5 Then
        Dim strValue As String = TryCast(e.Value, String)

        If strValue Is Nothing Then Return

        Dim strArray As String = strValue.Replace(" ", Environment.NewLine)

        e.Value = strArray
    End If
End Sub
Finally, not everyone would agree with me but I much prefer testing for the condition you want rather then testing for the condition you don't want and returning.
VB.NET:
Private Sub dgvRecords_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgvRecords.CellFormatting
    If e.ColumnIndex = 3 OrElse e.ColumnIndex = 5 Then
        Dim strValue As String = TryCast(e.Value, String)

        If strValue IsNot Nothing Then
            Dim strArray As String = strValue.Replace(" ", Environment.NewLine)

            e.Value = strArray
        End If
    End If
End Sub
Make those changes, although the last one would be optional, and see what happens. It may well be that your string processing is just too slow, but let's see.
 
Back
Top