Add many DataGridView controls at run time?

R

rossmclean

Hi,
I am trying to add "x" number of DGV controls at run time. In my app
the user selects a dir with a number of mdbs in it. They then select
which dbs they want to run a common SQL against.

From here I want to load the results from each mdb into it's own DGV.
So far I have not been able to do this. I could dim a fix amount and
set a hard limit, but I would prefer not to.

So far I have tried declaring one DGV and adding it to a new tab for
each mdb, the result of which was to have many copies of the results
from the last mdb on each tab.
I have also tried using a collection, adding a new DGV to the
collection then loading the dataset, then bunging that on to a new
tab, the outcome of that is that I only end up with one DGV, and that
on the last tab.

Any ideas? Is the collect the right way to go, and I'm just doing it
wrong?
Cheers
Ross
 
P

Patrice

Hard to say without seeing the simplest code that shows the problem (we
don't need to see the dataset loading code if for now you don't see any
gridview).

Are you adding the controls to the form or just in your private collection ?
Basically it should be something such as :

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim newTextbox
For i As Integer = 0 To 5
newTextbox = New TextBox
newTextbox.top = i * newTextbox.Height * 1.2
Controls.Add(newTextbox)
Next
End Sub

Note that each new control is added to the form controls collection so that
the form knows about those controls. In your case you'll likely have to add
them to the container control such as a tab. My guess is that you add them
just for now to a private collection and so the form has no way to know
about those dynamically created controls...
 
F

Family Tree Mike

Hi,
I am trying to add "x" number of DGV controls at run time. In my app
the user selects a dir with a number of mdbs in it. They then select
which dbs they want to run a common SQL against.

From here I want to load the results from each mdb into it's own DGV.
So far I have not been able to do this. I could dim a fix amount and
set a hard limit, but I would prefer not to.

So far I have tried declaring one DGV and adding it to a new tab for
each mdb, the result of which was to have many copies of the results
from the last mdb on each tab.
I have also tried using a collection, adding a new DGV to the
collection then loading the dataset, then bunging that on to a new
tab, the outcome of that is that I only end up with one DGV, and that
on the last tab.

Any ideas? Is the collect the right way to go, and I'm just doing it
wrong?
Cheers
Ross

I was just doing something similar in c#. Similar code should work in VB.

I am creating a new tab page for each AssetName, and putting a datagridview
on that tab page. Hope this is helpful...

List <string> AssetNames = AssetMgr.AssetNames ();
foreach (string name in AssetNames)
{
TabPage tp = new TabPage(name);
tp.Name = name;

tcAssetQuery.TabPages.Add ( tp );
DataGridView dgv = new DataGridView ();

dgv.Columns.Add ( "AssetName", "Asset Name" );
dgv.Columns.Add ( "City", "City" );
dgv.Columns.Add ( "State", "State" );
dgv.Columns.Add ( "URL", "URL" );

dgv.Dock = DockStyle.Fill;
dgv.AutoSizeColumnsMode =
DataGridViewAutoSizeColumnsMode.Fill;
dgv.AllowUserToAddRows = false;

tp.Controls.Add ( dgv );
dgvDictionary.Add ( name, dgv );
}
 
R

rossmclean

Thanks Chaps,

I had originally correctly created the DGVs (i.e buy adding them to my
tab page controls collection), what i was doing wrong was that I had a
design time Data Binding Source, and I was binding all my DGV to this!
I just created a new BS for each DGV and it works fine!
Silly me I should have worked this out before, new to .Net so was
looking it the wrong place for the answer!

Thanks for your help
Ross
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top