【发布时间】: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 每个旧文件将有一行。您需要“交叉表”旧文件吗?