CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2005
    Posts
    1

    add data in datagrid

    Hi,
    I have a problem, please help me to solve it.
    I want to make a column in a datagrid and that column get data from two column beside. For example i want make a column that get data from amount two column beside. That data will display when I insert new row. I use a datatable in my datagrid.

    When i add new row, there will be error.

  2. #2
    Join Date
    Jun 2005
    Location
    Maryland,USA
    Posts
    20

    Re: add data in datagrid

    It would be better for you to do all calculations in the SQL query itself before you attach the result to the datagrid.So if u want to do some calculations on one column and show it in another column,it will be easy for you if do it in the query itself.Hope this helps.

  3. #3
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: add data in datagrid

    Add a DataColumn to the table. Set a name to it, and then set the Expression proeprty of it to be "columnA * columnB" where those are the names of the two columns. You can see an example in MSDN:
    Code:
    private void CalcColumns(){
        DataColumn cPrice;
        DataColumn cTax;
        DataColumn cTotal;
        DataTable myTable = new DataTable ();
     
        // Create the first column.
        cPrice = new DataColumn();
        cPrice.DataType = System.Type.GetType("System.Decimal");
        cPrice.ColumnName = "price";
        cPrice.DefaultValue = 50;
             
        // Create the second, calculated, column.
        cTax = new DataColumn();
        cTax.DataType = System.Type.GetType("System.Decimal");
        cTax.ColumnName = "tax";
        cTax.Expression = "price * 0.0862";
             
        // Create third column.
        cTotal = new DataColumn();
        cTotal.DataType = System.Type.GetType("System.Decimal");
        cTotal.ColumnName = "total";
        cTotal.Expression = "price + tax";
        
        // Add columns to DataTable.
        myTable.Columns.Add(cPrice);
        myTable.Columns.Add(cTax);
        myTable.Columns.Add(cTotal);
        DataRow myRow;
        myRow = myTable.NewRow();
        myTable.Rows.Add(myRow);
        DataView myView = new DataView(myTable);
        dataGrid1.DataSource = myView;
     }
    remember to remove the column before you update to database.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured