Solved

formula in a datagrid column

Posted on 2006-06-22
8
438 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

696 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