GoldBishop
GoldBishop

Reputation: 2861

Add Custom Item after Databinding

I have develeped a DataSet to access information from SQL for a desktop application (using WinForms).

Binding data to a ComboBox is nothing new but as i have done most of in ASP.Net, some classes that i would have used just are not available for WinForms, evidently.

So Basically, i need to know what i need to do to bind a combobox to a DataTable and then add a custom item to Index=0, basically states "Select MC". This item will be checked against on SelectionChange but i need to have a Nullification ListItem in the set so i can invalidate the conditional refinement.

I have 3 comboboxes and 1 datagridview. The 3 comboboxes can refine the visible list of information in the gridview but i also want the user to have the ability to unselect their choice.

Here is what i tried and not getting anywhere:
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
      'cboMC.Datasource = Me._mc.GetMC().AddMCRow("Select MC") <-- Did not Work
    cboMC.DataSource = Me._mc.GetMC()
    cboMC.Items.Insert(0, New Object() {"Select MC"}) <-- Current Try

    ...    
End Sub

Any other suggestions? Again this is a WinForms application and not WebForms.

Final Revision (per @Tebc)

cboMC.Items.Add("Select MC")
cboMC.Items.AddRange(Me._mc.GetMC().ToArray())

Derivative of above making sure Invalidator is at index 0

cboMC.Items.AddRange(Me._mc.GetMC().ToArray())
cboMC.Items.Insert(0, "Select MC")

Worked like a charm....thans for putting me on the correct path.

Upvotes: 1

Views: 2981

Answers (1)

Tebc
Tebc

Reputation: 121

I don't recall the exact syntax, but what I have done is something along the lines of:

  1. Create a temp Array with size set to the count of your data table
  2. cboMC.Items.Add("Select MC")
  3. use the ToArray function of the data table
  4. cobMC.Items.AddRange([the temp Array])

Upvotes: 2

Related Questions