【问题标题】:DataGrid add function to cell buttonDataGrid向单元格按钮添加功能
【发布时间】:2021-05-10 19:59:08
【问题描述】:

大家好,我想问一下如何为dataGridCell中的按钮添加功能,所以当点击按钮打开文件资源管理器时,我选择文件并返回其他单元格中的文件路径。

我想在datagrid单元格“浏览”按钮下添加的功能:

public string test5()
    {
        var fileContent = string.Empty;
        var filePath = string.Empty;

        using (OpenFileDialog openFileDialog = new OpenFileDialog())
        {
            openFileDialog.InitialDirectory = "c:\\users\\Desktop";
            openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog.FilterIndex = 2;
            openFileDialog.RestoreDirectory = true;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                //Get the path of specified file
                filePath = openFileDialog.FileName;
            }
            return filePath;
        }
    }

然后我在按钮加载数据下写了我想在上面使用的这个按钮功能:

 private void button1_Click(object sender, EventArgs e)
    {
        //xDxD();
        //xDxD2();

        //textBox1.Text = xDxD2();

        //this.dataGridView1.Rows.Add("five", "six", "seven", "eight"); 
        this.dataGridView1.Rows.Insert(0, "one", "two", "three", test5());
    }

但是当我点击button1时,它会从这个单元格自动启动功能。

对不起我的英语。

【问题讨论】:

    标签: c# datagrid


    【解决方案1】:

    你离成功很近了

    如果此代码没有帮助,您应该检查一下:Extracting Path from OpenFileDialog path/filename

    但这是我的代码:

    OpenFileDialog openFileDialog = new OpenFileDialog();
    openFileDialog.InitialDirectory = "c:\\users\\Desktop";
    openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
    openFileDialog.FilterIndex = 2;
    openFileDialog.RestoreDirectory = true;
    
    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        var filePath = Path.GetDirectoryName(openFileDialog.FileName);
        textBox1.Text = filePath;
    }
    

    希望你找到了你需要的东西!

    【讨论】:

      猜你喜欢
      • 2019-01-20
      • 2018-04-29
      • 1970-01-01
      • 1970-01-01
      • 2019-09-03
      • 2019-11-20
      • 1970-01-01
      • 2015-09-17
      • 2017-10-04
      相关资源
      最近更新 更多