通过C#代码添加到dataGridView

时间:2014-04-11 13:36:50

标签: c# datagridview

我想通过使用新的BindingSource每次计数器增加新行时添加 但它添加了行"最后一次修改"

这是我的代码:

     public class PacketWrapper
    {
        public string p;

        public int Count { get; set; }
        public string Timeval { get { return "l"; } }
        public string LinkLayerType { get { return"2"; } }
        public int Length { get { return p.Length; } }

        public PacketWrapper(int count, string p)
        {
            this.Count = count;
            this.p = p;
        }
    }
    BindingSource bs = new BindingSource();

    PacketWrapper p;
    private void Form1_Load(object sender, EventArgs e)
    {
        dataGridView1.DataSource = bs;
        p = new PacketWrapper(1, "djd");

        Timer MyTimer = new Timer();
        MyTimer.Interval = (450); // 45 mins
        MyTimer.Tick += new EventHandler(MyTimer_Tick);

        MyTimer.Start();

    }

    private void MyTimer_Tick(object sender, EventArgs e)
    {
        for (int i = 1; i < 11; i++)
        {
            p.Count = i;

            bs.DataSource = p;

        }
    }

如何使用此代码每次添加新行?

1 个答案:

答案 0 :(得分:0)

替换

bs.DataSource = p;

bs.Add(p);