views:

43

answers:

3

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
A: 

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.

Tim Murphy
Thank for the answers. I found the solution.
Troels
Thanks. I tried. Didnt work.
Troels
A: 

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.

Alex Essilfie
A: 

Hi, 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) DT.AcceptChanges()

    With cboCallStatus
        .DataSource = DT
        .DisplayMember = "CampaignCallStatus"
        .ValueMember = "CampaignCallStatusID"
    End With
    cboCallStatus.Refresh()
Troels
Would you please tidy up the source code for markdown.
Tim Murphy
Is the solution the InsertAt() or is it the cboCallStatus.Refresh()? You did not have that in your original question.
Tim Murphy