[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Datagridview problem visual basic

Posted on 2009-05-03
43
Medium Priority
?
371 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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
 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

650 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