0
votes

How can I add a new row in a DataGridView when using relations?

When the DataGridView is bound to a DataTable, I can add a row, but when Im using relations, nothing happens, the row won't add to theDataGridView`.

1
after updating the binding if you donot see row added then you need to refresh DatagridviewAfnan Bashir
datagridview.update(); and datagridview.refresh(); doest not workuser538497
can you show us the code perhaps we could see it and help you outAfnan Bashir
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;user538497

1 Answers

0
votes
        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;

before this i use relations, ds.Relations.Add(new DataRelation .......... and after this i can`t add row dataGridView3.DataSource = ds.Tables[0]; dataGridView3.DataMember = "relation_name";

because i must use data binding to filter out my result, eg. i click on parent datagrid 1 and in datagrid 2 i see child rows

e.g. i click in parent grid Ford in child grid shows all Ford models, then on Mazda and so on, and i can`t add row to child grid, because i have relation, when i delete relation all i fine.