2017-08-28 113 views
0

我正在使用Form1_Load加载事件将数据添加到我的dataGridView1。现在我试图从一个文本文件中添加更多的数据,这个文件将被加载到winforms应用程序中。C# - 使用文本文档将行添加到现有的dataGridView

正如你们会看到的,我试图增加更多的行到dataGridView1,但这些新行不会被添加。我究竟做错了什么?

我很欣赏任何形式的建议和帮助。

getTexFilePath功能代码:

private void getTexFilePath() 
{ 
    OpenFileDialog openFileDialog1 = new OpenFileDialog(); 
    openFileDialog1.InitialDirectory = @"C:\"; 
    openFileDialog1.Title = "Browse Text Files"; 

    openFileDialog1.CheckFileExists = true; 
    openFileDialog1.CheckPathExists = true; 

    openFileDialog1.DefaultExt = "txt"; 
    openFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; 
    openFileDialog1.FilterIndex = 2; 
    openFileDialog1.RestoreDirectory = true; 

    openFileDialog1.ReadOnlyChecked = true; 
    openFileDialog1.ShowReadOnly = true; 

    if (openFileDialog1.ShowDialog() == DialogResult.OK) 
    { 
     filePath = openFileDialog1.FileName; 

     foreach (var line in File.ReadAllLines(filePath)) 
     { 
      var index = dataGridView1.Rows.Add(); 
      dataGridView1.Rows[index].Cells["Column1"].Value = line; 
      dataGridView1.Rows[index].Cells["Column2"].Value = "undefined"; 
     } 

    } 
} 

Form1_Load的代码:

private void Form1_Load(object sender, EventArgs e) 
{ 
    DataTable table = new DataTable(); 
    table.Columns.Add("Username", typeof(string)); 
    table.Columns.Add("Links"); 

    table.Rows.Add("No File uploaded", "Missing data"); 

    dataGridView1.DataSource = table; 
} 

回答

0

不知道你的所有要求,这将是在那里我开始。

using System.ComponentModel; 
using System.Windows.Forms; 
using System.IO; 

namespace DatagridView_AddRowsAfterInitialLoad_45922121 
{ 
    public partial class Form1 : Form 
    { 
     string filePath = ""; 

     BindingList<dgventry> dgvSourceList = new BindingList<dgventry>(); 

     public Form1() 
     { 
      InitializeComponent(); 
      InitializeDGV(); 
      //put initial values in the grid 
      dgvSourceList.Add(new dgventry { field1 = "yeah", field2 = "yeah in f2", field3 = "yeah in F3" }); 
      //put values from the datafile 
      getTexFilePath(); 
     } 

     private void InitializeDGV() 
     { 
      dataGridView1.DataSource = dgvSourceList; 
     } 

     private void getTexFilePath() 
     { 
      OpenFileDialog openFileDialog1 = new OpenFileDialog(); 
      openFileDialog1.InitialDirectory = @"C:\"; 
      openFileDialog1.Title = "Browse Text Files"; 

      openFileDialog1.CheckFileExists = true; 
      openFileDialog1.CheckPathExists = true; 

      openFileDialog1.DefaultExt = "txt"; 
      openFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; 
      openFileDialog1.FilterIndex = 2; 
      openFileDialog1.RestoreDirectory = true; 

      openFileDialog1.ReadOnlyChecked = true; 
      openFileDialog1.ShowReadOnly = true; 

      if (openFileDialog1.ShowDialog() == DialogResult.OK) 
      { 
       filePath = openFileDialog1.FileName; 

       foreach (var line in File.ReadAllLines(filePath)) 
       { 
        dgvSourceList.Add(new dgventry { field1 = line, field2 = "", field3 = "" }); 
       } 
      } 
     } 
    } 

    public class dgventry 
    { 
     public string field1 { get; set; } 
     public string field2 { get; set; } 
     public string field3 { get; set; } 
    } 


} 
0

Form1_Load您使用的数据源(DataTable实例),所以您的网格数据绑定的(手段中,网格反映其来源的内容)。

但是,在getTexFilePath中,您只是尝试以非绑定方式将行添加到网格本身。

解决方案1 ​​

您应该添加新行到底层DataTable代替。

解决方案2

创建一个新表并重新绑定:还

dataGridView1.DataSource = null; 

DataTable newDataTable = ReadFileAsDataTable(fileName); // implement this 
dataGridView1.DataSource = newDataTable; 

解决方案3

使用非绑定网格在Load事件,类似于你getTexFilePath。但不建议这样做,因为在这种情况下,您没有一个干净的UI独立模型对象(数据源),并且只能从UI控件读取业务数据,这非常不便。

0

处理此问题的简单方法是将DataTable表作为表单类的成员。所以,在你的Form_Load只是有:

table = new DataTable(); 

然后在GetTexFilePath您的foreach循环使用以下命令:

DataRow dR = table.NewRow(); 
dR[0] = line; 
dR[1] = "undefined"; 
table.rows.Add(dR); 
相关问题