Good day guys,
here is another question I have, working with vb.net community 15. I have a large datagridview to display all lessons for my school timetabling program. There could be up to 14 timeslots = lessons per day. Now I like a timetabler to be able swap days like Monay with Wednesday etc. I know how to do it via the indexes of columns (slow). However, I came across the displayindex method of swapping columns very fast . Swapping days with 12 columns is in the blink of an eye. The problem is I can’t work out how to than save the dataviewgrid as it is displayed after the swap. I found a few questions to this regard on the net but no answers and the ways I tried didn’t work. Maybe anyone here came across the problem before?

8 Months
Discussion Span
Last Post by ddanbe
Featured Replies

endedit method didn't help. After saving and reloading the data came back as was saved previously.


Thank you or your interest. So there is nothing special in the save method:

 Friend Sub savemytimetable()
        Dim j, i As Integer, str As String
        str = ""
        myfileaction(303) 'delete old timetable first
        For j = 0 To Maintt.DataGridView1.Rows.Count - 1 'was j=2
            For i = 0 To Maintt.DataGridView1.Columns.Count - 1
                str = str & CStr(Maintt.DataGridView1.Item(i, j).Value) & ","
            mytxt = Trim(str)
            myfileaction(160) 'send mytxt to the fileaction and save timetable

            str = ""
        Exit Sub

and myfileaction(160) just saves the textfile after being called.

 Case 160 'save maintimetable
                Dim myfile As System.IO.StreamWriter
                myfile = My.Computer.FileSystem.OpenTextFileWriter(mypath & "\" & constfoldername & "\" & timetableselected & "\" & "TimeTable.txt", True)

Loading the time table:

 Case 161 'load timetables into form
                If My.Computer.FileSystem.FileExists(mypath & "\" & constfoldername & "\" & timetableselected & "\" & "TimeTable.txt") Then
                    Dim readText() As String = File.ReadAllLines(mypath & "\" & constfoldername & "\" & timetableselected & "\" & "TimeTable.txt")
                    Dim delimiters() As Char = {","c}, parts() As String, iPart As IEnumerator
                    Dim s As String, i, j As Integer
                    j = 0
                    For Each s In readText
                        parts = s.Split(delimiters)
                        iPart = parts.GetEnumerator
                        For i = 0 To Maintt.DataGridView1.Columns.Count - 1
                            Maintt.DataGridView1.Item(i, j).Value = parts(i)
                        j = j + 1
                End If

So there is nothing special there.


So here is some more explanation:
This would be the method I would like to use if can figure out how to change the indexes.
This is the code to swap- timetabler can click in any column that displays the first week name then the second. The program will find the first occurance of the first name, than the first occurance of the second name, than work out the distance via these indexes. I than just need to loop through, find the first occurance and swap using the distance.
After the swap completes I rename the days.


        For ijj As Integer = 0 To Maintt.DataGridView1.ColumnCount - 1
            If CStr(Maintt.DataGridView1.Item(ijj, 0).Value) = myday1 Then
                Maintt.DataGridView1.Columns(ijj).DisplayIndex = ijj + mydiff
                Maintt.DataGridView1.Columns(ijj + mydiff).DisplayIndex = ijj
            End If


        For ijj As Integer = 0 To Maintt.DataGridView1.ColumnCount - 1
            If CStr(Maintt.DataGridView1.Item(ijj, 0).Value) = myday1 Then
                Maintt.DataGridView1.Item(ijj, 0).Value = myday2
            ElseIf CStr(Maintt.DataGridView1.Item(ijj, 0).Value) = myday2 Then
                Maintt.DataGridView1.Item(ijj, 0).Value = myday1
            End If

Edited by Minimalist: corrections

Attachments swap.gif 533.48 KB

Problem is the DisplayIndex. It changes the order on the screen, but internally nothing is changed.
My best bet to continue here would be to have a look at the DataGridViewColumnCollection Click Here


Well, the displayindex is indeed the problem as to my original question. The DataGridViewColumnCollection also refers to the indexes as the datagridview was originally created as to my understanding.
At the moment I use this code and it works good. The benefit here is that I don't need to rename the columns as I only swap the relevant cells.

 For ii = 0 To Maintt.DataGridView1.ColumnCount - 1
            If Maintt.DataGridView1.Rows(0).Cells(ii).Value.ToString = myday1 Then
                For intI = 2 To Maintt.DataGridView1.RowCount - 1
                    str = Maintt.DataGridView1.Rows(intI).Cells(ii).Value.ToString & " "
                    str1 = Maintt.DataGridView1.Rows(intI).Cells(ii + mydiff).Value.ToString & " "
                    Maintt.DataGridView1.Rows(intI).Cells(ii).Value = str1
                    Maintt.DataGridView1.Rows(intI).Cells(ii + mydiff).Value = str
                    Trim(Maintt.DataGridView1.Rows(intI).Cells(ii + mydiff).Value.ToString)
            End If

This way there is no problem with the saving.
You loose some you get some. I came accross the displayindex method and thought of it as good idea. Maybe it is just more of an academic question now. Someone might have this brilliant idea?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.