开发者

DataGridView not flitering detail record and ignoring foreign key relationship

开发者 https://www.devze.com 2023-03-01 04:29 出处:网络
I have requirment in which i am showing Master- Detail information into DataGridView. I have add the foriegn key relationship to the Dataset. But Detail Grid is not flitering it self based on the sel

I have requirment in which i am showing Master- Detail information into DataGridView.

I have add the foriegn key relationship to the Dataset. But Detail Grid is not flitering it self based on the selection of master. I dont know what I am missing here!

Just to verify if the relationship exist , I have checked them using DataGrid. I shows up there.

DataGridView not flitering detail record and ignoring foreign key relationship

 namespace WindowsFormsApplication1
 {
  public partial class Form1 : Form
  {
      public Form1()
      {
        InitializeComponent();
      }


    DataSet CreateTableAndFill()
    {
        DataSet ds = new DataSet();
        DataTable Header = ds.Tables.Add("Header");
      开发者_运维知识库  DataTable Line = ds.Tables.Add("Line");
        DataColumn dataColumn =  Header.Columns.Add("ID", typeof(int));
        dataColumn.AutoIncrement = true;
        dataColumn.AutoIncrementSeed = 1;
        Header.Columns.Add("Name", typeof(string));
        Line.Columns.Add("ID", typeof(int));
        Line.Columns.Add("Phone", typeof(string));
        DataRelation dataRelation = new DataRelation("ID_FK", Header.Columns["ID"], Line.Columns["ID"]);
        ds.Relations.Add(dataRelation);
        Header.Rows.Add(null, "Huzaiafa");
        Header.Rows.Add(null, "Arthur");
        Line.Rows.Add(1, "4253068516");
        Line.Rows.Add(1, "4252746864");
        Line.Rows.Add(2, "5654034");
        Line.Rows.Add(2, "12563");
        return ds;
    }

    private void button1_Click(object sender, EventArgs e)
    {
       DataSet ds = CreateTableAndFill();
       dataGridView1.DataSource = ds;
       dataGridView2.DataSource = ds;
       dataGridView1.DataMember = "Header";
       dataGridView2.DataMember = "Line";

    }
}
}

Even after applying the walk through from msdn it does not work ! Same result.

    private void button1_Click(object sender, EventArgs e)
    {
       DataSet ds = CreateTableAndFill();

       BindingSource masterBindingSource = new BindingSource();
       BindingSource detailsBindingSource = new BindingSource();

       masterBindingSource.DataSource = ds;
       detailsBindingSource.DataSource = masterBindingSource.DataSource;
       masterBindingSource.DataMember = "Header";
       detailsBindingSource.DataMember = "Line";  

       dataGridView1.DataSource = masterBindingSource;
       dataGridView2.DataSource = detailsBindingSource;


    }


Make the following changes:

Header.Rows.Add(1, "Huzaiafa");        
Header.Rows.Add(2, "Arthur");

detailsBindingSource.DataSource = masterBindingSource;
detailsBindingSource.DataMember = "ID_FK"; 


You'll get a lot of the MSDN article Walkthrough: Creating a Master/Detail Form Using Two Windows Forms DataGridView Controls.

It shows pretty much what you're trying to do.

You'll see that the example binds a DataSet to a BindingSource bound to the master DGV, and the detail DGV is bound to another BindingSource which itself is found to the master BindingSource. That's how you'll get the master-detail relationship between the DGVs.

Update:
This will work in place of your button1_Click:

private void button1_Click(object sender, EventArgs e) {
    BindingSource masterBindingSource = new BindingSource();
    BindingSource detailsBindingSource = new BindingSource();

    masterBindingSource.DataSource = CreateTableAndFill();
    masterBindingSource.DataMember = "Header";

    dataGridView1.DataSource = masterBindingSource;

    detailsBindingSource.DataSource = masterBindingSource;
    detailsBindingSource.DataMember = "ID_FK";

    dataGridView2.DataSource = detailsBindingSource;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消