By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,050 Members | 1,174 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,050 IT Pros & Developers. It's quick & easy.

calculated column in datagridview

P: 2
Hi friends
I am new to , I am having difficulty to add a calculated column to calculate
amount = salesPrice*qty in the code below
please help me

Expand|Select|Wrap|Line Numbers
  2.  DataGridView1.ColumnCount = 5
  3.         DataGridView1.Columns(0).Name = "Product ID"
  4.         DataGridView1.Columns(1).Name = "Product Name"
  5.         DataGridView1.Columns(2).Name = "salesPrice"
  6.         DataGridView1.Columns(3).Name = "qty"
  7.         DataGridView1.Columns(4).Name = "amount"
  9.         Dim row As String() = New String() {"1", "Product 1"}
  10.         DataGridView1.Rows.Add(row)
  11.         row = New String() {"2", "Product 2"}
  12.         DataGridView1.Rows.Add(row)
  13.         row = New String() {"3", "Product 3"}
  14.         DataGridView1.Rows.Add(row)
  15.         row = New String() {"4", "Product 4"}
  16.         DataGridView1.Rows.Add(row)
Oct 29 '12 #1
Share this Question
Share on Google+
4 Replies

Expert Mod 5K+
P: 9,731
See if the DataGridView CellParsing Event can help you achieve what you are looking for.

Oct 29 '12 #2

P: 2
Thanks for your reply, I have figured out on adding expression column another way but now having difficulty to get the correct out put . any bit of info would be helpful
Expand|Select|Wrap|Line Numbers
  1.         Dim dt As New DataTable
  2.         dt.Columns.Add("col1")
  3.         dt.Columns.Add("col2")
  4.         dt.Columns.Add("col3")
  5.         dt.Columns.Add("col4")
  6.         dt.Columns.Add("total")
  7.         dt.Columns("total").Expression = "[col1]*[col2]"
Nov 1 '12 #3

P: 6
In the cell leave event of Quantity column multiply the column of item amount and Quantity and set the cell value of total Amount column. And it is nice if you give Save button to save the records.
Nov 2 '12 #4

Expert Mod 5K+
P: 9,731
That is really cool!
I have never used the DataColumn.Expression Property for anything except filtering. I didn't even realize you could do that.

Anyways, according to the documentation (in the link I just posted) it seems you do not have to have the [] around the column names.

Try removing them and see if it fixes your problem :)

I love it when I learn something new :)

Nov 2 '12 #5

Post your reply

Sign in to post your reply or Sign up for a free account.