OpenFileDialog没有出现

时间:2011-10-06 09:26:42

标签: c# datagridview combobox openfiledialog

我有这个DataGridView,我想每次你点击浏览文件... 打开一个openFileDialog。

enter image description here

到目前为止这样做但它不起作用。

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

也试过这些但没有:

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

2 个答案:

答案 0 :(得分:0)

一个解决方案是捕获显示数据网格控件的事件( EditingControlsShowing ),并在组合框中添加SelectionChanged处理程序。

这样的事情:

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

修改

为了将事件处理程序添加到网格:转到表单的设计视图,然后右键单击网格。单击上下文菜单中的属性。在属性窗口中,单击“事件”按钮(闪电图像)并搜索EditingControlShowing条目。双击空白区域以添加事件处理程序。在后面的页面代码中,您将看到一个与* dataGridView1_EditingControlShowing *类似的空方法,在该方法中复制/粘贴上述方法中的代码。除此之外,还在同一源文件中复制/粘贴第二种方法cellComboBox_SelectedIndexChanged。

答案 1 :(得分:0)

它应该是什么样的?

if (bbb.equals("Browse From File..."))