Solved

formula in a datagrid column

Posted on 2006-06-22
8
395 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:engg
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 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

11 Experts available now in Live!

Get 1:1 Help Now