Datagridview ComboBoxCell设置默认值?

时间:2012-08-23 11:11:59

标签: c# .net datagridview combobox

我有一个包含大量数据的datagridview,当我添加一个新行时,第一列的最后一行创建一个包含四个项目的新ComboBoxCell。但是我无法为组合框设置默认值(“DropDown”)。每次我必须手动选择“DropDown”。解决方案是什么?

 DataGridViewComboBoxCell dgvCell = new DataGridViewComboBoxCell();
 dgv[1, dgv.Rows.Count - 1] = dgvCell;

 string[] controltype = {"DropDown", "CheckBoxList", "ListControl", "Tree" };
 dgvCell.DataSource = controltype;

3 个答案:

答案 0 :(得分:0)

private void dataGridView_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
    {
        e.Row.Cells[4].Value = "DropDown";
    }

答案 1 :(得分:0)

它很简单,如果你的DataGrid视图中有一个ComboBox列,并且你想知道组合框的选定索引是什么,那么你需要这样做: 1.处理DataGrid View的EditingControlShowing事件。在此事件处理程序中,检查当前列是否符合我们的兴趣。然后我们创建一个临时的ComboBox对象并获取所选的索引:

代码

private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 0)
{
// Check box column
ComboBox comboBox = e.Control as ComboBox;
comboBox.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);
}
}

void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedIndex = ((ComboBox)sender).SelectedIndex;
MessageBox.Show("Selected Index = " + selectedIndex);
}

答案 2 :(得分:-1)

尝试:

if(!isPostBack)
  {
   dgvCell.SelectedItem=controltype[0].toString(); 
  }