Solved

formula in a datagrid column

Posted on 2006-06-22
8
429 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

828 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