Datagridview problem visual basic

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

LVL 1
demoniumzAsked:
Who is Participating?
 
JackOfPHConnect With a Mentor Commented:
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
 
JackOfPHCommented:
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
 
demoniumzAuthor Commented:
nop help same think is pass auto the purchase id to the datagridview
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
demoniumzAuthor Commented:
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
 
demoniumzAuthor Commented:
no one  want to help me? Experts?
0
 
demoniumzAuthor Commented:
JackOfPH:  can u  help me  please needed to  finish the project the aplication is above and the database
0
 
JackOfPHCommented:
Sorry for the late reply,

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



0
 
JackOfPHCommented:
Is this a project? or an assignment?
0
 
demoniumzAuthor Commented:
a small project
0
 
JackOfPHCommented:
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
 
demoniumzAuthor Commented:
yea  this i want
0
 
JackOfPHCommented:
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
 
demoniumzAuthor Commented:
PurchaseID is not decleare
0
 
JackOfPHCommented:
What is the name of the column you want to insert the value of the PurchaseIDTextbox?
0
 
demoniumzAuthor Commented:
PurchaseID hehee  but why is not decleare
0
 
JackOfPHCommented:
Is this the first column in the DatagridView?
0
 
demoniumzAuthor Commented:
yea
 is the first  column
0
 
demoniumzAuthor Commented:
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
 
demoniumzAuthor Commented:
JackOfPH: you tried in my aplication and is working?
0
 
JackOfPHCommented:
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
 
JackOfPHCommented:
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
 
demoniumzAuthor Commented:
same thing
0
 
JackOfPHCommented:
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
 
demoniumzAuthor Commented:
nop working  Orders form is the same as this form   but here the code is not working
0
 
demoniumzAuthor Commented:
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
 
demoniumzAuthor Commented:
JackOfPH: are u here;
0
 
JackOfPHCommented:
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
 
demoniumzAuthor Commented:
row is not decleared
0
 
demoniumzAuthor Commented:
is not working as before i lose it not  for you is working?
0
 
demoniumzAuthor Commented:
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
 
demoniumzAuthor Commented:
i have big problem with this form i am not able to work
0
 
demoniumzAuthor Commented:
JackOfPH: can  u tell me  why these happend?
0
 
demoniumzAuthor Commented:
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
 
demoniumzAuthor Commented:
anyone please?
0
 
demoniumzAuthor Commented:
JackOfPH: can you help pls with dupliate value in datagrid view? as i described above
0
 
jake072Commented:
demoniumz,

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

Jake
0
 
demoniumzAuthor Commented:
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
 
demoniumzAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.