DataGridView - basic question

J

John Dann

I need to display some internal program data in a grid to help with
debugging (just display a simple 2D table, not edit or anything else),
but I'm not an experienced database coder. AFAICS the only bindable
grid in VB2005 is the DataGridView control. But I'm struggling to get
this to do the simple task that's required - it's obviously a very
flexible and capable control but all the features maybe get in the way
of simple use for me, at least without spending a lot of learning
time.

My data is in a programmatically-created dataset with a single
2-column table. I was hoping that the task might as simple as plonking
a DGV grid on my form and pointing its datasource property at my
dataset. Then I imagined that doing a DGV.refresh or .update would be
enough to populate the grid automatically, even if all the formatting
etc wasn't perfect. Ignorance is bliss sometimes!

Obviously this isn't enough. Can anyone point me in the right
direction of the minimum number of extra steps just to be able to see
my progam data in the grid. (All the tutorials I can readily see seem
to assume that the user wants to retrieve data from a pre-existing
database and set up all the connections etc via a wizard.)

TIA
JGD
 
B

Brian Tkatch

John said:
I need to display some internal program data in a grid to help with
debugging (just display a simple 2D table, not edit or anything else),
but I'm not an experienced database coder. AFAICS the only bindable
grid in VB2005 is the DataGridView control. But I'm struggling to get
this to do the simple task that's required - it's obviously a very
flexible and capable control but all the features maybe get in the way
of simple use for me, at least without spending a lot of learning
time.

My data is in a programmatically-created dataset with a single
2-column table. I was hoping that the task might as simple as plonking
a DGV grid on my form and pointing its datasource property at my
dataset. Then I imagined that doing a DGV.refresh or .update would be
enough to populate the grid automatically, even if all the formatting
etc wasn't perfect. Ignorance is bliss sometimes!

Obviously this isn't enough. Can anyone point me in the right
direction of the minimum number of extra steps just to be able to see
my progam data in the grid. (All the tutorials I can readily see seem
to assume that the user wants to retrieve data from a pre-existing
database and set up all the connections etc via a wizard.)

TIA
JGD

Set the DataGridView's DataSource to the DataTable's DefaultView.

DataGridView.DataSource = DataSet.Tables([TableName]).DefaultView.

That'll show the data.

If the way your data changes is the data in the database changing, you
can refresh the data in the DataTable with a .Clear() and .Fill():

DataSet.Tables([TableName]).Clear()
Data_Adapter.Fill(DataSet.Tables([TableName]))

A Fill() without a Clear() first, will leave the original rows there
and just add more rows.

Being the DataGrid is connected to the DataTable DefaultView, there
will be no need to tell the DataGrid anything changed. It will reflect
changes made to the DataTable automatically.

B.
 
J

John Dann

Have you looked into using the objectdatasource?

Sorry - I should have added a little more detail:

What I've currently done is:

1. Add a DGV control to my form. This brings up an interesting little
input panel (DGV Tasks), which I don't know in detail how to answer,
for example

DataSource - well this is design time and my data source doesn't exist
yet so it's not intuitive to me what the answer is here. My instinct -
probably wrong - is to leave it at none.

Add columns - do I want explicitly to add columns here? I was kind of
hoping that once the control had seen the data source (at run time) it
would automatically add the columns present in the single table of the
dataset to the DGV.

So - again probably wrongly - I just dismiss this tasks panel.

2. I set: DataGridView1.datasource = MyDataset

3. And then: DataGridView1.update

Well, it compiles OK but I can't see anything on the grid.

What I'm really searching for is a basic tutorial on using the DGV
control in this particular context. (ie populating the DGV
programmatically and as automatically as possible from a runtime
dataset and with no frills) I'm not trying to do anything elaborate or
clever here - just need to get the basic pieces making sense.

JGD
 
J

John Dann

Set the DataGridView's DataSource to the DataTable's DefaultView.

DataGridView.DataSource = DataSet.Tables([TableName]).DefaultView.

Many thanks - so what I wanted to do can indeed be accomplished with
just a single line of code. But I could have looked for a long time
before spotting this magic incantation that DefaultView seems to
represent. I'm not quite sure why the term DefaultView should have
these very powerful results, but no doubt there's a good reason!

Thanks again.
JGD
 
B

Brian Tkatch

John said:
Set the DataGridView's DataSource to the DataTable's DefaultView.

DataGridView.DataSource = DataSet.Tables([TableName]).DefaultView.

Many thanks - so what I wanted to do can indeed be accomplished with
just a single line of code. But I could have looked for a long time
before spotting this magic incantation that DefaultView seems to
represent. I'm not quite sure why the term DefaultView should have
these very powerful results, but no doubt there's a good reason!

Thanks again.
JGD

Heh, i was just as foncused as you when i started this project in .NET.

The reason DefaultView works, is actually quite simple. A DataTable is
nothing more than a collection of DataRows (the data) and DataViews
(the range). In a sense, the DataTable is abstract, and cannot be
touched directly, without it being presented in some form. A DataView
is a presentation. The DefaultView is equivalent to the SQL statement
"SELECT * FROM DataTable".

..NET allows a lot of flexibility and much hierarchical order.
Unfortunately, the normal ease-of-use Microsoft usually provides seems
to have taken a back seat to formaility.

B.
 

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