我正在使用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;
}