"InvalidArgument=Value of '0' is not valid for 'index'

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jaydeep Ramavat
    New Member
    • Jul 2009
    • 12

    "InvalidArgument=Value of '0' is not valid for 'index'

    I am facing a problem in binding the datagridviewcom bo box cell.

    Code:
    DataGridViewComboBoxCell cmbControlTypes= new DataGridViewComboBoxCell();
    
    cmbControlTypes.DataSource = result.resultTable;
    cmbControlTypes.DisplayMember = "name";
    cmbControlTypes.ValueMember = "id";
    after binding the comboboxcell and adding it to row.
    when i access the item collection as follows.

    Code:
    cmbControlTypes.Value = cmbControlTypes.Items[0]
    i am getting an error

    "InvalidArgumen t=Value of '0' is not valid for 'index'.
    Parameter name: index"

    is there any solution for above problem?.
    Last edited by tlhintoq; Mar 2 '10, 12:58 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      is there any solution for above problem?.
      Yes. Don't presume you have items. Get used to range checking.

      Code:
      if (cmbControlTypes.Items.Count > 0)
      {
      
      cmbControlTypes.Value = cmbControlTypes.Items[0]
      
      }

      Comment

      • Jaydeep Ramavat
        New Member
        • Jul 2009
        • 12

        #4
        Thanks for your writing instruction and reply.

        Actually i was also doing count on items property.
        In my application i am getting count = 7, so not a problem.

        Comment

        • Jaydeep Ramavat
          New Member
          • Jul 2009
          • 12

          #5
          I have figured out the issue by following code.

          In below code loadcontrol function loads table and bind it to the cmbcontroltypes combo.

          Code:
                         DataTable dtReturn = loadControlTypes(cmbControlTypes);
          
                          if (dtReturn != null)
                          {
                              if (dtReturn.Rows.Count > 0)
                              {
                                  cmbControlTypes.Value = dtReturn.Rows[0][0];                        
                              }
                          }
          I can get the member at 0 position at selected value in datagridviewcom boboxcell.

          Thanks and Regards
          Jay

          Comment

          Working...