Arrange columns around based on list(of string)

时间:2016-07-11 22:01:45

标签: c# vb.net

I have a list of column names in a list(of string), they match the current column.headers that are in my datagridview.

I want the columns to appear in the same order as they are in the list(of string).

For example, I have columns in this order

1 | 2 | 3 | 4 | 5

If I order them in my list to appear in this order...

3 | 2 | 1 | 4 | 5

I want them the columns to arrange in the same order I have my list.

How would this be accomplished?

I came up with this, but it appears to change it and then change it back..

   For l_index As Integer = 0 To visiblecolumns.Count - 1
        Dim l_text As String = visiblecolumns(l_index) 'String for the current index
        Dim num As Integer = dgvResults.Columns.[Single](Function(c) c.Header.ToString() = l_text).DisplayIndex
        dgvResults.Columns(num).DisplayIndex = l_index
    Next

1 个答案:

答案 0 :(得分:0)

I've found the solution

    For l_index As Integer = 0 To visiblecolumns.Count - 1
        Dim l_text As String = visiblecolumns(l_index) 'String for the current index
        Dim num As Integer = dgvResults.Columns.ToList().FindIndex(Function(c) c.Header = l_text) 'index of the column, based on header name
        dgvResults.Columns(num).DisplayIndex = l_index 'change displayindex of column based on list index
    Next