0

I have two tables, one is created by myself and the other is not.

I need to show some columns from both tables in one DatGridView.

Is it possible? If so how can I do it?

If you need anything about my code or database feel free to ask, I'll provide it to you, if I can.

Rand Random
  • 7,300
  • 10
  • 40
  • 88
  • Make a 3rd merged DataTable and use that. – Rand Random Aug 23 '17 at 13:51
  • Thanks for the suggestion, I'll keep that in mind – Josue Figueiredo Aug 23 '17 at 13:51
  • That wasnt a suggestion, thats the only way to go. `DataGridView` can only handle one `ItemsSource` and it has no logic for it to combine two `DataTables`, there is a `DataTable.Merge` method, but this only works in cases where the schema is similar. Which in your case that doesnt seem to be the case. How would you expect the ``DataGridView`` to know what the logic is to merge them? What column of 1 table fits to the other column of the other? What data should be present when a column is missing? Is it null, a default value? What if a column with same name have different datatypes and so on... – Rand Random Aug 23 '17 at 13:58
  • ``DataTable.Merge`` - https://msdn.microsoft.com/en-us/library/fk68ew7b(v=vs.110).aspx – Rand Random Aug 23 '17 at 13:59
  • Thanks for all the help – Josue Figueiredo Aug 23 '17 at 14:13
  • @Josue Figueiredo,What are tables? – VIGNESH ARUNACHALAM Aug 24 '17 at 11:22
  • You could rewrite your question to give a concrete example with some example tables. Providing an example input and expected output would help us discern your EXACT requirement. – Dan Rayson Aug 24 '17 at 11:25

1 Answers1

0

You need to join two tables and fetch the results as below:

I am using my table with MySQL:

using(MySqlConnection connection = new MySqlConnection(MyConnectionString))
using(MySqlCommand cmd = connection.CreateCommand())
{
    connection.Open();
    cmd.CommandText = "SELECT pb.Id, pb.Name, pb.MobileNo, e.email FROM phonebook pb INNER JOIN email e ON e.Id= pb.Id";
    MySqlDataAdapter adap = new MySqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    adap.Fill(ds);
    dataGridView1.DataSource = ds.Tables[0].DefaultView;
}