Datagrid Sorting Problem

Ste T

New member
Joined
Jan 30, 2009
Messages
2
Programming Experience
Beginner
Hi

I am creating a program that allows employees to fill in a worksheet which can then be sent to management who can see what each employee is doing.

Currently the program has a datagrid that allows users to choose any day from a combobox located in the first column of the datagrid and then enter information for this day in the other columns. I have got the grid so that it automatically puts the rows in order in terms of the days and the starting weekday that the user chooses from a date time picker. For example if the user chooses a week start date that falls on a Tuesday then the grid will sort itself by placing the rows that have Tuesday chosen for them at the top followed by Wednesday, Thursday etc. The code that I use to do this is shown below and is located in the days combobox SelectedValueChanged event:

VB.NET:
        DataGridCell1 = DataGrid1.CurrentCell
        DataGrid1(DataGridCell1.RowNumber, DataGridCell1.ColumnNumber) = comboControl5.Text

        OrderingDays = True

        GetRow2 = Table1.Rows.Item(DataGridCell1.RowNumber)
        GetRow = Table1.NewRow
        GetRow.Item("Day") = GetRow2.Item(0)
        GetRow.Item("Project") = GetRow2.Item(1)
        GetRow.Item("Activity") = GetRow2.Item(2)
        GetRow.Item("Description") = GetRow2.Item(3)
        GetRow.Item("Miles") = GetRow2.Item(4)
        GetRow.Item("Location") = GetRow2.Item(5)
        GetRow.Item("InScope") = GetRow2.Item(6)
        GetRow.Item("ChargeableDays") = GetRow2.Item(7)
        GetRow.Item("Concessed") = GetRow2.Item(8)
        GetRow.Item("Reason") = GetRow2.Item(9)
        GetRow.Item("DayTotal") = GetRow2.Item(10)

        Dim FindRow As Integer
        Dim i As Integer
        Dim b As Integer
        Dim InsertRowAt As Integer
        Dim RowFound As Boolean = False
        FindRow = comboControl5.SelectedIndex

        If Table1.Rows.Count - 1 = 1 Then
            InsertRowAt = 1
            RowFound = True
        End If

        For i = 0 To FindRow
            If RowFound = True Then Exit For
            For b = 0 To Table1.Rows.Count - 1
                If b <> DataGridCell1.RowNumber Then
                    'MsgBox(DaysOrder(FindRow - i))
                    'MsgBox(Table1.Rows(b).Item(0))
                    If DaysOrder(FindRow - i) = Table1.Rows(b).Item(0) Then
                        InsertRowAt = b + 1
                    End If
                End If
            Next
            If InsertRowAt <> 0 Then
                RowFound = True
            End If
        Next

        Table1.Rows.Remove(GetRow2)
        Table1.Rows.InsertAt(GetRow, InsertRowAt)
        Table1.AcceptChanges()
        Refresh()
        OrderingDays = False

Now I have no problem organising the data however I do get a problem if the user attempts to fill in other data within the grid, which for some reason ocassionally adds a blank row and then moves the row being edited somewhere else messing up the ordering system. The above code though does not run when this error occurs, however if I remove this code then no problems, which makes me think it must be something to do with how the rows are being inserted into the grid.

Does anyone have any idea what this problem could be?

Cheers
 
Back
Top