使用DataSource向datagridview添加行

时间:2013-03-29 07:19:17

标签: datagridview

我有一个用C#编写的窗口应用程序。我里面有一个datagridview。这是代码:

private List<Tablet> tabletList = new List<Tablet>();
...

private void tabControl1_Selected(object sender, TabControlEventArgs e)
{
    GetTabletList();
}

void GetTabletList()
{
    Tablet newTablet = new Tablet();
    newTablet.xxx = yyy;
    newTablet.xxx2 = yyy2;
    tabletList.Add(newTablet);

    dataGridView.DataSource = tabletList;
    Console.WriteLine(tabletList.Count);
}

public class Tablet
{
    public string xxx { get; set; }
    public string xxx2 { get; set; }
}

每次调用GetTabletList()时,值tabletList.Count都会不断增加,但我只能在datagridview中看到1行。怎么了?

1 个答案:

答案 0 :(得分:0)

尝试ObservableCollection<Tablet>代替List<Tablet>