Column Expression

I have database with two tables what i would like to do is in the sales table when i put data in the datagrid is to have a column expression that calculates the quantity * price that gives the total.I am using C# express edition2005 and SQL server express edition 2005 hope someone can help with code and where to place it. Thankyou



Answer this question

Column Expression

  • Flores

    Hi Common Genius I looked at that info and other stuff on microsoft site and other posts I must be doing something wrong i tried the code in the site you said and i can not get it to work . I don't know if it makes any differance that my datagridveiw is bound to a dataset or not if you could give me any further advice it would be very thankfull Thankyou Barry


  • CurtTampa

    The DataTable you are creating doesnt have a Quantity or Price column; where is the calculated column supposed to get its values from You need to add to the calculated column to the table that contains the data you are trying to calculate.

  • Kingsonal

    What do you mean it didnt work What happened Can you post your code

  • calloatti

    Hi Common Genius here is the code

    namespace MyApp

    {

    public partial class Form2 : form

    {

    public Form2()

    {

    InitializeComponent();

    }

    private void Form2_Load(object sender, EventArgs e)

    {

    this.salesTableAdapter.Fill (this.database1DataSet.Sales);

    }

    private void CalcColumns()

    {

    DataTable table = new DataTable();

    DataColumn totalColumn = new DataColumn();

    totalColumn. DataType = System.Type.GetType("System Decimal");

    totalColumn.ColumnName = "Total";

    totalColumn.Expression = "Quantity * Price";

    table.Columns.Add(totalColumn);

    DataRow.row = table. NewRow();

    table.Rows.Add(row);

    DataView view = new DataView(table);

    dataGridView1 . DataSource = view;

    }

    }

    I don't know what i am doing wrong hope you can help after seeing the code i used thanks Barry


  • Igor Siticov

    good work
  • neilchan

    Thankyou for your quick reply I made the quantity and price columns in my sales data table i thought i only had to add the total column with an expression to get a result so to make this work i have to add these columns in the code i posted sorry for being a pain thankyou Barry
  • HappyCow

  • Column Expression