Solved

formula in a datagrid column

Posted on 2006-06-22
8
423 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

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…
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 short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

832 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