向数据集添加数据

时间:2019-10-16 13:32:12

标签: c# visual-studio

我有一个在“ Visual Studio”中创建的数据集。 当我打开数据网格时,它将填充数据网格,并且我可以看到我输入的数据。 如果再次加载表单,数据就消失了

我已使用此代码创建新的数据行。

            foreach (var ticket in esTicket)
            {
                databaseDataSet1.ES.AddESRow(ticket.esNum, ticket.title, ticket.link, ticket.open, ticket.escalated, ticket.downTime, ticket.startWork, ticket.endWork, ticket.toolID, ticket.toolType, ticket.openedBy);
            }

还有:

            foreach (var ticket in esTicket)
            {
                databaseDataSet1.ES.Rows.Add(ticket.esNum, ticket.title, ticket.link, ticket.open, ticket.escalated, ticket.downTime, ticket.startWork, ticket.endWork, ticket.toolID, ticket.toolType, ticket.openedBy);
            }

插入的数据只是不显示

1 个答案:

答案 0 :(得分:0)

您的esTicket似乎是一个列表。如果是这样,我建议您可以使用另一种方法来填充datagridview而不使用数据集。

代码:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            List<Student> list = new List<Student>();
            list.Add(new Student { Age=12,Name="Test1",Title="Hello" });
            list.Add(new Student { Age = 13, Name = "Test2", Title = "Hi" });
            list.Add(new Student { Age = 15, Name = "Test5", Title = "Greeting" });
            dataGridView1.DataSource = list;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<Student> list = (List<Student>)dataGridView1.DataSource;
            list.Add(new Student { Age = 16, Name = "Test6", Title = "Hello1" });
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = list;
        }
    }

    public class Student
    {
        public int Age { get; set; }
        public string Name { get; set; }

        public string Title { get; set; }


    }

结果: enter image description here