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