在DataGridView中显示列表值

时间:2017-03-31 11:25:33

标签: c# windows-forms-designer

我有ListDataGridView。我在List添加了元素,但当我尝试在List中显示DataGridView值时,它未显示。

public partial class Queries : Form {
    List<EmployeeDetails> lst = new List<EmployeeDetails> { };

    public Queries() {
        InitializeComponent();
    }

    public void btnSubmit_Click(object sender, EventArgs e) {
        EmployeeDetails emp = new EmployeeDetails();
        emp.id = Convert.ToInt32(numId.Value);
        emp.name = tbName.Text;
        emp.malecheck = rbMale.Checked;
        emp.femealecheck = rbFemale.Checked;
        lst.Add(emp);
    }

    private void btnDataGridView_Click(object sender, EventArgs e) {
        LinqdataGridView.DataSource = lst;
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用BindingSource,试试这个;

private void btnDataGridView_Click(object sender, EventArgs e){
    var source = new BindingSource();
    source.Datasource = lst;
    LinqdataGridView.DataSource = source;
}

希望有所帮助,