Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to populate a DataGridView without binding to a Database

Status
Not open for further replies.

marskills

Programmer
Oct 14, 2007
7
CA
Hello All,

I'm new to .Net (VB 2005) and would like some advice on what methodology should be used to populate a DataGridView.

The scenario is an orders screen with some text fields such as txtOrderItem, txtOrderQTY and a DataGridView. When a button cmdAddItem is pressed the data in the textboxes is added to the DataGridView as a new row.

Here's what I've tried for this sample:
Code:
Imports System.Data
Public Class frmOrdersIn

    Private Sub frmOrdersIn_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        'Create a datatable for the orders DataGridView

        Dim dtOrderItems As DataTable
        dtOrderItems = New DataTable("OrdersList")
        Dim Product As DataColumn = New DataColumn("Item")
        Dim Product As DataColumn = New DataColumn("Qty")
Now when I try to add code to the cmdAddItem such as:

Code:
Private Sub cmdAddItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddItem.Click
        OrderItem = dtOrderItems.NewRow()
        OrderItem("Item") = txtOrderItem.Text
        OrderItem("QTY") = txtOrderQTY.Text
        
        dtOrderItems.Rows.Add(OrderItem)
The IDE shows me that dtOrderItems is not declared and therefore there is no NewRow() method available.


So my questions are:

1. How to declare objects or variables so that they are available within procedures in a form but not outside the form.
2. Whats the best (preferred) way to populate a DataGridView without binding to a database (I'll write to the database after all the items are entered in the datagridview).

Any help or guidance is very much appreciated.

Thanks,

Rory
 
1. Declare your DataTable as a form level variable (dim DT as DataTable right below Public Class frmOrdersIn, rather than in a sub).

2. DataGridView.DataSource = DT;

Hope this helps,

Alex

[small]----signature below----[/small]
Who are you, and why should I care?
Side A: We're a community of outdated robots who refused to upgrade and came here
*changes sides*
Side B: to live a simpler existence. Free of technology.
 
No problem. Thank you for taking the time to determine the root of the problem before posting :)



[small]----signature below----[/small]
Who are you, and why should I care?
Side A: We're a community of outdated robots who refused to upgrade and came here
*changes sides*
Side B: to live a simpler existence. Free of technology.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top