471,122 Members | 1,098 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,122 software developers and data experts.

DataGridView - basic question

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
Jun 28 '06 #1
5 1808
Have you looked into using the objectdatasource?

John Dann wrote:
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


Jun 28 '06 #2
John Dann wrote:
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.

Jun 28 '06 #3
On 28 Jun 2006 08:54:53 -0700, "sflynn" <ss*****@gmail.com> wrote:
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
Jun 28 '06 #4
On 28 Jun 2006 09:46:55 -0700, "Brian Tkatch"
<Ma***********@ThePentagon.com> wrote:
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
Jun 29 '06 #5
John Dann wrote:
On 28 Jun 2006 09:46:55 -0700, "Brian Tkatch"
<Ma***********@ThePentagon.com> wrote:
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.

Jun 29 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Mitchell S. Honnert | last post: by
2 posts views Thread by michael sorens | last post: by
2 posts views Thread by galletg | last post: by
4 posts views Thread by gregarican | last post: by
8 posts views Thread by =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.