C#中数据列表中的标签 [英] Label in datalist in C#
问题描述
朋友们,
在我的hr项目中,我使用了数据列表,
就像这样
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
Leave | balance | used |remained
sl | 6 | 3 |3
cl | 6 | 0 |6
pl | 15 | 0 |15
lwp | - | 0 |-
so how can i do this.
推荐答案
最简单的方法可能是添加执行减法的绑定表达式.假设您现有的 DataList ItemTemplate 代码如下所示:
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:
<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:
<table><tbody><tr><td><![CDATA[<%# (Container.DataItem as MyClass).balance - (Container.DataItem as MyClass).used %>]]></td></tr></tbody></table>
其中 MyClass 是数据项类的名称.
Where MyClass is the name of your data item class.
创建一个类,表示Data并用列名填充.
create a class say Data and fill it with column names.
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
List<Data> DataList = new List<Data>();
现在创建Data类的对象并将其添加到列表中.
Now Create object of class Data and add it to list.
DataList.Add(new Data{Leave=<value>,Balance="<value>",Used="<value>",Remain="<value>"});
此列表中的每个元素对应于一行.
U甚至可以直接将任何可接受的Gui元素的数据源设置为此列表.
[列的名称将为属性]
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]
这篇关于C#中数据列表中的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!