I have this DataGridView and I want every time you click on the Browse From File... an openFileDialog to open.
Made this so far but it does not work.
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
string bbb = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
开发者_运维问答 if (bbb == "Browse From File...")
{
openFileDialog2.ShowDialog();
}
Tried also these but nothing:
if (e.ColumnIndex.Equals = "Browse From File...")
if (dataGridView1.SelectedCells = "Browse From File...")
if ((string)dataGridView1.SelectedCells[0].Value == "Browse From File...")
if (dataGridView1.Rows[1].Cells["Browse From File..."].Value.ToString() == "Browse From File...")
{
//openFileDialog2.ShowDialog();
}
Is it what it's supposed to be?
if (bbb.equals("Browse From File..."))
One solution would be to catch the event where the controls from the datagrid are shown (EditingControlsShowing) and add SelectionChanged handler on the combo box.
Something like this:
private void dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is ComboBox)
{
ComboBox cellComboBox = (ComboBox)e.Control;
if (cellComboBox != null)
{
cellComboBox.SelectedIndexChanged += new EventHandler(cellComboBox_SelectedIndexChanged);
}
}
}
void cellComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
DataGridViewComboBoxEditingControl comboBox = sender as DataGridViewComboBoxEditingControl;
if (comboBox != null)
{
if (String.Compare(comboBox.Text, "Browse From File...") == 0)
{
openFileDialog.ShowDialog();
}
}
}
Edit
In order to add the event handler to the grid: Go to design view for your form and right click on the grid. Click properties in the context menu. In the properties window click on the Events button (the lightning image) and search the EditingControlShowing entry. Double click the empty space to add the event handler. In the page code behind you'll see an empty method similar with *dataGridView1_EditingControlShowing*, in that method copy/paste the code from the above method. Beside that also copy/paste in the same source file the second method cellComboBox_SelectedIndexChanged.
精彩评论