1
votes

I have a gridview with property autogeneratecolumn=true and my DataTable is filling the data as mentioned below using mdx query.

sales2015   sales2014
1256           1235
3569            0
0              1235

i want to add another column named "VARIENT" and need to show the percentages in the same.

Formula: (sales2015 - sales2014)/sales2015 * 100

Need to calulate each row for the datatable using same formula and bind to gridview.

please help me with the logic.

Note : my grid is having autogenerated columns

expected result is like this

sales2015   sales2014  Varient
1256           1235      **%
3569            0         **%
0              1235      **%  

My Code part is:

System.Data.DataTable dt = new System.Data.DataTable();
ViewData1 vData = new ViewData1();
dt = vData .__getCustomerSource()// Here in this function i was filling the datatable and returing

DataGrid1.DataSource = null;

DataGrid1.DataSource = dt;
DataGrid1.DataBind();
1
How did you generate the grid View? Show the code for the query. You could add the formula for each item in a row in the grid View and get the desired result. - Vini
will you please give me any example to do that like - sowmya
I do not know how you are populating your grid view. - Vini
i have posted the code please look into that - sowmya
before bindng i want to do this task - sowmya

1 Answers

0
votes

You can create the third column on the fly and populate it like this:-

dt = vData .__getCustomerSource()
dt.Columns.Add("Varient", typeof(string));
foreach (DataRow row in dt.Rows)
 {
    row["Varient"] = String.Format("{0} %", ((row.Field<int>("sales2015") - 
                        row.Field<int>("sales2014")) / row.Field<int>("sales2015")) * 100);
 }
DataGrid1.DataSource = dt;
DataGrid1.DataBind();

You need to change your formula accordingly.

Update:

If your column names will be changing then you can make use of column index (but as you know it may fail if your datatable structure change dynamically) like this:-

(int)row.[1] - (int)row[2]