tags:

views:

31

answers:

2

When I add the first item in the datagridview its ok but when i add the second one it replace the last item being added. here's my code Private Sub add()

    Dim i As Integer
    For i = 0 To DataGridView1.Rows.Count - 1
        'DataGridView1.Rows.Add()
        DataGridView1.Rows(DataGridView1.RowCount - 1).Cells("TransID").Value = txttrans.Text
        DataGridView1.Rows(DataGridView1.RowCount - 1).Cells("ProductCode").Value = txtprodcode.Text
        DataGridView1.Rows(DataGridView1.RowCount - 1).Cells("ProductName").Value = cmbprodname.Text
        DataGridView1.Rows(DataGridView1.RowCount - 1).Cells("Quantity").Value = txtqty.Text
        DataGridView1.Rows(DataGridView1.RowCount - 1).Cells("Price").Value = txtprc.Text
        DataGridView1.Rows(DataGridView1.RowCount - 1).Cells("Amount").Value = txtat.Text
        DataGridView1.Rows(DataGridView1.RowCount - 1).Cells("CustomerName").Value = txtcust.Text
        DataGridView1.Rows(DataGridView1.RowCount - 1).Cells("Date1").Value = txtdate.Text

    Next i


End Sub

And this is in my ADDbutton:

Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click

    Try
        add()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
    Dim total As Integer

    For Each row As DataGridViewRow In DataGridView1.Rows

        total += row.Cells("Amount").Value

    Next

    txtamt.Text = total
A: 

Should this (and the other lines)

DataGridView1.Rows(DataGridView1.RowCount - 1).Cells("TransID").Value = txttrans.Text

not be

DataGridView1.Rows(i).Cells("TransID").Value = txttrans.Text

because otherwise you are just addint the row DataGridView1.RowCount - 1.

Obalix
It worked but when i add the third item it replaces all the last 2 items
stephanie
@stephanie: could you show your updated code ?
Obalix
i Dont have yet an update code for that form. I'm working on a Point of Sale. and after populating the textbox i wll add it to the datagridview
stephanie
A: 

Try the following. Create a datatable with with the data structue that you need. I provided a small example of what to do. The data column array contains the column information. dt.columns.addRange sets your columns to the desired column name structure. The dt.newRow() method returns an empty row but contains the necessary schema so that you won't get the error that you are getting. Once you have added all the rows necessary you can reference the rows in the datatable using row["columnName"]

Use datagridview.datasource = dt to bind the datagrid to the datatable object.

  public Form1()
    {
        DataColumn column0 = new DataColumn("TransID");
        DataColumn column1 = new DataColumn("Price");
        DataColumn column2 = new DataColumn("Quantity");

        columns[0] = column0;
        columns[1] = column1;
        columns[2] = column2;

        DataTable dt = new DataTable();
        dt.Columns.AddRange(columns);

        //Creates a datarow object based on the dataable's schema
        DataRow row = dt.NewRow();

        row["TransID"] = "Transaction Text";
        row["Price"] = "Price Text";
        row["Quantity"] = "Quantity";

        //Add the row to the dataTable
        dt.Rows.Add(row);

        dataGridView1.DataSource = dt;
    }
gsirianni
I am trying to add items...There's an error appear said"Column named TransID cannot be found.Parameter name:columnName.but when i checked it its the same name.
stephanie
Sounds like there is some schema issue. Can you post the complete datagridview declaration? By schema I mean the layout of the datagridview's data structure. I ran into this issue last week myself.
gsirianni
Also, did you declare your columns through the designer or did you declare them programatically?
gsirianni
Me.TransID = New System.Windows.Forms.DataGridViewTextBoxColumn Me.ProductCode = New System.Windows.Forms.DataGridViewTextBoxColumn Me.ProductName = New System.Windows.Forms.DataGridViewTextBoxColumn Me.Quantity = New System.Windows.Forms.DataGridViewTextBoxColumnMe.Price = New System.Windows.Forms.DataGridViewTextBoxColumn Me.Amount = New System.Windows.Forms.DataGridViewTextBoxColumn Me.CustomerName = New System.Windows.Forms.DataGridViewTextBoxColumn Me.Date1 = New System.Windows.Forms.DataGridViewTextBoxColumn
stephanie
also this one...'DataGridView1 ' Me.DataGridView1.BackgroundColor = System.Drawing.SystemColors.Control Me.DataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize Me.DataGridView1.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {Me.TransID, Me.ProductCode, Me.ProductName, Me.Quantity, Me.Price, Me.Amount, Me.CustomerName, Me.Date1}) Me.DataGridView1.Location = New System.Drawing.Point(9, 58) Me.DataGridView1.Name = "DataGridView1" Me.DataGridView1.ReadOnly = True
stephanie
@gsirianni i delared it in the designer. in the arrow of the datagridview
stephanie