如何在datagridviewcombobox中隐藏所选项目 [英] How to hide selected item in datagridviewcombobox
问题描述
我在datagridview中使用了combobox。它连接到数据库。一旦我在位于第一列的第一个单元格的组合框中选择一个项目(tblTime中的项目),就不再能够在第二个单元格的组合框中看到所选项目,该组合框也位于第一列。如果我选择datagridview中的按钮,那就是组合框刷新的时间。或者简而言之,我只想在我的datagridview中隐藏从组合框中选择的项目,以便在下一个单元格中不再选择项目。
任何人都可以帮助我?
void Timeout()
{
conn = new SqlConnection(connec.GetServer());
conn.Open();
string q =从tblTime中选择Distinct TimeOUT,其中Temp ='+f+';
SqlDataAdapter da = new SqlDataAdapter(q,conn);
DataTable dt = new DataTable();
da.Fill(dt);
// Craete组合列
DataGridViewComboBoxColumn combo2 = new DataGridViewComboBoxColumn();
// Assing dataSource
combo2.DataSource = dt;
//分析表组合字段
combo2.DisplayMember =TimeOUT;
combo2.ValueMember =TimeOUT;
combo2.HeaderText =TimeOUT;
//在dataGridView中添加组合框列
dataGridView1.Columns.Insert(2,combo2); // 0是数据库中找到的字段的数组数。
dataGridView1.Refresh();
}
据我所知,组合框项目无法隐藏(我在这里只讨论System.Windows.Forms
);所以你只能暂时删除项目,并在需要时再添加它:DataGridViewComboBoxCell cell = // 某个单元格
int comboBoxListItemIndex = // 某些索引
// ...
cell.Items.RemoveAt(comboBoxListItemIndex);
// ...
cell.Items.Insert(comboBoxListItemIndex,< span class =code-string> some item);
// ...
< span class =code-comment> // 或者您可以通过复制参考来删除某些项目
// 某处,稍后再次插入:
object saveItem = cell.Items [comboBoxListItemIndex];
// 或者它可能是某个类的字段
// ...
cell.Items.Insert(comboBoxListItemIndex,saveItem);
请参阅:
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 .objectcol lection.remove%28v = vs.100%29.aspx [ ^ ]。
- SA
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(); }
To best of my knowledge, combo box items cannot be hidden (I'm talking only aboutSystem.Windows.Forms
here); so you can only remove item temporarily, and add it again when needed: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
这篇关于如何在datagridviewcombobox中隐藏所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!