DataGridViewComboBoxCell

DataGridViewComboBoxCell

本文介绍了我如何在DataGridViewComboBoxCell中设置默认项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在datagridview中有一个DataGridViewComboBoxCell。我在form_load中加载了它的值。它最初在DataGridViewComboBoxCell中显示空白,当添加新行时。但是我的问题是如何设置Default值,例如其他在DataGridViewComboBoxCell.So中,每当我尝试添加新行时,它将显示其中的默认项。



我想向您显示我的代码。 >

DataGridViewComboBoxCell comboItem_current = dataGridView1.CurrentRow.Cells [2]作为DataGridViewComboBoxCell;

  comboItem_current.Items.Clear(); 
comboItem_current.Items.Add(Other);

foreach(DataRow dr in ds_Item.Tables [0] .Rows)
{
DataGridViewComboBoxCell comboItem1 = dataGridView1.CurrentRow.Cells [2]作为DataGridViewComboBoxCell;
comboItem1.Items.Add(dr [1] .ToString());

}

请帮助...谢谢

解决方案

我知道这是一个古老的帖子,但希望我可以帮助一些人避免我的困惑。



使用CellFormatting是一个失败者,因为它每次触摸单元格时都会调用它。结果是这个值不断地恢复为默认值。



对我来说有用的是处理DefaultValuesNeeded事件,如下所示:

  private void OnGridDefaultValuesNeeded(object sender,DataGridViewRowEventArgs e)
{
e.Row.Cells [Position]。Value = PositionEnum.Any ;
}

这允许我设置默认值,并让用户更改值。


I'm having a DataGridViewComboBoxCell in a datagridview.I'm loading values in it at form_load.It shows blank in DataGridViewComboBoxCell initially,when new row is added.But my problem is how do i set Default value, say "Others" in that DataGridViewComboBoxCell.So that whenever i try to add new row it will show default item in it.

I would like to show my code to you.

DataGridViewComboBoxCell comboItem_current = dataGridView1.CurrentRow.Cells[2] as DataGridViewComboBoxCell;

                comboItem_current.Items.Clear();
                comboItem_current.Items.Add("Other");

                foreach (DataRow dr in ds_Item.Tables[0].Rows)
                {
                    DataGridViewComboBoxCell comboItem1 = dataGridView1.CurrentRow.Cells[2] as DataGridViewComboBoxCell;
                    comboItem1.Items.Add(dr[1].ToString());

                }

please help out....thanks

解决方案

I know this is an ancient post, but hopefully, I can help some people avoid my confusion.

Using CellFormatting is a loser because it calls it every time anything touches the cell. The result is that the value gets set back to the default constantly.

What worked for me was handling the DefaultValuesNeeded event like so:

private void OnGridDefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
{
    e.Row.Cells["Position"].Value = PositionEnum.Any;
}

This allowed me to set the default value, and lets the user change the value.

这篇关于我如何在DataGridViewComboBoxCell中设置默认项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-07 22:25