由于数据源的原因,无法在 DGV 上使用 ofd 列出文件名
总的来说,我是编程的初学者。 我的应用程序有一个按钮,列出 DGV 上选定文件的名称。我使用 DGV 任务在 Windows 窗体设计器上添加了一行文件名。代码如下:
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Word(*.docx)| *.docx|PPT(*.pptx)|*.pptx|PDF(*.pdf)|*.pdf|Alle Dateien(*.*)|*.*";
ofd.Multiselect = true;
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string[] dateinamen = ofd.SafeFileNames;
for (int i = 0; i < ofd.FileNames.Count() - 1; i++)
{
dataGridView1.Rows.Add(dateinamen[i]);
}
}
}
创建这个按钮后,我创建了一个类作为该DGV的数据源(在Windows Forms Design -> DGV任务 -> 选择数据源 -> 对象 -> 我创建的类) 然后,我尝试使用上面提到的按钮打开文件以列出其名称。我收到以下消息: “当控件进行数据绑定时,无法以编程方式将行添加到 DataGridView 的行集合中”
我可以理解为什么,并且我想解决这个问题。我想,最好的选择是将 ofd 代码放入 Datasource 类,但我不知道如何做。我什至不确定这是否正确。如果没有,如果我找到正确的方法来解决这个问题,那就太好了。
提前致谢!
I am a beginner in programming in general.
My app has a button listing names of selected files on DGV. I added a row for file names on windows Forms Designer using DGV Tasks. The code is as follows:
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Word(*.docx)| *.docx|PPT(*.pptx)|*.pptx|PDF(*.pdf)|*.pdf|Alle Dateien(*.*)|*.*";
ofd.Multiselect = true;
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string[] dateinamen = ofd.SafeFileNames;
for (int i = 0; i < ofd.FileNames.Count() - 1; i++)
{
dataGridView1.Rows.Add(dateinamen[i]);
}
}
}
After creating this button, I made a class to use as data source of this DGV(on windows Forms Design -> DGV tasks -> Choose data source -> Object -> Class I created)
Then, I tried to open files to list their names using the button I mentioned above. And I get the following message:
"Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound"
I can kinda understand why, and I want to fix this. Best option would be, I guess, to put ofd codes to Datasource class, but I have no idea how. I'm not even sure if it'd be right. If not, it'll be great if I get the right way to fix this problem.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)