Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how to set default value of combobox in c#

how to set default value of combobox in c#

Scheduled Pinned Locked Moved C#
csharptutorialquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tanzeel85
    wrote on last edited by
    #1

    i fill combobox through datasource but at form load i have to show "None" default value of combobox. how i do it??

    D P 2 Replies Last reply
    0
    • T tanzeel85

      i fill combobox through datasource but at form load i have to show "None" default value of combobox. how i do it??

      D Offline
      D Offline
      Dan Mos
      wrote on last edited by
      #2

      try setting it's Text property. Being bound you can not add Items programatically but the Text property should work.

      1 Reply Last reply
      0
      • T tanzeel85

        i fill combobox through datasource but at form load i have to show "None" default value of combobox. how i do it??

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Add a "None" item to the collection before you bind it to the control?

        modified on Wednesday, April 28, 2010 12:00 PM

        T 1 Reply Last reply
        0
        • P PIEBALDconsult

          Add a "None" item to the collection before you bind it to the control?

          modified on Wednesday, April 28, 2010 12:00 PM

          T Offline
          T Offline
          tanzeel85
          wrote on last edited by
          #4

          items collection can not be modified when the datasource property is set...

          D V 2 Replies Last reply
          0
          • T tanzeel85

            items collection can not be modified when the datasource property is set...

            D Offline
            D Offline
            Dan Mos
            wrote on last edited by
            #5

            not to the comboboxitems. Add "None" to the dataset or whatever underlying collection the combobox is bound to.

            1 Reply Last reply
            0
            • T tanzeel85

              items collection can not be modified when the datasource property is set...

              V Offline
              V Offline
              V 0
              wrote on last edited by
              #6

              Yes you can:

                  /// <summary>
                  /// Adds an item to the DataSource of the ComboBox.
                  /// </summary>
                  /// <param name="box">A reference to the combobox.</param>
                  /// <param name="displaymember\_value">The value wich should be displayed.</param>
                  /// <param name="valuemember\_value">The value behind the displayed item.</param>
                  /// <returns>true if succesful otherwise, false.</returns>
                  public bool AddItemToComboDataSource(ref ComboBox box, string displaymember\_value, string valuemember\_value)
                  {
                      bool succes = false;
                      try
                      {
                          DataView view = (DataView)box.DataSource;
                          DataRow row = view.Table.Rows\[0\];
                          row\[0\] = displaymember\_value;
                          row\[1\] = valuemember\_value;
                          view.Table.Rows.Add(row);
                          box.DataSource = view;
                          succes = true;
                      }												//end try
                      catch (Exception ex)
                      {
                          errormsg = ex.Message;
                      }												//end catch
                      return succes;
                  }													//end method AddItemToComboDataSource
              

              Which is a little cheating, probably, but it works.

              V.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups