问题
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