Display 2 different tables in 1 datagridview

Hello.

I have not worked with datagrid earlier and I have to just list two tables in the same datagridview but not at the same time.

i want to show "Order" table at load, but when I click on the "customer" button gridview should show customer table.

I need help with the code behind the button, how to switch from one order to customer showing in the same datagridview by pressing a button...

Please..help



Answer this question

Display 2 different tables in 1 datagridview

  • VSFW3

    that did not work either.

    Look at the listed please...


  • drinkwater

    try By using View object


  • viliescu

    I left work at 15:00 (european time), and will try to do the changes whe3n I get back to it in the morning.I will notify you then.

    Thanks for the help guys:)

    btw: My datagridview is autogenerated so is the bidingsource as well. I work inn C# express edition.


  • ajliaks

    change the Datamember property for example if you have dataset contain the both table

    when load write

    dataGridView1.DataSource = northwindDataSet;

    dataGridView1.DataMember = "Orders";

    when click change the DataMember

    dataGridView1.DataMember = "Customers";



  • DoubleDee

    it did not work.

    on load I load both datasets but the buttons does not return anything.

    source code:

    public AllBack()

    {

    InitializeComponent();

    }

    private void AllBack_Load(object sender, EventArgs e)

    {

    // on load gridview fills with kunde(customer data)

    this.kundeTableAdapter.Fill(this.allkopiPrivatDataSet.Kunde);

    this.ordreTableAdapter.Fill(this.allkopiPrivatDataSet.Ordre);

    }

    // this event fills gridview with kunde(cutomer) data

    private void btnKunder_Click(object sender, EventArgs e)

    {

    kundeBindingSource.DataSource = allkopiPrivatDataSet.Kunde;

    }

    // this event fills gridview with ordre(orders) data

    private void btnOrdre_Click(object sender, EventArgs e)

    {

    kundeBindingSource.DataSource = allkopiPrivatDataSet.Ordre;

    }

    It does not fill my gridview with the data from the tables when buttons are pressed


  • shivk

    hi Njofra

    did you configured it or still have a problem



  • AlexBB

    hei.

    I tried another angle here. Look below:

    public AllBack()

    {

    InitializeComponent();

    }

    private void AllBack_Load(object sender, EventArgs e)

    {

    dataGridView1.DataSource = allkopiPrivatDataSet;

    }

    private void btnKunder_Click(object sender, EventArgs e)

    {

    // TODO: This line of code loads data into the 'allkopiPrivatDataSet.Kunde' table.

    this.kundeTableAdapter.Fill(this.allkopiPrivatDataSet.Kunde);

    dataGridView1.DataMember = "Kunde";

    }

    private void btnOrdre_Click(object sender, EventArgs e)

    {

    // TODO: This line of code loads data into the 'allkopiPrivatDataSet.Ordre' table

    this.ordreTableAdapter1.Fill(this.allkopiPrivatDataSet.Ordre);

    dataGridView1.DataMember = "Ordre";

    }

    This kind of works, then btnKunder is clicked it returns all data in Kunder table...great, but btnOrdre_clicks only returns 1 column "KundeId" and not the rest of the table. can this have something to do with that only related column between two tables is displayed in "Order table"


  • ashmag

    hi,

    if your datagridview bind to a bindingsource , which was created by wizard it will not work, because the wizard creat columns and set GenerateMember to false, in the code that mohammed posted you don't have to have any column created in the datagridview b4, and you need to set GenerateMember property to true, then you can try the code again

    hope this helps



  • cybertaz69

    Hei.

    We are getting somewhere here. Now it changes between the tables but only columnnames come shows but not the data in them. I did as suggested from Mohammed and also sat Generate Datamember property to tru.

    Now it reads columns but not data in them.


  • ITJoeB

    Sorry i update previous post it will work try it

  • Duckboy

    Hello.

    I found out what was was not working..User error would I say.

    here is the code how it is supposed to be:

    public partial class AllBack : Form

    {

    public AllBack()

    {

    InitializeComponent();

    }

    //gets dataset

    private void AllBack_Load(object sender, EventArgs e)

    {

    dataGridView1.DataSource = allkopiPrivatDataSet;

    dataGridView1.DataMember = "Kunde";

    }

    //displays kunde(customer) table in dataGridView

    private void btnKunder_Click(object sender, EventArgs e)

    {

    dataGridView1.DataMember = "Kunde";

    kundeTableAdapter1.Fill(allkopiPrivatDataSet.Kunde);

    }

    //displays ordre(Order) table in dataGridView

    private void btnOrdre_Click(object sender, EventArgs e)

    {

    dataGridView1.DataMember = "Ordre";

    ordreTableAdapter1.Fill(allkopiPrivatDataSet.Ordre);

    }


  • Nfrf

    hi,

    yes and because your datagridview is auto generated , it make this trouble, at least the remove the auto generated columns from the collection and set GenerateMember to true, leave it bound to the bindingsource

    modify mohamed code and to change the dataMember of the bindingsource instead of datagridview

    just because autogenerated bindingsource has other dependencies like the navigation bar and the datagrid

    hope this helps



  • DINESH CHAUDHARI

    did you change your code withe updated one

    i test it on my machine and it is working

    what you get when you press click

    i will pos it agin when loading the form

    dataGridView1.DataSource = northwindDataSet;

    dataGridView1.DataMember = "Orders";

    when press the button change the Datamember to

    dataGridView1.DataMember = "Customers";



  • Display 2 different tables in 1 datagridview