简体   繁体   English

DataGridView-列标题更改怪异

[英]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. 因此,我目前有一个表单,在该表单的Load事件中,我添加了动态数量的TabPages和DataGridViews(DGV)-每个TabPage 1个DGV。 I want to handle certain events for these DGVs, so I also add handlers. 我想为这些DGV处理某些事件,因此我还添加了处理程序。 This is the code in Load for that: 这是Load中的代码:

'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. 我使用ColumnHeaderMouseDoubleClick事件允许用户更改列标题中的文本。 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: 这是我在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. 如果我注释掉该行以更新作为DGV数据源的表的列名,那么列属性就不会被重置。 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. 我以为我会使用ColumnHeaderCellChanged事件将这些属性更改回它们应有的状态,因为我希望上面的代码会触发该操作……但是事实并非如此。

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)? 所以我想我的问题是:为什么更改数据源的列名会产生这种影响(更改DGV的列的属性)?,为什么不触发ColumnHeaderCellChanged事件?,还有其他方法可以更改吗? DGV列的标题和数据源的列名称,以及该列的其他属性是否保持原样(或放回原样)?

Thank you! 谢谢!

Looks like AutoGenerateColumns is on. 看起来AutoGenerateColumns已打开。 You'll need to turn it off and manual add the columns. 您需要将其关闭,然后手动添加列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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