【问题标题】:C# dataGridView show filename only, but open full pathC# dataGridView 仅显示文件名,但打开完整路径
【发布时间】:2020-02-25 14:42:23
【问题描述】:

目标:

  • 将单元格值显示为文件名,而不是完整路径
  • 选定的单元格值,单击打开文件所在的文件夹

加载数据网格视图的当前代码:

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

输出:

打开目录的代码:

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

期望的输出:

只显示文件名而不是显示完整路径的单元格,就像这样:test.csv

单击按钮时,打开完整路径的文件位置。

我尝试过的:

已经将mysqlDataAdapter查询改成使用substring_index来获取文件名,这达到了我的第一个目标。

但是,如果我点击 button1 文件不存在.. 因为它不是在寻找完整路径。

问题:

实现这两个目标的好方法是什么?

目前正在努力理解如何将单元格显示为文件名。但在后台将其表示为一个完整的值。用户可以在哪里打开完整的路径名。

【问题讨论】:

  • 您可以拥有一个包含完整文件路径的隐藏列。
  • @Miamy,如何选择当前选定单元格右侧的值?
  • 您可以将DataGridViewCell.ColumnIndex 属性用于CurrentCell
  • 如果单行中的文件有不同的位置,显然应该有 10 个隐藏列。我建议在可见列之后创建具有完整路径的隐藏列。所以你总是可以通过CurrentCell.ColumnIndex + 1 来解决它
  • 您能否改为返回一组结果,其中一列用于文件名,另一列用于文件路径。 DGV 每个旧文件将有一行。您需要“交叉表”旧文件吗?

标签: c# mysql


【解决方案1】:

一种方法是在 C# 代码中获取文件名,同时将地址保存在单元格属性中:

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

这是你打开目录的修改代码:

打开目录

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

【讨论】:

  • System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index' - 我会看看你的代码,看看发生了什么。
  • 在哪一部分?因为我没有看到为索引分配负值
  • 突出显示此代码:dataGridView1.Columns[colidx].HeaderText = "Old File " + (colidx + 1).ToString();
  • for 语句从 0 到 9,你的数据返回多少列?因为 0 到 9 在手动操作中是相同的
  • 绝对是 0 -9。我自己也糊涂了哈哈!但这种方法正如我所要求的那样有效。
猜你喜欢
  • 2022-07-15
  • 1970-01-01
  • 2010-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-19
  • 2016-08-16
相关资源
最近更新 更多