如何防止组合框相同的项目选择多次 [英] How to prevent combobox same item selection more than one time

查看:90
本文介绍了如何防止组合框相同的项目选择多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含comobobox的数据网格;组合框是绑定数据源。

现在我想阻止用户不要在datagirdView中多次选择相同的组合框项目。

i想要限制他没有在gridview的组合框中选择相同的项目。

解决方案

先生,你必须循环遍历以前的所有行这里的值非常简单保存并检查该值是否与当前选定的值匹配。如果匹配则更改组合框的列索引。


进一步解决方案1 ​​...



你应该循环 DataGridView上的所有行,因为用户没有义务按照您认为应该的顺序输入数据! ;-)



验证应该进入 CellValidating 事件。



正如Agent_Spock建议的那样,一种方法是使用 foreach 循环,如此

  //  使用DataGridView循环...  
foreach (DataGridViewRow r in ((DataGridView)sender).Rows)
{
if ((r.Cells [ comboBoxColumn]。值?? )。ToString()。Equals(e.FormattedValue.ToString()))
{
// 将错误信息放在此处
e.Cancel = ; // 阻止接受该值
}
}



但是,现在我开始越来越多地使用Linq了,我更喜欢这个搜索(但你已经标记了C#3.5,所以可能无法做到这一点)

  //  使用Linq ...  
IEnumerable< DataGridViewRow> rows = dataGridView1.Rows
.Cast< DataGridViewRow>()
.Where(r = > (r.Cells [ comboBoxColumn]。值?? )的ToString()等于(e.FormattedValue.ToString()));
if (rows.Count()> 0 // 即结果从搜索中返回
{
// 将错误消息放在此处
e.Cancel = true ; // 阻止接受该值
}



请注意,您需要将comboBoxColumn更改为包含组合框的列的名称,并且您将需要使用System。 Linq; 如果你使用Linq方法。



注意

(r.Cells [comboBoxColumn]。值??)。ToString()

如果您尝试访问其中没有任何内容的单元格的,您将获得 NullReferenceException

Quote:

对象引用未设置为对象的实例。

?? null-coalescing operator [ ^ ] - 在此case我使用空字符串来避免异常。

最后,这个事件可以在用户从列表中选择任何内容之前被解雇(例如他们点击它但改变主意)所以我用

 如果(e.FormattedValue.ToString()。长度>包围我的验证。  0 
{
...
}


i have a datagrid which contain a comobobox; combo box is Bound with a Data Source.
Now i want to prevent user to not select same combobox item more than one time in datagirdView.
i want to restrict him that he not selected same item in combobox of gridview.

解决方案

sir, it is quite simple you have to loop through all previous rows where the value are saved and check if that value matches with the current selected value. If it matches then change the column index of the combobox.


Further to Solution 1 ...

You should loop through all rows on the DataGridView because Users are not obliged to enter data in the order you think they ought to! ;-)

The validation should go into the CellValidating event.

As Agent_Spock suggested, one way of doing it is to use a foreach loop like this

// using a loop through the DataGridView...
foreach (DataGridViewRow r in ((DataGridView)sender).Rows)
{
    if ((r.Cells["comboBoxColumn"].Value ?? "").ToString().Equals(e.FormattedValue.ToString()))
    {
        // Put your error message here
        e.Cancel = true;    // prevent the value being accepted
    }
}


However, now I'm starting to use Linq more and more, I prefer this search instead (but you've tagged C#3.5 so may not be able to do this)

// Using Linq...
IEnumerable<DataGridViewRow> rows = dataGridView1.Rows
        .Cast<DataGridViewRow>()
        .Where(r => (r.Cells["comboBoxColumn"].Value ?? "").ToString().Equals(e.FormattedValue.ToString()));
if (rows.Count() > 0)   // I.e. results were returned from the search
{
    // Put your error message here
    e.Cancel = true; // prevent the value being accepted
}


Note that you will need to change "comboBoxColumn" to the name of your column containing the comboBoxes and you will need using System.Linq; if you use the Linq method.

Notice the

(r.Cells["comboBoxColumn"].Value ?? "").ToString()

If you attempt to access the Value of a cell that has nothing in it, you will get a NullReferenceException

Quote:

Object reference not set to an instance of an object.

The ?? is the null-coalescing operator[^] - in this case I'm using an empty string to avoid the exception.
Finally, this event can get fired before the User has selected anything from the list (e.g. they click on it but change their mind) so I surrounded my validation with

if (e.FormattedValue.ToString().Length > 0)
{ 
    ...
}


这篇关于如何防止组合框相同的项目选择多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆