简体   繁体   中英

DataGridView - Column Header Change Weirdness

So I currently have a form in whose Load event I add a dynamic number of TabPages and DataGridViews (DGV) - 1 DGV per TabPage. I want to handle certain events for these DGVs, so I also add handlers. This is the code in Load for that:

'Add a tab page and corresponding grid for each table in the data set
For i = 0 To binData.BinnedTables.Tables.Count - 1
    Dim tabPage As C1.Win.C1Command.C1DockingTabPage = New C1.Win.C1Command.C1DockingTabPage
    tabPage.Text = binData.BinnedTables.Tables(i).TableName
    tabContent.TabPages.Add(tabPage)

    dgvData = New DataGridView
    AddHandler dgvData.DataBindingComplete, AddressOf dgvData_Created
    With dgvData
        .Dock = DockStyle.Fill
        .AllowUserToOrderColumns = False
        .AllowUserToAddRows = False
        .AllowUserToDeleteRows = False
        .DataSource = binData.BinnedTables.Tables(i)
        .Columns.Add("Total", "Total")
        .Columns("Total").ReadOnly = True
    End With

    tabPage.Controls.Add(dgvData)

    'These following lines have to go after dgvData is added to tabPage because otherwise they don't work
    dgvData.AutoResizeColumnHeadersHeight()

    For Each col As DataGridViewColumn In dgvData.Columns
        col.SortMode = DataGridViewColumnSortMode.NotSortable
        col.ValueType = GetType(Integer)
    Next

    AddHandler dgvData.RowPostPaint, AddressOf MyDGV_RowPostPaint

    AddHandler dgvData.KeyDown, AddressOf dgvData_KeyDown
    AddHandler dgvData.CellValueChanged, AddressOf dgvData_CellChanged
    AddHandler dgvData.CellEndEdit, AddressOf dgvData_CellEdit
    AddHandler dgvData.CellValidating, AddressOf dgvData_CellValidating

    AddHandler tabPage.TextChanged, AddressOf tabPage_TextChanged
    AddHandler dgvData.ColumnHeaderMouseDoubleClick, AddressOf dgvData_ColumnHeaderMouseDoubleClick
    AddHandler dgvData.ColumnHeaderCellChanged, AddressOf dgvData_ColumnHeaderTextChanged

Next   'DataTable In binData.BinnedTables.Tables

I use the ColumnHeaderMouseDoubleClick event to allow the user to change the text in the column header. I also want to update the column name for the corresponding column in the data source, so they are in sync. This is what I have in ColumnHeaderMouseDoubleClick:

Dim renameCol As New dlgNewCol
renameCol.lblInsertCol.Text = "Rename Column"

If renameCol.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
    Dim thisGrid As DataGridView = DirectCast(sender, DataGridView)
    Dim thisCol As DataGridViewColumn = thisGrid.Columns(e.ColumnIndex)

    thisCol.HeaderText = renameCol.txtColName.Text

    'The index of the TabPage corresponds to the index of the table in the data source
    binData.BinnedTables.Tables(tabContent.SelectedIndex).Columns(e.ColumnIndex - 1).ColumnName = thisCol.HeaderText

End If   'if dialog result is OK

What happens, though, is this works but then for whatever reason the column whose header was changed gets moved to the end of the grid, plus some other properties get reset.

If I comment out the line to update the column name of the table that is the data source for the DGV, then I don't have the issue with the column properties getting reset. However, then that source's column name does not get updated, so they are out of sync. Just having the table as the Data Source does not automatically update column names like it does the actual data.

I figured I'd use the ColumnHeaderCellChanged event to change these properties back to what they are supposed to be, because I'd expect that the code above would fire that....but it doesn't.

So I guess my questions are: Why does changing the column name of the data source have this effect (changing properties of the DGV's column)?, Why is the ColumnHeaderCellChanged event not firing?, And is there some other way I can manage to change both the DGV column's header and the data source's column name, plus have that column's other properties remain as they were (or be put back)?

Thank you!

Looks like AutoGenerateColumns is on. You'll need to turn it off and manual add the columns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM