2011-08-24 57 views
0

我已经制作了一个方法,以便当您单击从文件浏览时,会出现openDialogBox。但事实并非如此。哪里不对 ?当选择组合框时,必须出现openFileDialog

enter image description here

这是我作出打开打开文件对话框的方法:

public void Lista() 
     { 
      string[] col2 = new string[dataGridView1.Rows.Count]; 

      for (int i = 0; i < dataGridView1.Rows.Count; i++) 

        if (col2[i] == "Browse From File...") 
        { 
         DialogResult result2 = openFileDialog2.ShowDialog(); 
         if (result2 == DialogResult.OK) 
         { 
          // filename = openFileDialog1.FileName; 
         } 
     } 
     } 

这就是我所说的方法利斯塔的方法。

private void button1_Click(object sender, EventArgs e) 
      { 
       // opens window **BROWSE** 

       openFileDialog1.Title = "Choose File CSV "; 

       string filename = ""; 
       DialogResult result = openFileDialog1.ShowDialog(); 
       if (result == DialogResult.OK) 
       { 
        filename = openFileDialog1.FileName; 

        textBox1.Text = filename; 






        string line; 
        // Read the file and display it line by line. 


        System.IO.StreamReader file = new System.IO.StreamReader(textBox1.Text); 

        stringforData = file.ReadLine();  
        while ((line = file.ReadLine()) != null) 
        { 
         //read inside the table 
         fileList.Add(line.Split(';')); 
        } 

        file.Close(); 



        this.ToDataGrid(); 
        this.Lista(); 
       } 
      } 
+0

您是否尝试过通过看你走多远步进?它尝试openFileDialog2.ShowDialog()并且对话框不出现?我对发生的事情有点困惑,因为Lista只是循环使用值并检查文本。我会认为这将进入组合框的事件监听器。 – Josh

+0

@Josh我不能跨过或进入任何东西。我想这是因为阿拉这个东西是用鼠标选择的。 Lista正在检查值以及何时找到正确的值(Browse From File),它必须打开OpenFileDialog。我该如何做事件监听器呢? –

+1

我希望这是一个DataGridView,而不是一个DataGrid,或者这可能会变得很奇怪... 我想如果你在Lista的for循环中设置一个断点,你会发现一旦你完成选择您的CSV文件,而不是在datagridview组合框中选择更改时。 每个组合框通常都会有它自己的SelectedValueChanged和SelectedIndexChanged事件,您可以将它们连接起来。但是,在DataGridView中,如果单元格的值是您的浏览文本,您可能必须处理CellValueChanged事件并将OpenFileDialog放在那里。 – Josh

回答

0

DataGridView仅对所有行具有一个编辑控件。下面是我如何处理类似的情况,第一次尝试代表对EditControlShowing事件

命名空间WindowsFormsApplication1 { 公共部分Form1类:表格{ 公共 Form1的(){ 在InitializeComponent (); }

OpenFileDialog ofd = new OpenFileDialog(); 
    private void Form1_Load(object sender, EventArgs e) 
    { 
    dataGridView1.Columns.Add("ID", "Product ID"); 

    DataGridViewComboBoxColumn comboxboxCol = new DataGridViewComboBoxColumn(); 
    comboxboxCol.HeaderText = "Type"; 
    comboxboxCol.Items.Add("Obj1"); 
    comboxboxCol.Items.Add("Obj2"); 
    comboxboxCol.Items.Add("Obj3"); 

    dataGridView1.Columns.Add(comboxboxCol); 

    dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(Grid_EditingControlShowing); 
    } 

    void Grid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
    { 
     ComboBox combo = e.Control as ComboBox; 
     if (combo != null) 
     { 
      // the event to handle combo changes 
      EventHandler comboDelegate = new EventHandler(
       (cbSender, args) => 
       { 
        ofd.ShowDialog(); 
       }); 

      // register the event with the editing control 
      combo.SelectedValueChanged += comboDelegate; 

     } 

    } 
} 

}

希望这将解决您的问题