Add columns to datatable using LINQ

Dim dt As New DataTable
dt.Columns.AddRange((From col In columns
                     Select New DataColumn(col.ColumnHeaderText, col.ColumnDataType) With {.ReadOnly = col.ColumnReadOnly}
                     ).ToArray)

ColumnHeaderFill I couldn't figure out what was.

Adding rows assuming ItemList is a list of values for that column:
Dim rowcount = (From col In columns
                Select col.ItemList.Count
                ).Max

For i = 0 To rowcount - 1
    Dim ix = i
    dt.Rows.Add((From col In columns
                 Select If(ix < col.ItemList.Count, col.ItemList(ix), Nothing)
                 ).ToArray)
Next

Select col.ItemList(ix) rather than the conditional is sufficient if all ItemLists have same number of items.
 
Actually ItemList is a list of values that should appear as a dropdown for that column. But that column has to be deleted from the datagridview then added as a DataGridviewComboboxColumn with ItemList bound to its .Items property.

The code above works, but I'm having trouble inserting datagridviewcomboboxcolumn into the datagridview. I added a .ColumnIndex property to my Column object but I don't know how to get the index from an XML element. So far I have:

VB.NET:
.ColumnIndex = a.Elements("column").[Select](Function(ByVal el, ByVal idx), idx), _ ?????????

I then plan to do this:

VB.NET:
Dim dgvcolumns = (From dcolumn In AssayConfig.AssayColumns Where dcolumn.ItemList.Count > 0 _
                             Select New DataGridViewColumn() With { _
                                 .CellTemplate = New DataGridViewComboBoxCell(), _
                                 .DataPropertyName = dcolumn.ColumnHeaderText, _
                                 .HeaderText = dcolumn.ColumnHeaderText, _
                                 .FillWeight = dcolumn.ColumnHeaderFill}).ToArray

For Each col in dgvcolumns
dataGridView.Columns.Insert(dgvcolumns.ColumnIndex, dgvcolumns)
Next dgvcolumns

Then somehow add the ItemList to the dropdown.
 
Last edited:
Back
Top