Click here to Skip to main content
15,500,453 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi friends,

in my hr project i used datalist,
which is like this
Leave  | balance | used
  sl   |       6 |    3
  cl   |       6 |    0
  pl   |      15 |    0
  lwp  |       - |    0

now from this datalist i want to add new column (balance-used) from datalist.

result like this
XML
Leave  | balance | used   |remained
  sl   |       6 |    3   |3
  cl   |       6 |    0   |6
  pl   |      15 |    0   |15
  lwp  |       - |    0   |-


so how can i do this.
Posted
Updated 5-Sep-12 21:52pm
v4
Comments
Devang Vaja 1-Sep-12 7:31am    
are u filling datalist dynamically?
aarohi verma 1-Sep-12 7:42am    
no. fetching from two different tables.
AmitGajjar 1-Sep-12 8:11am    
what does balance-used mean ? balance and used both of the columns are present.
Devang Vaja 1-Sep-12 9:22am     CRLF
i think she want a column name Balance-used means there will be 4 collumns.. if balance-used column is not in db than fill it dynamically and add this column dynamically by calculation with javscript or any other thing...

The easiest way is probably to add a binding expression that does the subtraction. Let's say your existing DataList ItemTemplate code looks like this:

ASP
<itemtemplate>
  <tr>
    <td><![CDATA[<%# DataBinder.Eval(Container.DataItem, "Leave") %>]]></td>
    <td><![CDATA[<%# DataBinder.Eval(Container.DataItem, "balance") %>]]></td>
    <td><![CDATA[<%# DataBinder.Eval(Container.DataItem, "used") %>]]></td>
  </tr>
</itemtemplate>


Then, add the expression like this:

ASP
<table><tbody><tr><td><![CDATA[<%# (Container.DataItem as MyClass).balance - (Container.DataItem as MyClass).used %>]]></td></tr></tbody></table>


Where MyClass is the name of your data item class.
 
Share this answer
 
Comments
dbaseman 1-Sep-12 16:26pm    
What's wrong with this solution?
aarohi verma 6-Sep-12 2:04am    
It give compilation error because of - character.i just try solve this.
create a class say Data and fill it with column names.
C#
Class Data
{
public string Leave { get; set; }
public string Balance { get; set; }
public string Used { get; set; }
public string Remain  { get; set; }
}

create a list of type Data
C#
List<Data> DataList = new List<Data>();

Now Create object of class Data and add it to list.
C#
DataList.Add(new Data{Leave=<value>,Balance="<value>",Used="<value>",Remain="<value>"});


Each element in this list corresponds to a row.
U can even directly set the Datasource of any collection acceptable Gui element to this List.
[Columns would have names of Properties]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900