2017-08-28 73 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); 
相關問題