1

My application takes any database as its input, it then detects the tables in that database and then fill all the tables into a DataSet. Now I have image of database in the dataset. Now my question is how to display this dataset into windows form.

DataSet is dynamic because everytime it will have n number of tables and each of these tables can have any number of rows and any number of columns.

Please read this question for context. compare databases (tables only) using C# and Ado.net

2
  • Are you trying to build a Database application?
    – Steve
    Jun 28, 2016 at 7:24
  • @Steve Yes, I am trying to build a Database application. Jun 28, 2016 at 8:33

1 Answer 1

2

The best way of displaying a table is with a DataGridView (DGV from now on). The thing is that the dgv is able to show one table, and only one.

So how do you do it? You need to have more than one DGV (one for each table of the dataset) or have some way of changing the table the DGV is showing.

Lets say you decide to have multiple DGV. I will not cover how to add them dinamically to the form, if you want to know ask for it and I will edit the answer.

You create one DGV for each table on the dataset:

List<DataGridView> DGVs = new List<DataGridView>();
foreach (DataTable DT in DS.Tables){
    DataGridView DGV = new DataGridView();
    DGV.DataSource=DT;
    DGVs.Add(DGV);
    //Add code for adding them to the form
}

Now you have one DataGridView for each table on the dataset.

If you prefer to have one DGV and be able to choose what table you want to show let me know


How to add controls dinamically

You need to decide how do you want to add them, in this case it will be one bellow the other.

For making them visible you need to:

  1. Add them to the father control
  2. Locate them
  3. Make them visible.

Adding to the father controls is a simple:

form1.Contols.Add(DGV)

Locating them is a bit more tricky, because you want each one of them in a diferent position, but relative to the previous one. To do this you need to create a Point and after that moving it X pixels down. so:

Point p = new Point(0,0); //Position of the first DGV, choose the one you want
p.Y += DGV.Height + 10; //Margin that you want

And then seting the DGV position to the point

DGV.Location = p;

And then just need to show the DGV

DGV.Show();

You can do it all in the for each loop, it will be something like:

List<DataGridView> DGVs = new List<DataGridView>();
Point p = new Point(0,0); //Position of the first DGV, choose the one you want
foreach (DataTable DT in DS.Tables){
    DataGridView DGV = new DataGridView();
    DGV.DataSource=DT;
    DGVs.Add(DGV);
    p.Y += DGV.Height + 10; //Margin that you want
    DGV.Location = p;
    this.Controls.Add(DGV);
    DGV.Show();
}

Note that if you dont need the list of DGV (It can be usefull in some cases) you can ignore the first and DGVs.Add(DGV); lines.

5
  • Thanks for answering. I have updated the question. Could you tell me how to add them dynamically? Jun 28, 2016 at 8:34
  • Ok, I would edit the answer, comne back in some minutes
    – Aimnox
    Jun 28, 2016 at 8:37
  • @TemporaryNames Edited
    – Aimnox
    Jun 28, 2016 at 8:49
  • Thank you so much. I am developing a database comparator tool. May I have your assistance on that project? May I have your email id? Jun 29, 2016 at 4:49
  • @TemporaryNames I'm no expert, not even close. I'm glad to help you with what I can, but there will be a lot (like a lot) of things that I will not know. If you know how. Email me at [email protected] if you want
    – Aimnox
    Jun 29, 2016 at 7:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.