【问题标题】:C# add new row to bound datagridview using relationsC#使用关系将新行添加到绑定的datagridview
【发布时间】:2011-04-02 18:49:52
【问题描述】:

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

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

【问题讨论】:

  • 更新绑定后如果没有看到添加的行,则需要刷新 Datagridview
  • datagridview.update();和 datagridview.refresh();不工作
  • 你能给我们看一下代码吗,也许我们可以看到它并帮助你
  • 数据集 ds = new DataSet();数据表 dt = 新数据表(); dt.Columns.Add("ID", typeof(int)); dt.Columns.Add("用户名",typeof(string)); ds.Tables.Add(dt); dataGridView1.DataSource = dt;数据行博士 = dt.NewRow();博士[“ID”] = 1;博士["用户名"] = "maxWhite"; dt.Rows.Add(dr); dataGridView1.DataSource = dt;

标签: c# data-binding datagridview


【解决方案1】:
        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 中我看到子行

例如我在父网格中单击福特在子网格中显示所有福特车型,然后在马自达等上,我无法将行添加到子网格,因为我有关系,当我删除关系时一切都很好。

【讨论】:

  • datagridview1.Items .. datagrid中没有功能项
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-13
  • 2016-06-28
  • 1970-01-01
相关资源
最近更新 更多