Reputation: 113
I have a datagridview which contain a Combobox Column i want when i select a add value from the combobox it shows a new form. i tried this code but it doesn't work:
private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
switch (dataGridView2.Columns[e.ColumnIndex].Name)
{
case "CategorieDataGridViewTextBoxColumn":
if (dataGridView2.Rows[e.RowIndex].Cells["CategorieDataGridViewTextBoxColumn"].Value.ToString() == "Add")
{
Categorie cat = new Categorie();
cat.Show();
}
break;
}
}
So how can i do it??
Upvotes: 0
Views: 123
Reputation: 1473
You should handle the event when a value is changed in a ComboBox in a DataGridView cell. try this code which will fire the event of the selection in the comboBox in the dataGridView:
public Form1()
{
InitializeComponent();
DataGridViewComboBoxColumn cmbcolumn = new DataGridViewComboBoxColumn();
dataGridView2.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView2_EditingControlShowing);
}
private void dataGridView2_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox combo = e.Control as ComboBox;
if (combo != null)
{
combo.SelectedIndexChanged -= new EventHandler(ComboBox_SelectedIndexChanged);
combo.SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);
}
}
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox cb = (ComboBox)sender;
string item = cb.Text;
if (item == "Add")
{
Categorie cat = new Categorie();
cat.Show();
}
}
Upvotes: 3