DataGridView - basic question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Dann

    #1

    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 programmaticall y-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
  • sflynn

    #2
    Re: DataGridView - basic question

    Have you looked into using the objectdatasourc e?

    John Dann wrote:[color=blue]
    > 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 programmaticall y-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[/color]

    Comment

    • Brian Tkatch

      #3
      Re: DataGridView - basic question

      John Dann wrote:[color=blue]
      > 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 programmaticall y-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[/color]

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

      DataGridView.Da taSource = 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.Fi ll(DataSet.Tabl es([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.

      Comment

      • John Dann

        #4
        Re: DataGridView - basic question

        On 28 Jun 2006 08:54:53 -0700, "sflynn" <ssflynn@gmail. com> wrote:
        [color=blue]
        >Have you looked into using the objectdatasourc e?
        >[/color]

        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.d atasource = MyDataset

        3. And then: DataGridView1.u pdate

        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
        programmaticall y 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

        Comment

        • John Dann

          #5
          Re: DataGridView - basic question

          On 28 Jun 2006 09:46:55 -0700, "Brian Tkatch"
          <Maxwell_Smart@ ThePentagon.com > wrote:
          [color=blue]
          >Set the DataGridView's DataSource to the DataTable's DefaultView.
          >
          >DataGridView.D ataSource = DataSet.Tables([TableName]).DefaultView.[/color]

          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

          Comment

          • Brian Tkatch

            #6
            Re: DataGridView - basic question

            John Dann wrote:[color=blue]
            > On 28 Jun 2006 09:46:55 -0700, "Brian Tkatch"
            > <Maxwell_Smart@ ThePentagon.com > wrote:
            >[color=green]
            > >Set the DataGridView's DataSource to the DataTable's DefaultView.
            > >
            > >DataGridView.D ataSource = DataSet.Tables([TableName]).DefaultView.[/color]
            >
            > 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[/color]

            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.

            Comment

            Working...