Value of '0' is not valid for 'SelectedIndex'

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrus

    Value of '0' is not valid for 'SelectedIndex'

    To reproduce, run the code.

    Observed:
    System.Argument OutOfRangeExcep tion was unhandled
    Message="Invali dArgument=Value of '0' is not valid for
    'SelectedIndex' .\r\nParameter name: SelectedIndex"

    t2.c1 contains legal value.
    Why this error occurs ?
    How to fix ?

    Andrus.


    using System.Windows. Forms;
    using System.Data;
    using System;
    class testForm : Form {

    testForm() {
    DataTable t = new DataTable();
    t.Columns.Add(" displaymember") ;
    t.Columns.Add(" valuemember");
    t.Rows.Add("low er", "l");

    ComboBox comboBox1 = new ComboBox();
    comboBox1.Displ ayMember = "displaymember" ;
    comboBox1.Value Member = "valuemembe r";
    comboBox1.DataS ource = t;
    comboBox1.AutoC ompleteSource = AutoCompleteSou rce.ListItems;
    comboBox1.AutoC ompleteMode = AutoCompleteMod e.SuggestAppend ;

    DataTable t2 = new DataTable();
    t2.Columns.Add( "c1");
    t2.Rows.Add("l" );
    comboBox1.DataB indings.Add("Se lectedValue", t2, "c1");
    Controls.AddRan ge(new Control[] { comboBox1 });
    }

    [STAThread]
    static void Main() {
    Application.Run (new testForm());
    }
    }

  • Kevin Spencer

    #2
    Re: Value of '0' is not valid for 'SelectedIndex'

    It isn't legal if it's a string.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP

    Printing Components, Email Components,
    FTP Client Classes, Enhanced Data Controls, much more.
    DSI PrintManager, Miradyne Component Libraries:


    "Andrus" <kobruleht2@hot .eewrote in message
    news:eMNs7PNtHH A.4800@TK2MSFTN GP05.phx.gbl...
    To reproduce, run the code.
    >
    Observed:
    System.Argument OutOfRangeExcep tion was unhandled
    Message="Invali dArgument=Value of '0' is not valid for
    'SelectedIndex' .\r\nParameter name: SelectedIndex"
    >
    t2.c1 contains legal value.
    Why this error occurs ?
    How to fix ?
    >
    Andrus.
    >
    >
    using System.Windows. Forms;
    using System.Data;
    using System;
    class testForm : Form {
    >
    testForm() {
    DataTable t = new DataTable();
    t.Columns.Add(" displaymember") ;
    t.Columns.Add(" valuemember");
    t.Rows.Add("low er", "l");
    >
    ComboBox comboBox1 = new ComboBox();
    comboBox1.Displ ayMember = "displaymember" ;
    comboBox1.Value Member = "valuemembe r";
    comboBox1.DataS ource = t;
    comboBox1.AutoC ompleteSource = AutoCompleteSou rce.ListItems;
    comboBox1.AutoC ompleteMode = AutoCompleteMod e.SuggestAppend ;
    >
    DataTable t2 = new DataTable();
    t2.Columns.Add( "c1");
    t2.Rows.Add("l" );
    comboBox1.DataB indings.Add("Se lectedValue", t2, "c1");
    Controls.AddRan ge(new Control[] { comboBox1 });
    }
    >
    [STAThread]
    static void Main() {
    Application.Run (new testForm());
    }
    }
    >

    Comment

    • Andrus

      #3
      Re: Value of '0' is not valid for 'SelectedIndex'

      Kevin,

      thank you.
      The code does not set SelectedIndex as all.

      Why .NET tries to set SelectedIndex to invalid value ?

      Andrus.
      It isn't legal if it's a string.
      >>
      >Observed:
      >System.Argumen tOutOfRangeExce ption was unhandled
      >Message="Inval idArgument=Valu e of '0' is not valid for
      >'SelectedIndex '.\r\nParameter name: SelectedIndex"
      >>
      >t2.c1 contains legal value.
      >Why this error occurs ?
      >How to fix ?
      >>
      >Andrus.
      >>
      >>
      >using System.Windows. Forms;
      >using System.Data;
      >using System;
      >class testForm : Form {
      >>
      > testForm() {
      > DataTable t = new DataTable();
      > t.Columns.Add(" displaymember") ;
      > t.Columns.Add(" valuemember");
      > t.Rows.Add("low er", "l");
      >>
      > ComboBox comboBox1 = new ComboBox();
      > comboBox1.Displ ayMember = "displaymember" ;
      > comboBox1.Value Member = "valuemembe r";
      > comboBox1.DataS ource = t;
      > comboBox1.AutoC ompleteSource = AutoCompleteSou rce.ListItems;
      > comboBox1.AutoC ompleteMode = AutoCompleteMod e.SuggestAppend ;
      >>
      > DataTable t2 = new DataTable();
      > t2.Columns.Add( "c1");
      > t2.Rows.Add("l" );
      > comboBox1.DataB indings.Add("Se lectedValue", t2, "c1");
      > Controls.AddRan ge(new Control[] { comboBox1 });
      > }
      >>
      > [STAThread]
      > static void Main() {
      > Application.Run (new testForm());
      > }
      >}
      >>
      >
      >

      Comment

      • =?windows-1257?Q?G=F6ran_Andersson?=

        #4
        Re: Value of '0' is not valid for 'SelectedIndex'

        Andrus wrote:
        To reproduce, run the code.
        >
        Observed:
        System.Argument OutOfRangeExcep tion was unhandled
        Message="Invali dArgument=Value of '0' is not valid for
        'SelectedIndex' .\r\nParameter name: SelectedIndex"
        >
        t2.c1 contains legal value.
        Why this error occurs ?
        How to fix ?
        >
        Andrus.
        >
        >
        using System.Windows. Forms;
        using System.Data;
        using System;
        class testForm : Form {
        >
        testForm() {
        DataTable t = new DataTable();
        t.Columns.Add(" displaymember") ;
        t.Columns.Add(" valuemember");
        t.Rows.Add("low er", "l");
        >
        ComboBox comboBox1 = new ComboBox();
        comboBox1.Displ ayMember = "displaymember" ;
        comboBox1.Value Member = "valuemembe r";
        comboBox1.DataS ource = t;
        comboBox1.AutoC ompleteSource = AutoCompleteSou rce.ListItems;
        comboBox1.AutoC ompleteMode = AutoCompleteMod e.SuggestAppend ;
        >
        DataTable t2 = new DataTable();
        t2.Columns.Add( "c1");
        t2.Rows.Add("l" );
        comboBox1.DataB indings.Add("Se lectedValue", t2, "c1");
        Controls.AddRan ge(new Control[] { comboBox1 });
        }
        >
        [STAThread]
        static void Main() {
        Application.Run (new testForm());
        }
        }
        >
        You can't select a specific item from a combobox that doesn't contain
        any items at all.

        You haven't databound the combobox, so it doesn't contain any items.

        --
        Göran Andersson
        _____
        Göran Anderssons privata hemsida.

        Comment

        • =?windows-1257?Q?G=F6ran_Andersson?=

          #5
          Re: Value of '0' is not valid for 'SelectedIndex'

          Andrus wrote:
          Kevin,
          >
          thank you.
          The code does not set SelectedIndex as all.
          >
          Why .NET tries to set SelectedIndex to invalid value ?
          >
          Andrus.
          You are setting the SelectedValue property. That will basically look for
          the value among the items, and set the SelectedIndex property for the
          matching item.

          --
          Göran Andersson
          _____
          Göran Anderssons privata hemsida.

          Comment

          • Tom Spink

            #6
            Re: Value of '0' is not valid for 'SelectedIndex'

            Andrus wrote:
            To reproduce, run the code.
            >
            Observed:
            System.Argument OutOfRangeExcep tion was unhandled
            Message="Invali dArgument=Value of '0' is not valid for
            'SelectedIndex' .\r\nParameter name: SelectedIndex"
            >
            t2.c1 contains legal value.
            Why this error occurs ?
            How to fix ?
            >
            Andrus.
            >
            >
            using System.Windows. Forms;
            using System.Data;
            using System;
            class testForm : Form {
            >
            testForm() {
            DataTable t = new DataTable();
            t.Columns.Add(" displaymember") ;
            t.Columns.Add(" valuemember");
            t.Rows.Add("low er", "l");
            >
            ComboBox comboBox1 = new ComboBox();
            comboBox1.Displ ayMember = "displaymember" ;
            comboBox1.Value Member = "valuemembe r";
            comboBox1.DataS ource = t;
            comboBox1.AutoC ompleteSource = AutoCompleteSou rce.ListItems;
            comboBox1.AutoC ompleteMode = AutoCompleteMod e.SuggestAppend ;
            >
            DataTable t2 = new DataTable();
            t2.Columns.Add( "c1");
            t2.Rows.Add("l" );
            comboBox1.DataB indings.Add("Se lectedValue", t2, "c1");
            Controls.AddRan ge(new Control[] { comboBox1 });
            }
            >
            [STAThread]
            static void Main() {
            Application.Run (new testForm());
            }
            }
            Hi,

            This is *totally* a guess, because I haven't tried your code, but try moving
            this line:

            comboBox1.DataS ource = t;

            So it immediately follows this line:

            ComboBox comboBox1 = new ComboBox();

            --
            Tom Spink
            University of Edinburgh

            Comment

            Working...