I need to have multiple lines in a cell, so I use DataGridViewTextBoxColumn.DefaultCellStyle.WrapMode =DataGridViewTriState.True.
I add columns (datagridviewtextboxcolumn) in designer. Then bound datagridview to a table which has exact the same columns as datagridview. Then I find that datagridview has duplicate columns: the first column is empty (which I guess is added through designer), the second column is filled with data from table. My question is how to correctly allow multi-lines and bound to a table at the same time?
In Designer
Friend WithEvents DGV As New System.Windows.Forms.DataGridView
Friend WithEvents columnClient As System.Windows.Forms.DataGridViewTextBoxColumn
Friend WithEvents columnAccount As System.Windows.Forms.DataGridViewTextBoxColumn
Me.DGV.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {Me.columnClient, Me.columnAccount})
Dim cellStyle As System.Windows.Forms.DataGridViewCellStyle = New System.Windows.Forms.DataGridViewCellStyle
cellStyle.WrapMode = DataGridViewTriState.True
Me.columnClient.DefaultCellStyle = cellStyle
Me.columnAccount.DefaultCellStyle = cellStyle
Then I bound datagridview to a table
DGV.DataSource = m_table
m_table.Columns.Add(columnClient)
m_table.Columns.Add(columnAccount)
Dim rowText As DataRow = m_table.NewRow
Dim strBaseKey As String="base key"
Dim accountkey As String="account key"
rowText(columnClient) = strBaseKey
rowText(columnAccount) = accountKey
Add: I just deleted columns in designer and it works now.
The solution is simple: just do not add columns to datagridview in designer; instead add columns to table and then bound datagridview to table. To enable multi-lines in datagridview, set the column.DefaultCellStyle.WrapMode=DataGridViewTriState.True
The datagridview will show: