Datagridview ComboBoxCell set default value?

老子叫甜甜 提交于 2019-12-25 11:54:21

问题


I have a datagridview with lots of data, and when I add a new line, the first column's last row creates a new ComboBoxCell which contain four items. But I can't set the default value ("DropDown") for the combobox. Every time I must manually select "DropDown". What is the solution?

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

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

回答1:


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



回答2:


it,s easy,If you have a ComboBox Column in your DataGrid View and you want to know what is the selected index of the combo box, then you need to do this: 1. Handle the EditingControlShowing event of DataGrid View. In this event handler, check if the current column is of our interest. Then we create a temporary ComboBox object and get the selected index:

Code

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);
}



回答3:


try :

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


来源:https://stackoverflow.com/questions/12090286/datagridview-comboboxcell-set-default-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!