Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Expression column in datagridview

Status
Not open for further replies.

rony01

MIS
Sep 12, 2012
8
US
Hi friends
I am new to using datagridview ,I am trying to learn how to add an expression column in datagridview .can someone tell me why the total column gives me wrong out put ?
Code:
 Dim dt As New DataTable
        dt.Columns.Add("col1")
        dt.Columns.Add("col2")
        dt.Columns.Add("col3")
        dt.Columns.Add("col4")
        dt.Columns.Add("total")

        dt.Rows.Add("")
        dt.Rows.Add("")
        dt.Rows.Add("")
        dt.Rows.Add("")

        Me.DataGridView1.DataSource = dt
        dt.Columns("total").Expression = "[col2]+[col3]"
 
Since it has been a while since you posted this question I don't know if you already figured it out. I ran your code as is and it concatenated the column values; I am guessing that is the same issue you had.

Just change the data type of the columns right after you add them to the data table then it will work.

dt.Columns.Add("col1")
dt.Columns.Add("col2")
dt.Columns.Add("col3")
dt.Columns.Add("col4")
dt.Columns.Add("total")

dt.Columns.Add("col2", System.Type.GetType("System.Double"))
dt.Columns.Add("col3", System.Type.GetType("System.Double"))
dt.Columns.Add("total", System.Type.GetType("System.Double"))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top