Click here to Skip to main content
15,924,367 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i write this code and i don't know where the error ?


C#
void FillComboboxCarPlace()
        {
            try
            {
                
                dSet = new DataSet();
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                s = "select Move_id , Move_Place from Car_Move_Place";
                sCommand = new SqlCommand(s, con);
                sdAdapter = new SqlDataAdapter();
                sdAdapter.SelectCommand = sCommand;
                sdAdapter.Fill(dSet);
                DataRow dr = dSet.Tables[0].NewRow();
                dr.ItemArray = new object[2] { 0, " ---Select--- " };
                dSet.Tables[0].Rows.InsertAt(dr, 0);
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    int index = 0;
                    var cbxMove = row.Cells[index] as DataGridViewComboBoxCell;
                    //DataGridViewComboBoxCell ContactCombo = (DataGridViewComboBoxCell)(row.Cells["Car_Move_Place"]);
                    ComboMovePlace.ValueMember = "Move_id";
                    ComboMovePlace.DisplayMember = "Move_Place";
                    ComboMovePlace.DataSource = dSet.Tables[0];
                }
                
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }

            }
            catch
            {
                return;
            }
        }


What I have tried:

i want some help for this problem and thanks
Posted
Updated 26-Jun-16 23:00pm
Comments
[no name] 27-Jun-16 4:44am    
If you don't know where the error is, how should we know ?
How about running it through debugger and tell us where the exception is ?
MahmoudOmar 27-Jun-16 4:48am    
debugger, It is not clear to me anything
[no name] 27-Jun-16 4:53am    
Hmmm.... first : Learn how to use Visual Studio..

C#
foreach (DataGridViewRow row in dataGridView1.Rows)
           {
               int index = 0;
               var cbxMove = row.Cells[index] as DataGridViewComboBoxCell;
               cbxMove.ValueMember = "Move_id";
               cbxMove.DisplayMember = "Move_Place";
               cbxMove.DataSource = dSet.Tables[0];
           }


index =0; this will consider the first column and it will try to cast it to DataGridViewComboBoxCell, if it is not of type comboboxcell then the error will be thrown
change the index value to point the ComboBox column, it is zero based index so if the combobox column in in 3rd Column, you will have to assign the index as 2
 
Share this answer
 
"debugger, It is not clear to me anything"
Probably, it's because you haven't used the data in your DataSet to load your DataGridView before you try to start using the rows. So the system doesn't know what your DGV columns should be like.
Start by putting a breakpoint on the line:
C#
dSet.Tables[0].Rows.InsertAt(dr, 0);

And step through your code.
Look at the variables, and what exactly is going on - it's very muddled code, with your looping on the DGV rows to set a couple of non-loop variables cbxMove (which you never use again) index (which you use once and it irrelevant) and ComboMovePlace which gets the same value each time the loop ends.

I think you need to think a bit more carefully about what you are trying to do - it isn't at all obvious from that code - before you jump into coding.
 
Share this answer
 

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



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