0

我有一个使用数据表填充的数据网格视图。我的问题是当我向数据表添加列并更新数据网格视图时,数据网格视图列的标题文本从标题更改为数据表的名称。下面的代码在 datagridview 加载(正确)后开始,现在我单击一个按钮添加一列。我尝试手动循环遍历列以将标题文本更改为标题(显示),但它不起作用。有任何想法吗?

{vb.net

Private Sub Btn_AddAsColumn_Click(sender As Object, e As EventArgs) Handles Btn_AddAsColumn.Click

    If Not g_FormColumnHandler.TxtBox_ColumnName.Text = "" Then
                ' Add new column on a new thread.
                AddColmThread = New System.Threading.Thread(AddressOf AddCustomColumns)
                AddColmThread.SetApartmentState(Threading.ApartmentState.STA)
                AddColmThread.Start()
            Else
                MessageBox.Show(Me, "Please make a selection before continuing.", "Make a selection.")
                Exit Sub
            End If
End Sub

}

{

公共子 AddCustomColumns()

    Dim DtCol As DataColumn

    ' Create a column for the datatable.
    Select Case Me.Invoke(New GetCustomColTypeDel(AddressOf GetCustomColType))
    'Select Case SelectedProperties.Item(0).DataType
        Case Inv.ValueTypeEnum.kStringType
            'DtCol.DataType = Type.GetType("System.String")
            'DtCol.ColumnName = ToolBox.RemoveCharAndSpaces(g_FormColumnHandler.TxtBox_ColumnName.Text)
            DtCol = New DataColumn(ToolBox.RemoveCharAndSpaces(g_FormColumnHandler.TxtBox_ColumnName.Text), Type.GetType("System.String"))
        Case Inv.ValueTypeEnum.kIntegerType
            'DtCol.DataType = Type.GetType("System.Int32")
            'DtCol.ColumnName = ToolBox.RemoveCharAndSpaces(g_FormColumnHandler.TxtBox_ColumnName.Text)
            DtCol = New DataColumn(ToolBox.RemoveCharAndSpaces(g_FormColumnHandler.TxtBox_ColumnName.Text), Type.GetType("System.Int32"))
        Case Inv.ValueTypeEnum.kDoubleType
            'DtCol.DataType = Type.GetType("System.Int32")
            'DtCol.ColumnName = ToolBox.RemoveCharAndSpaces(g_FormColumnHandler.TxtBox_ColumnName.Text)
            DtCol = New DataColumn(ToolBox.RemoveCharAndSpaces(g_FormColumnHandler.TxtBox_ColumnName.Text), Type.GetType("System.Int32"))
        Case Inv.ValueTypeEnum.kBooleanType
            'DtCol.DataType = Type.GetType("System.Boolean")
            'DtCol.ColumnName = ToolBox.RemoveCharAndSpaces(g_FormColumnHandler.TxtBox_ColumnName.Text)
            DtCol = New DataColumn(ToolBox.RemoveCharAndSpaces(g_FormColumnHandler.TxtBox_ColumnName.Text), Type.GetType("System.Boolean"))
        Case Else
            DtCol = New DataColumn(ToolBox.RemoveCharAndSpaces(g_FormColumnHandler.TxtBox_ColumnName.Text), Type.GetType("System.String"))
    End Select

    ' Add the caption which will become the header text.
    DtCol.Caption = g_FormColumnHandler.TxtBox_ColumnName.Text

    ' Turn on DBNULL "empty" cell values.
    DtCol.AllowDBNull = True

    ' Add column to data table.
    g_DatTablColHandler.Columns.Add(DtCol)

    ' datagridview was created on a different thread. have to delegate.
    Me.Invoke(New UpdateDgv_ColumnsDel(AddressOf UpdateDgv_Columns))

结束子

}

{

Public Delegate Sub UpdateDgv_ColumnsDel() Public Sub UpdateDgv_Columns() Dim _Sync As New Object

    SyncLock _Sync
        DGV_Columns.DataSource = Nothing

        ' Reset the column header text to the datatable captions.
        For iCol As Integer = 0 To DGV_Columns.Columns.Count - 1 : DGV_Columns.Columns.Item(iCol).HeaderText = g_DatTablColHandler.Columns.Item(iCol).Caption : Next


        DGV_Columns.DataSource = g_DatTablColHandler

        ' Update the datagridview
        DGV_Columns.Refresh()
    End SyncLock
End Sub

}

4

0 回答 0