Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,

VB newbe looking for some help. :-)

I have a datagrid with 8 columns (id, ean, name, description, price, totalprice, a spacer column and vat).

I like to show a calculated column in a datagrid showing price inc. vat (totalprice)
So i created a unbound column wich is on index 5 and when i try to populate that column it doesn't show the results.
VB
For Each r As DataGridViewRow In Me.dataGridProducts.Rows
    TotaalPrijs = r.Cells(4).Value + (r.Cells(4).Value * r.Cells(8).Value / 100)
    r.Cells(5).Value = totaalPrijs
Next

When i use the index of any other bound column it shows me the result in the wrong column
VB
For Each r As DataGridViewRow In Me.dataGridProducts.Rows
    TotaalPrijs = r.Cells(4).Value + (r.Cells(4).Value * r.Cells(8).Value / 100)
    r.Cells(6).Value = totaalPrijs
Next

Any help would be appriciated
Posted
Updated 8-May-13 17:05pm
v3
Comments
ZurdoDev 8-May-13 19:57pm    
"when i use the index of any other bound column it shows me the result in the wrong column" - Sounds like your index is off.
Sergey Alexandrovich Kryukov 8-May-13 21:12pm    
Very much so... :-)
—SA

1 solution

Its in C# hope this helps

private void buildDataTable() {

    DataTable dt = new DataTable("test");
    dt.Columns.Add("col1", typeof(int));
    dt.Columns.Add("col2", typeof(int));
    dt.Columns.Add("col3", typeof(int));

    dt.Rows.Add(1, 2);
    dt.Rows.Add(2, 3);
    dt.Rows.Add(3, 4);
    dt.Rows.Add(4, 5);

    foreach (DataRow dr in dt.Rows) {
        dr[2] = (int)dr[0] + (((int)dr[1] * 100) / 100);
    }
    dgv1.DataSource = dt;
}

private void addColunmAndValue() {

    dgv1.Columns.Add("Col4", "Col4");
    foreach (DataGridViewRow dr in dgv1.Rows) {
        DataGridViewCellCollection dc = dr.Cells;
        if (!dr.IsNewRow) {
            dc[3].Value = (int)dc[2].Value + ((int)dc[1].Value * 100);
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900