时间:2011-04-02 18:49:53

标签: c# data-binding datagridview

如何在使用关系时在DataGridView中添加新行?

DataGridView绑定到DataTable时,我可以添加一行,但是当我m using relations, nothing happens, the row won't add to the DataGridView`时。

1 个答案:

答案 0 :(得分:0)

        DataSet ds = new DataSet();
        DataTable dt = new DataTable();

        dt.Columns.Add("ID", typeof(int));
        dt.Columns.Add("UserName",typeof(string));

        ds.Tables.Add(dt);
        dataGridView1.DataSource = dt;

        DataRow dr = dt.NewRow();
        dr["ID"] = 1;
        dr["UserName"] = "maxWhite";
        dt.Rows.Add(dr);
        dataGridView1.DataSource = dt;

在此之前我使用关系,ds.Relations.Add(new DataRelation .......... 在此之后我不能添加行             dataGridView3.DataSource = ds.Tables [0];             dataGridView3.DataMember =“relation_name”;

因为我必须使用数据绑定来过滤掉我的结果,例如。我点击父数据网格1,在数据网格2中我看到子行

e.g。我点击父网格福特儿童网格显示所有福特模型,然后马自达等等,我不能添加行到子网格,因为我有关系,当我删除关系所有我很好。