DeveloperLV
DeveloperLV

Reputation: 1781

C# dataGridView show filename only, but open full path

Goal:

Current Code to load data grid view:

    private void Form14_Load(object sender, EventArgs e)
    {
        int select = Convert.ToInt32(f9.dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
        // MySQL connection string
        using (var conn = new MySqlConnection(ConnectionString()))
        {
            using (var mySqlDataAdapter = new MySqlDataAdapter(@"select file_attachment1, file_attachment2,file_attachment3,file_attachment4,file_attachment5,file_attachment6,file_attachment7,file_attachment8,file_attachment9,file_attachment10 from document_control  where id = " + select + "", conn))
            {
                using (var dataSet = new DataSet())
                {
                    DataSet DS = new DataSet();
                    mySqlDataAdapter.Fill(DS);
                    dataGridView1.DataSource = DS.Tables[0];
                    dataGridView1.Columns[0].HeaderText = "Old File 1";
                    dataGridView1.Columns[1].HeaderText = "Old File 2";
                    dataGridView1.Columns[2].HeaderText = "Old File 3";
                    dataGridView1.Columns[3].HeaderText = "Old File 4";
                    dataGridView1.Columns[4].HeaderText = "Old File 5";
                    dataGridView1.Columns[5].HeaderText = "Old File 6";
                    dataGridView1.Columns[6].HeaderText = "Old File 7";
                    dataGridView1.Columns[7].HeaderText = "Old File 8";
                    dataGridView1.Columns[8].HeaderText = "Old File 9";
                    dataGridView1.Columns[9].HeaderText = "Old File 10";
                }
            }
        }
    }

Output:

enter image description here

Code to open the directory:

private void button1_Click(object sender, EventArgs e)
{
    if (this.dataGridView1.CurrentCell != null)
    {
        Cursor.Current = Cursors.WaitCursor;

        int select = Convert.ToInt32(f9.dataGridView1.SelectedRows[0].Cells[0].Value.ToString());

        string file = dataGridView1.CurrentCell.Value.ToString();
        Cursor.Current = Cursors.Default;

        if (File.Exists(file))
        {
            Cursor.Current = Cursors.WaitCursor;
            Process.Start("explorer.exe", " /select, " + file);
            Cursor.Current = Cursors.Default;
        }
        else
        {
            MessageBox.Show("No File Found...");
        }

    }
    else
    {
        MessageBox.Show("No record selected");
    }
}

Desired Output:

Instead of cells to show the full path, show just the filename, Like so: test.csv.

On button click, open the file location of full path.

What I have tried:

Have already altered mysqlDataAdapter query to use substring_index to get the filename, which achieves my first goal.

But, if I click button1 file is non existent.. because it is not looking for full path.

Question:

What is a good method to achieve these 2 goals?

Currently struggling to understand, how I can display cells as filename. But in the background represent it as a full value. Where user will be able to open the full path name.

Upvotes: 1

Views: 607

Answers (1)

EveRegalado
EveRegalado

Reputation: 115

One way is that you get the name of the file in C # code and at the same time save the address in the cell property:

Current Code to Load DGV:

private void Form14_Load(object sender, EventArgs e)
{
    // You previus code here ...
    // Here is you new modify code:
    using (var dataSet = new DataSet())
    {
        DataSet DS = new DataSet();
        mySqlDataAdapter.Fill(DS);
        dataGridView1.DataSource = DS.Tables[0];

        for(int colidx=0; colidx<dataGridView1.Columns.Count; colidx++) // You have index from 0 to 9
        {
            // if you use C# 7 you use: 
            // dataGridView1.Columns[colidx].HeaderText = $"Old File {(colidx+ 1).ToString()};
            dataGridView1.Columns[colidx].HeaderText = "Old File " + (colidx+ 1).ToString();

            // The magic:
            for(int rowidx = 0; rowidx < dataGridView1.Rows.Count; rowidx++)
            {
                string filepathcell = dataGridView1.Rows[rowidx].Cells[colidx].Value.ToString();
                // Only filename, remember: implements "using System.IO;" and this may launch exception, be careful
                dataGridView1.Rows[rowidx].Cells[colidx].Value = Path.GetFileName(filepathcell);
                // Save full pathfile:
                dataGridView1.Rows[rowidx].Cells[colidx].Tag = filepathcell;
            }
        }
    }
}

And this is your modify code to open directory:

Open Dir

private void button1_Click(object sender, EventArgs e)
{
    if (this.dataGridView1.CurrentCell != null)
    {
        Cursor.Current = Cursors.WaitCursor;

        // This is the only line modified
        string file = dataGridView1.CurrentCell.Tag.ToString();
        Cursor.Current = Cursors.Default;

        if (File.Exists(file))
        {
            Cursor.Current = Cursors.WaitCursor;
            Process.Start("explorer.exe", " /select, " + file);
            Cursor.Current = Cursors.Default;
        }
        else
        {
            MessageBox.Show("No File Found...");
        }

    }
    else
    {
        MessageBox.Show("No record selected");
    }
}

Upvotes: 1

Related Questions