Solved

Datagridview problem visual basic

Posted on 2009-05-03
43
357 Views
Last Modified: 2012-05-06
Hello Experts.I have a small problem!!! I make a form called purchase,inside of it i have  textboxes and 1 combobox that using the code bellow i pass the data i want inside a datagrid view.What is my problem? I have a databiding navigator when i push the +(add button) button  the value that have the Purchase ID  Is passed automaticly to datagrid view ,I want to pass it manually  when i push the add button to enter a product inside the form

If i misspelled something tell me
Private Sub Addpr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Addpr.Click

        Dim bsource As BindingSource = Me.PurchaseDataGridView.DataSource

        Dim ds As DataSet = DirectCast(bsource.DataSource, DataSet)

        Dim purchasetable As New DataTable

        Dim dr As DataRow = ds.Tables("purchase").NewRow

        dr("PurchaseID") = PurchaseIDTextBox.Text

        dr("ProductID") = ProductIDComboBox1.SelectedValue

        dr("UnitPrice") = UnitPriceTextBox1.Text

        dr("ProQty") = ProQtyTextBox.Text

        dr("Proname") = ProductNameTextBox1.Text

        dr("Total") = Convert.ToString(UnitPriceTextBox1.Text.ToString()) * Convert.ToString(ProQtyTextBox.Text.ToString())

 

        ds.Tables("Purchase").Rows.Add(dr)

 

        TotalSum()

Open in new window

0
Comment
Question by:demoniumz
  • 25
  • 12
43 Comments
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24292447
If you are using sql server then set the identity specification to No and if you are using MS Access set the fields datatype to integer instead of Autonumber.

If you have further questions, post back.
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24292941
nop help same think is pass auto the purchase id to the datagridview
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24293762
i upload it here if anyone can  help me to fix it is welcome pls.i want to pass data from  textboxes to  datagrid view when  push  add button  to add  in the datagrid view.i have another form called order that is like they same method that i do  before  but now is not working and i dont know why.My aplication is update here pls help

my aplication is here   http://rapidshare.com/files/229005880/s.zip.html

I left last hope here experts



NicolaouDB.mdb
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24297285
no one  want to help me? Experts?
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24300521
JackOfPH:  can u  help me  please needed to  finish the project the aplication is above and the database
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24300834
Sorry for the late reply,

ok, I will look in to your code...



0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24300889
Is this a project? or an assignment?
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24302165
a small project
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24302212
In your form there is txtPurchase textbox.

If I understand your question right, you want to input the purchaseID in the purchase textbox then throws that value in the colPurchaseID when you click add, right?
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24302239
yea  this i want
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24302277
Add this to your code...
 Private Sub PurchaseDataGridView_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles PurchaseDataGridView.RowsAdded

        PurchaseDataGridView.Rows(e.RowIndex).Cells(PurchaseID.Index).Value = Me.PurchaseIDTextBox.Text

    End Sub

Open in new window

0
 
LVL 1

Author Comment

by:demoniumz
ID: 24302307
PurchaseID is not decleare
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24302338
What is the name of the column you want to insert the value of the PurchaseIDTextbox?
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24302354
PurchaseID hehee  but why is not decleare
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24302377
Is this the first column in the DatagridView?
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24302380
yea
 is the first  column
0
 
LVL 15

Accepted Solution

by:
JackOfPH earned 500 total points
ID: 24302385
okey try this one:
Private Sub PurchaseDataGridView_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles PurchaseDataGridView.RowsAdded

        PurchaseDataGridView.Rows(e.RowIndex).Cells(0).Value = Me.PurchaseIDTextBox.Text

    End Sub

Open in new window

0
 
LVL 1

Author Comment

by:demoniumz
ID: 24302438
Index -1 is either negative or above rows count

Private Sub addrec_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles addrec.Click
        Me.NicolaouDBDataSet.Purchase.Clear()
        TextBox1.Text = String.Empty
        PurchaseBindingSource.AddNew ()((((((((((((((( the problem  goes in this line)))))))))))
    End Sub
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24302457
JackOfPH: you tried in my aplication and is working?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 15

Expert Comment

by:JackOfPH
ID: 24302462
Remove the code I gave you...
Private Sub PurchaseDataGridView_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles PurchaseDataGridView.RowsAdded

        PurchaseDataGridView.Rows(e.RowIndex).Cells(0).Value = Me.PurchaseIDTextBox.Text

    End Sub

Open in new window

0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24302467
Then try this one...

Private Sub addrec_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles addrec.Click
        Me.NicolaouDBDataSet.Purchase.Clear()
        TextBox1.Text = String.Empty
        PurchaseBindingSource.AddNew
 PurchaseDataGridView.Rows( PurchaseDataGridView.Rows.count -1).Cells(0).Value = Me.PurchaseIDTextBox.Text

    End Sub
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24302498
same thing
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24302512
Private Sub addrec_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles addrec.Click
        Me.NicolaouDBDataSet.Purchase.Clear()
        TextBox1.Text = String.Empty
        PurchaseBindingSource.AddNew
 PurchaseDataGridView.Rows( PurchaseDataGridView.Rows.count -2).Cells(0).Value = Me.PurchaseIDTextBox.Text
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24302555
nop working  Orders form is the same as this form   but here the code is not working
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24302593
the problem is somefixed if i  unload

 Private Sub TotalSum()
        Dim sum As Decimal = 0
        For Each row As DataGridViewRow In PurchaseDataGridView.Rows
            row.Cells(5).Value = row.Cells(3).Value * row.Cells(4).Value
            sum += row.Cells(5).Value
        Next
        Me.TextBox1.Text = sum
    End Sub
is working but with small mistakes if is loaded  is give me

 Operator '*' is not defined for type 'Short' and type 'DBNull' in this line
.row.Cells(5).Value = row.Cells(3).Value * row.Cells(4).Value
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24302629
JackOfPH: are u here;
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24302647
Try this code...
If not row.Cells(3).Value is system.dbnull.value andalso not row.Cells(4).Value is system.dbnull.value then
 

row.Cells(5).Value = row.Cells(3).Value * row.Cells(4).Value
 

else

row.Cells(5).Value = ""

end if

Open in new window

0
 
LVL 1

Author Comment

by:demoniumz
ID: 24302678
row is not decleared
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24302730
is not working as before i lose it not  for you is working?
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24302756
when add a product in the datagrid view in a line above is give me duplicate the Purchace ID and the Proqty

example


untitled.PNG
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24302783
i have big problem with this form i am not able to work
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24302896
JackOfPH: can  u tell me  why these happend?
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24303706
The only problem that remain now i fix it  the other part is when i  insert the quantity  in to datagridview the previous rows take the same lets say

purchase id  product id product name Unitprice quantity  total
1                     2               sain nicolas   $2.40         2    $4.80

when i insert another product with  quantity lets say 5 i get
purchase id  product id product name Unitprice quantity  total
1                     2               sain nicolas   $2.40         5    $4.80
1                      1               malboro light  $3.80         5   $19.00

why these happend bellow is my code
Public Class NicolaouPurchase
 

    Private Sub PurchaseBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PurchaseBindingNavigatorSaveItem.Click

        Me.Validate()

        Me.PurchaseBindingSource.EndEdit()

        Me.PurchaseTableAdapter.Update(Me.NicolaouDBDataSet.Purchase)
 

    End Sub

   

    Private Sub PurchasedetailsBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PurchaseBindingNavigatorSaveItem.Click

        Me.Validate()

        Me.PurchasedetailsBindingSource.EndEdit()

        Me.PurchasedetailsTableAdapter.Update(Me.NicolaouDBDataSet.Purchasedetails)
 

    End Sub

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

        'TODO: This line of code loads data into the 'NicolaouDBDataSet.Purchasedetails' table. You can move, or remove it, as needed.

        Me.PurchasedetailsTableAdapter.Fill(Me.NicolaouDBDataSet.Purchasedetails)

        'TODO: This line of code loads data into the 'NicolaouDBDataSet.Products' table. You can move, or remove it, as needed.

        Me.ProductsTableAdapter.Fill(Me.NicolaouDBDataSet.Products)

        'TODO: This line of code loads data into the 'NicolaouDBDataSet.Suppliers' table. You can move, or remove it, as needed.

        Me.SuppliersTableAdapter.Fill(Me.NicolaouDBDataSet.Suppliers)

        'TODO: This line of code loads data into the 'NicolaouDBDataSet.Purchase' table. You can move, or remove it, as needed.

        Me.PurchaseTableAdapter.Fill(Me.NicolaouDBDataSet.Purchase)
 

    End Sub
 

    Private Sub PurchaseBindingNavigator_RefreshItems(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PurchaseBindingNavigator.RefreshItems
 

    End Sub
 

    Private Sub Addpr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Addpr.Click

        Dim bsource As BindingSource = Me.PurchasedetailsDataGridView.DataSource

        Dim ds As DataSet = DirectCast(bsource.DataSource, DataSet)

        Dim orderdetailstable As New DataTable

        Dim dr As DataRow = ds.Tables("PurchaseDetails").NewRow

        dr("Purchaseid") = PurchaseIDTextBox.Text

        dr("Productid") = ProductIDComboBox1.SelectedValue

        dr("UnitPrice") = UnitPriceTextBox1.Text

        dr("Quantity") = QuantityTextBox1.Text

        dr("Productname") = ProductNameTextBox1.Text

        dr("Total") = Convert.ToString(UnitPriceTextBox1.Text.ToString()) * Convert.ToString(QuantityTextBox1.Text.ToString())
 

        ds.Tables("PurchaseDetails").Rows.Add(dr)
 

        TotalSum()

    End Sub

    Private Sub TotalSum()

        Dim sum As Decimal = 0

        For Each row As DataGridViewRow In PurchasedetailsDataGridView.Rows

            row.Cells(5).Value = row.Cells(3).Value * CDec(row.Cells(4).Value)

            sum += row.Cells(5).Value

        Next

        Me.TextBox1.Text = sum

        Me.TextBox1.Text = sum.ToString("$#,###.##")
 

        PurchasedetailsDataGridView.Columns(5).DefaultCellStyle.Format = ("$#,###.##")
 

    End Sub
 

    Private Sub PurchasedetailsDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles PurchasedetailsDataGridView.CellContentClick
 

    End Sub

End Class

Open in new window

0
 
LVL 1

Author Comment

by:demoniumz
ID: 24309453
anyone please?
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24312950
JackOfPH: can you help pls with dupliate value in datagrid view? as i described above
0
 
LVL 14

Expert Comment

by:jake072
ID: 24325502
demoniumz,

I'm rather confused by the thread, can you reiterate what your having problems with?  Also include the relevant code please.

Jake
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24334907
The only problem that remain now i fix it  the other part is when i  insert the quantity  in to datagridview the previous rows take the same lets say

purchase id  product id product name Unitprice quantity  total
1                     2               sain nicolas   $2.40         2    $4.80

when i insert another product with  quantity lets say 5 i get
purchase id  product id product name Unitprice quantity  total
1                     2               sain nicolas   $2.40         5    $4.80
1                      1               malboro light  $3.80         5   $19.00

why these happend bellow is my code
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24334914
soory i forget the code above is my problem
Public Class NicolaouPurchase

 

    Private Sub PurchaseBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PurchaseBindingNavigatorSaveItem.Click

        Me.Validate()

        Me.PurchaseBindingSource.EndEdit()

        Me.PurchaseTableAdapter.Update(Me.NicolaouDBDataSet.Purchase)

 

    End Sub

   

    Private Sub PurchasedetailsBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PurchaseBindingNavigatorSaveItem.Click

        Me.Validate()

        Me.PurchasedetailsBindingSource.EndEdit()

        Me.PurchasedetailsTableAdapter.Update(Me.NicolaouDBDataSet.Purchasedetails)

 

    End Sub

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

        'TODO: This line of code loads data into the 'NicolaouDBDataSet.Purchasedetails' table. You can move, or remove it, as needed.

        Me.PurchasedetailsTableAdapter.Fill(Me.NicolaouDBDataSet.Purchasedetails)

        'TODO: This line of code loads data into the 'NicolaouDBDataSet.Products' table. You can move, or remove it, as needed.

        Me.ProductsTableAdapter.Fill(Me.NicolaouDBDataSet.Products)

        'TODO: This line of code loads data into the 'NicolaouDBDataSet.Suppliers' table. You can move, or remove it, as needed.

        Me.SuppliersTableAdapter.Fill(Me.NicolaouDBDataSet.Suppliers)

        'TODO: This line of code loads data into the 'NicolaouDBDataSet.Purchase' table. You can move, or remove it, as needed.

        Me.PurchaseTableAdapter.Fill(Me.NicolaouDBDataSet.Purchase)

 

    End Sub

 

    Private Sub PurchaseBindingNavigator_RefreshItems(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PurchaseBindingNavigator.RefreshItems

 

    End Sub

 

    Private Sub Addpr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Addpr.Click

        Dim bsource As BindingSource = Me.PurchasedetailsDataGridView.DataSource

        Dim ds As DataSet = DirectCast(bsource.DataSource, DataSet)

        Dim orderdetailstable As New DataTable

        Dim dr As DataRow = ds.Tables("PurchaseDetails").NewRow

        dr("Purchaseid") = PurchaseIDTextBox.Text

        dr("Productid") = ProductIDComboBox1.SelectedValue

        dr("UnitPrice") = UnitPriceTextBox1.Text

        dr("Quantity") = QuantityTextBox1.Text

        dr("Productname") = ProductNameTextBox1.Text

        dr("Total") = Convert.ToString(UnitPriceTextBox1.Text.ToString()) * Convert.ToString(QuantityTextBox1.Text.ToString())

 

        ds.Tables("PurchaseDetails").Rows.Add(dr)

 

        TotalSum()

    End Sub

    Private Sub TotalSum()

        Dim sum As Decimal = 0

        For Each row As DataGridViewRow In PurchasedetailsDataGridView.Rows

            row.Cells(5).Value = row.Cells(3).Value * CDec(row.Cells(4).Value)

            sum += row.Cells(5).Value

        Next

        Me.TextBox1.Text = sum

        Me.TextBox1.Text = sum.ToString("$#,###.##")

 

        PurchasedetailsDataGridView.Columns(5).DefaultCellStyle.Format = ("$#,###.##")

 

    End Sub

 

    Private Sub PurchasedetailsDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles PurchasedetailsDataGridView.CellContentClick

 

    End Sub

End Class

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now