LV98:
ゴール:
- ファイル名だけではなく、完全なパスとしてセルの値を表示します
- ファイルをクリックの上に配置され、選択したセルの値が、開いているフォルダ
負荷データグリッドビューに現在のコード:
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
、それは完全なパスを探してされていないため、ファイルを...非存在です。
質問:
これらの2つの目標を達成するための良い方法は何ですか?
現在、私は、ファイル名としてセルを表示する方法を理解するのに苦労。しかし、バックグラウンドで完全な値としてそれを表します。どこのユーザーは、完全なパス名を開くことができます。
EveRegalado:
一つの方法は、あなたが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");
}
}