【问题标题】:When a combobox is chosen a openFileDialog must appear选择组合框时,必须出现 openFileDialog
【发布时间】:2011-08-24 13:22:43
【问题描述】:

我已经制定了一个方法,当您单击 Browse From File 时,会出现 openDialogBox。但事实并非如此。怎么了?

这是我打开 OpenFileDialog 的方法:

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

这是我调用 Lista 方法的方法。

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

【问题讨论】:

  • 你有没有尝试过一步一步来看看你能走多远?它是否尝试 openFileDialog2.ShowDialog() 并且对话框没有出现?我对正在发生的事情有点困惑,因为 Lista 只是循环值并检查您的文本。我认为这将进入组合框的事件侦听器。
  • @Josh 我不能跨过或进入任何东西。我想这是因为所有东西都是用鼠标选择的。 Lista 正在检查值,当它找到正确的值(从文件浏览)时,它必须打开 OpenFileDialog。我该如何做事件监听器?
  • 我希望这是一个 DataGridView 而不是一个 DataGrid 或者这可能会变得很奇怪......我想如果你在 Lista 的 for 循环上设置一个断点,你会看到它被命中为选择完 CSV 文件后,而不是在 datagridview 组合框中选择更改时。每个组合框通常都有自己的 SelectedValueChanged 和 SelectedIndexChanged 事件,您可以连接到这些事件。但是,在 DataGridView 中,如果单元格的值是您的浏览文本,您可能必须处理 CellValueChanged 事件并将 OpenFileDialog 放在那里。

标签: c# openfiledialog


【解决方案1】:

DataGridView 通过对所有行只有一个编辑控件。以下是我处理类似情况的方法,首先尝试委托 EditControlShowing 事件

命名空间 WindowsFormsApplication1 { 公共部分类Form1:表格 { 公共表格1() { 初始化组件(); }

    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;

        }

    }
}

}

希望这能解决您的问题

【讨论】:

    猜你喜欢
    • 2016-01-08
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 1970-01-01
    • 2012-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多