0

Hello all I wanted to ask how I can add function to button in dataGridCell, so when click button it open file explorer, I select file and it return file path in other cell.

The function I want add under button of datagrid cell "Browse":

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

then I wrote under button load data with this button function I want use above:

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

But when I click button1, then it auto start function from this cell.

Sorry for my english.

1 Answers1

0

You were very close to succeeding

If this code is not helpful you should check this out: Extracting Path from OpenFileDialog path/filename

But here is my code:

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

Hope you found what you need!