Solved

formula in a datagrid column

Posted on 2006-06-22
8
410 Views
Last Modified: 2012-06-22
Hello,

Is it possible to have one column of a datagrid depend on other columns of the datagrid?
For example,
Suppose column A contains 'x' values,
column B contains 'y' values,

then I want column C to contain 'x / y' (x divided by y) values.

Please help. Thanks.


0
Comment
Question by:engg
  • 3
  • 3
  • 2
8 Comments
 
LVL 26

Expert Comment

by:Rejojohny
ID: 16965187
you could do that in the query itself i.e. when u fetch it from database (i assume the data is coming from a database)... no  need to do that in the datagrid ..

select A, B, A*B as C from table

Rejo
0
 
LVL 9

Accepted Solution

by:
bele04 earned 400 total points
ID: 16965432
I'd suggest you go with what Rejo suggested to lessen the work load on your application.  But if you want to do some extra functionality then you can do something like this:

[ASPX Code]

<asp:TemplateColumn HeaderText="CategoryID">
    <ItemTemplate>
 <asp:Label id=Label1 runat="server" Text='<%# DisplayMethod(Container) %>'>
 </asp:Label>
    </ItemTemplate>
</asp:TemplateColumn>

[CodeBehind]

public string DisplayMethod(object c)
{
    if(DataBinder.Eval(c, "DataItem.A").ToString() != "")
    {
        int a = Integer.Parse(DataBinder.Eval(c, "DataItem.A").ToString());
        int b = Integer.Parse(DataBinder.Eval(c, "DataItem.B").ToString());
        string result = "" + (a*b);
        return result;
    }

    return 0;
}
0
 
LVL 9

Expert Comment

by:bele04
ID: 16965435
oopps...that should have been:

[CodeBehind]

public string DisplayMethod(object c)
{
    if(DataBinder.Eval(c, "DataItem.A").ToString() != "")
    {
        int a = Integer.Parse(DataBinder.Eval(c, "DataItem.A").ToString());
        int b = Integer.Parse(DataBinder.Eval(c, "DataItem.B").ToString());
        string result = "" + (a*b);
        return result;
    }

    return "0";
}
0
 

Author Comment

by:engg
ID: 16969452
Thanks Rejojohny, bele04.

I can't do that directly in the SQL query. Every column in the datagrid is going to be filled in by a different query or a by a different calculation.  each of those queries/calculations is going to be moderately complex.  

I will get back to you soon after I try out bele04's solution.
Thank you.


0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:engg
ID: 17043796
Hello Rejojohny, bele04,

Sorry for being late.

I used DataTable to store the data and then filled the datagrid using the datatable. .NET DataTable's  'Compute' method and 'Expression' property are useful if we want any column to be dependent on other columns.  
what do you think?

Thanks.


0
 
LVL 26

Assisted Solution

by:Rejojohny
Rejojohny earned 100 total points
ID: 17047215
am not aware of the "expression" property, but "compute" is used to compute a value of a single column (avg, sum etc) within multiple rows (not columns) of a datatable for a condition and not within columns as you require .. correct me if I am wrong ..

Rejo
0
 
LVL 9

Expert Comment

by:bele04
ID: 17055765
I too am not aware of the Expression property of the DataTable.  I don't think DataTable even has that property.  Did the code I posted above work for you? If not then you can use another approach.

Since you are using a datatable to bind the datagrid then why not manipulate the data in the datatable before you bind it to the datagrid like:

//dt is your datatable
foreach (DataRow dr in dt.Rows)
{
    dr["ColumnC"] = ((int)dr["ColumnA"] * (int)dr["ColumnB"]).ToString();
}
0
 

Author Comment

by:engg
ID: 17061164
Yeah, Rejo is right.

DataTable's DataColumn has 'Expression' property which can be used to create computed columns or Bele04's approach would work as well.

I will definitely try out bele04's code. Right now, the Datatable approach has worked for me.
Thank you very much for your help.


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
asp.net bundle 8 47
Search by two fields at the same time using c# radio button 5 39
ASP.NET Web API or ASP.NET Core MVC? 3 52
Entity Framework 7 32
AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now