C# - 使用textdocument将行添加到现有dataGridView

时间:2017-08-28 15:07:37

标签: c# datagridview

我目前正在使用dataGridView1加载事件向我的Form1_Load添加数据。现在我尝试从文本文件中添加更多数据,这些数据加载到winforms应用程序中。

正如大家们所看到的,我试图在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;
}

3 个答案:

答案 0 :(得分: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控件中读取业务数据,这是令人讨厌的。

答案 1 :(得分:0)

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

table = new DataTable();

然后在GetTexFilePath的foreach循环中使用以下内容:

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

答案 2 :(得分: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; }
    }


}