Link to home
Start Free TrialLog in
Avatar of demoniumz
demoniumz

asked on

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

Avatar of JackOfPH
JackOfPH
Flag of Philippines image

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.
Avatar of demoniumz
demoniumz

ASKER

nop help same think is pass auto the purchase id to the datagridview
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
no one  want to help me? Experts?
JackOfPH:  can u  help me  please needed to  finish the project the aplication is above and the database
Sorry for the late reply,

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



Is this a project? or an assignment?
a small project
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?
yea  this i want
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

PurchaseID is not decleare
What is the name of the column you want to insert the value of the PurchaseIDTextbox?
PurchaseID hehee  but why is not decleare
Is this the first column in the DatagridView?
yea
 is the first  column
ASKER CERTIFIED SOLUTION
Avatar of JackOfPH
JackOfPH
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
JackOfPH: you tried in my aplication and is working?
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

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
same thing
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
nop working  Orders form is the same as this form   but here the code is not working
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
JackOfPH: are u here;
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

row is not decleared
is not working as before i lose it not  for you is working?
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
i have big problem with this form i am not able to work
JackOfPH: can  u tell me  why these happend?
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

anyone please?
JackOfPH: can you help pls with dupliate value in datagrid view? as i described above
demoniumz,

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

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