【发布时间】:2021-05-10 19:59:08
【问题描述】:
大家好,我想问一下如何为dataGridCell中的按钮添加功能,所以当点击按钮打开文件资源管理器时,我选择文件并返回其他单元格中的文件路径。
我想在datagrid单元格“浏览”按钮下添加的功能:
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;
}
}
然后我在按钮加载数据下写了我想在上面使用的这个按钮功能:
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());
}
但是当我点击button1时,它会从这个单元格自动启动功能。
对不起我的英语。
【问题讨论】: