由于数据源的原因,无法在 DGV 上使用 ofd 列出文件名

发布于 2025-01-13 01:05:02 字数 982 浏览 4 评论 0原文

总的来说,我是编程的初学者。 我的应用程序有一个按钮,列出 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

jJeQQOZ5 2025-01-20 01:05:02
 BindingList<Datei> dateienList = new BindingList<Datei>();

  private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog(); 
            ofd.Filter = "Word(*.docx)| *.docx|PPT(*.pptx)|*.pptx|PDF(*.pdf)|*.pdf|Alle Dateien(*.*)|*.*";
            ofd.Multiselect = true;
            try
            {
                if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    foreach (String path in ofd.FileNames) 
                    {                                      
                        Datei datei = new Datei();  
                        datei.filePath = path; 
                        datei.Dateiname = Path.GetFileName(path);
                     dateienList.Add(datei);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Fehler! Die Datei kann nicht gelesen werden: " + ex.Message);
            }
        }

public class Datei //class as Datasource
 {                     // 
        [DisplayName("Dateiname")] 
        public string Dateiname { get; set; }
       
        public string filePath { get; set; }
      
      
     
    }
}
 BindingList<Datei> dateienList = new BindingList<Datei>();

  private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog(); 
            ofd.Filter = "Word(*.docx)| *.docx|PPT(*.pptx)|*.pptx|PDF(*.pdf)|*.pdf|Alle Dateien(*.*)|*.*";
            ofd.Multiselect = true;
            try
            {
                if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    foreach (String path in ofd.FileNames) 
                    {                                      
                        Datei datei = new Datei();  
                        datei.filePath = path; 
                        datei.Dateiname = Path.GetFileName(path);
                     dateienList.Add(datei);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Fehler! Die Datei kann nicht gelesen werden: " + ex.Message);
            }
        }

public class Datei //class as Datasource
 {                     // 
        [DisplayName("Dateiname")] 
        public string Dateiname { get; set; }
       
        public string filePath { get; set; }
      
      
     
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文