Click here to Skip to main content
15,914,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using combobox inside my datagridview. It is connected to the database. Once I select an item (items in tblTime) in the combobox at first cell located at first column, the item selected should not anymore be seen in the combobox at the second cell which is also located at the first column. If I select the button inside the datagridview, that is the time that the comboboxes were refresh. Or in short, I just wanted to hide the item selected from the combobox inside my datagridview for the item not to be selected again in the next cells.

Can anyone help me?

void Timeout()
        {
            conn = new SqlConnection(connec.GetServer());
            conn.Open();
            string q = "select Distinct TimeOUT from tblTime Where Temp= '" +"f"+ "'";
            SqlDataAdapter da = new SqlDataAdapter(q, conn);

            DataTable dt = new DataTable();
            da.Fill(dt);
            //Craete combo column

            DataGridViewComboBoxColumn combo2 = new DataGridViewComboBoxColumn();

            //Assing dataSource
            combo2.DataSource = dt;

            //Assing table Field on combo

            combo2.DisplayMember = "TimeOUT";
            combo2.ValueMember = "TimeOUT";
            combo2.HeaderText = "TimeOUT";
           
            //Adding combo box column in dataGridView

            dataGridView1.Columns.Insert(2, combo2); // 0 is the number of array to the field found in the database.
            dataGridView1.Refresh();


         
        }
Posted
Updated 19-Jan-15 10:03am
v2
Comments
BacchusBeale 19-Jan-15 15:43pm    
Please provide more details and code sample. It sounds like tblPlace is a fixed list of options and both comboboxes list data from the same table, and you want to prevent user choosing same place twice. Correct?
DarkDreamer08 19-Jan-15 16:04pm    
Yes, the table is a fixed list of options. and you are right about my problem.

1 solution

To best of my knowledge, combo box items cannot be hidden (I'm talking only about System.Windows.Forms here); so you can only remove item temporarily, and add it again when needed:
C#
DataGridViewComboBoxCell cell = // some cell
int comboBoxListItemIndex = // some index
// ...
cell.Items.RemoveAt(comboBoxListItemIndex);
// ...
cell.Items.Insert(comboBoxListItemIndex, "some item");
// ...


// Or you can preserve some item before deleting by copying the reference
// somewhere and later insert it again:

object saveItem = cell.Items[comboBoxListItemIndex];
// or it could be some class's field
// ...
cell.Items.Insert(comboBoxListItemIndex, saveItem);


Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcell.objectcollection.insert%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcell.objectcollection.remove%28v=vs.100%29.aspx[^].

—SA
 
Share this answer
 
v3
Comments
DarkDreamer08 19-Jan-15 15:45pm    
I got a little bit confused to the link that you gave after reading it. Can you show me a sample code about it? Let say for example is my code in the datagridview combobox . Can you help me about it?
Sergey Alexandrovich Kryukov 19-Jan-15 16:05pm    
Honestly, I don't know what sample could help you, but I tried to put some. Please see the sample in updated answer above.
Does it makes things clearer to you? If not, just try to play with it, and if you have some problem, ask your follow-up questions.

Will you now accept the answer formally?

—SA
DarkDreamer08 19-Jan-15 19:56pm    
Thanks for that. I will try that. I will mark it as an answer :) Thanks again!
Sergey Alexandrovich Kryukov 19-Jan-15 20:04pm    
You are very welcome.
Good luck, call again.
—SA

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