DataGridView - 列标题更改怪异

时间:2015-09-25 18:27:36

标签: vb.net events datagridview datagridviewcolumn

所以我目前有一个表单在其Load事件中我添加了动态数量的TabPages和DataGridViews(DGV) - 每个TabPage 1个DGV。我想处理这些DGV的某些事件,所以我也添加了处理程序。这是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

我使用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

但是,这会起作用,但无论出于何种原因,其标题已更改的列都会移动到网格的末尾,另外还会重置其他一些属性。

如果我注释掉该行以更新作为DGV数据源的表的列名,那么我没有遇到重置列属性的问题。但是,该源的列名称不会更新,因此它们不同步。只需将表作为数据源,就不会自动更新列名,就像实际数据一样。

我想我会使用ColumnHeaderCellChanged事件将这些属性更改回它们应该是什么,因为我希望上面的代码会触发它....但它没有。

所以我想我的问题是:为什么更改数据源的列名会产生这种影响(更改DGV列的属性)?,为什么ColumnHeaderCellChanged事件没有触发?,还有其他方法可以设法更改DGV列的标题和数据源的列名,还加上该列的其他属性保持不变(或被放回)?

谢谢!

1 个答案:

答案 0 :(得分:0)

看起来AutoGenerateColumns已启用。您需要将其关闭并手动添加列。