Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
I'm getting this error when i click on the combobox (dropdown) the grid view in the winforms.

Index -1 does not have a value.

Please help me in this... I'm Stuck for a long time... No luck in googling. :(

Thanks.
Posted
Updated 4-Feb-13 23:37pm
v2
Comments
Naveen.Sanagasetti 5-Feb-13 4:07am    
post your code.. please
BotCar 5-Feb-13 4:12am    
Not much we can do without seeing some code, but index -1 means nothing is selected. Hope that helps you figure it out.
BotCar 5-Feb-13 4:22am    
I don't understand your comment. Can you please rephrase it?
Naveen.Sanagasetti 5-Feb-13 4:31am    
for error explanation , they post some information that's enough...
But user asking in his question "Please help me in this... I'm Stuck for a long time... No luck in googling.", that's way i'm asking code, try to resolve where he facing the issue. that's want i'm asking yar...
Velrats 5-Feb-13 4:41am    
I dont know where the error comes from... but I get the error message after loading the form.

When no item is selected, the index value that is returned by the SelectedIndex property is -1

At the top of your event handler check it before you use it:
C#
ComboBox cb = sender as ComboBox;
if (cb != null && cb.SelectedIndex >= 0)
   {
   ...
   }
 
Share this answer
 
Comments
Joezer BH 5-Feb-13 5:35am    
5+, this one has required decryption work before producing an answer
Velrats 5-Feb-13 5:36am    
I have the combobox inside the gridview. At that time how can I do this?? please explain....
OriginalGriff 5-Feb-13 5:49am    
So? The error is occurring in an event handler method, no? And the event handler is presumably identifying the specific combobox? So change the code above to match the code you have!
Hi,

Umm we really do need to see some code to help on this but a quick demo below:

C#
namespace WindowsFormsApplication160
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
   MessageBox.Show("index = "+comboBox1.SelectedIndex.ToString()+"\nvalue = "+comboBox1.Text);  
        }
    }
}

This will return -1 if there is no value in the Combo box when you read it. If you have data in it make sure it is shown. To show a value use the below:
C#
comboBox1.SelectedIndex = 1;
That will take the second value in the list of data that you have entered to the combo box Items value. For a full use of combo boxes in C# see
http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox(v=VS.90).aspx[^]
Hope that helps.
Glenn
 
Share this answer
 
v2
Comments
Velrats 5-Feb-13 5:36am    
I have the combobox inside the gridview. At that time how can I do this?? please explain....
glennPattonWork3 5-Feb-13 5:47am    
I have never used Grid View but I am assumning you have it set up correctly, if I have a look at <p>http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview(VS.80).aspx</p>
it would seem that you would need to give the location of the combo box i.e. DataGrid1.Row[?] and DataGrid.Column[?] then the line to get the value from the combo box.
Glenn (sorry) (Orig Griff over to you!)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900