Troels
Troels

Reputation: 101

Add a new row to an existing dynamic datatable

I have a dynamic datatable, created from a database. I want to add a new row (blank), so there is an option to choose nothing in a combobx. Everything works ok without adding the new row. But when I add a new row, the combobox displays nothing. What am I missing?

Here is the code

Dim DT As New DataTable

DT = DS.Tables("CallStatus")
Dim drNewRow As DataRow = DT.NewRow

'Add new row
drNewRow.Item("CampaignCallStatusID") = ""
drNewRow.Item("CampaignCallStatus") = ""
DT.Rows.Add(drNewRow)
DT.AcceptChanges()

'Fill combobox
With cboCallStatus
    .DataSource = DT
    .DisplayMember = "CampaignCallStatus"
    .ValueMember = "CampaignCallStatusID"
End With

Upvotes: 1

Views: 25347

Answers (3)

Troels
Troels

Reputation: 101

I found a solution. I used the method InsertAt, and plased the row in the top.

 Dim DT As New DataTable
 DT = DS.Tables("CallStatus")
 Dim drNewRow As DataRow = DT.NewRow
 DT.Rows.InsertAt(drNewRow, 0)   ' <== This is the solution
 DT.AcceptChanges()

 With cboCallStatus
    .DataSource = DT
    .DisplayMember = "CampaignCallStatus"
    .ValueMember = "CampaignCallStatusID"
 End With
 cboCallStatus.Refresh()

Upvotes: 4

Alex Essilfie
Alex Essilfie

Reputation: 12613

You can try iterating through all the desired rows and adding them manually to the ComboBox.
An example is as follows:

ComboBox.Items.Clear()
For Each dr as DataRow in DT.Rows
    ComboBox.Items.Add(dr("col1").ToString())
Next

This is guaranteed to work when data binding fails.

Upvotes: 0

Tim Murphy
Tim Murphy

Reputation: 4932

In your example the CampaignCallStatus for the blank row is empty therefore the display in the combobox will be empty. If you believe that one of the database values should be displayed at the record that you are currently displaying then change the blank row CampaignCallStatus to a value (say Debug) to confirm the blank row is being displayed.

More than likely the reason the blank row is shown in the combobox is because of the CampaignCallStatusID is not the same type as database type for CampaignCallStatusID. Try changing the blank row value from "" to 0.

Upvotes: 0

Related Questions