GridView控件的RowDataBound功能 [英] RowDataBound function of GridView
问题描述
我有一个包含3个字段数据表
: ACount
, BCount
和 DCOUNT
。如果 ACount< 0
然后我需要在 GridView控件
的其中一列显示S。如果 ACount> 0
然后我在该列(在标签)来显示'D'。与 BCount
同样的事情, DCOUNT
。我怎样才能做到在的RowDataBound
函数这个条件检查?
I have a DataTable
that contains 3 fields: ACount
, BCount
and DCount
. If ACount < 0
then I need to display 'S' in one of the columns of the GridView
. If ACount > 0
then I have to display 'D' in that column(in label). Same thing with BCount
and DCount
. How can I do this conditional check in the RowDataBound
function?
推荐答案
GridView控件<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.onrowdatabound.aspx\"相对=nofollow> OnRowDataBound
事件是你的朋友:
The GridView OnRowDataBound
event is your friend:
<asp:gridview
id="myGrid"
onrowdatabound="MyGrid_RowDataBound"
runat="server">
<columns>
<asp:boundfield headertext="ACount" datafield="ACount" />
<asp:boundfield headertext="BCount" datafield="BCount" />
<asp:boundfield headertext="DCount" datafield="DCount" />
<asp:templatefield headertext="Status">
<itemtemplate>
<asp:label id="aCount" runat="server" />
<asp:label id="bCount" runat="server" />
<asp:label id="dCount" runat="server" />
</itemtemplate>
</asp:templatefield>
</columns>
</asp:gridview>
// Put this in your code behind or <script runat="server"> block
protected void MyGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType != DataControlRowType.DataRow)
{
return;
}
Label a = (Label)e.Row.FindControl("aCount");
Label b = (Label)e.Row.FindControl("bCount");
Label d = (Label)e.Row.FindControl("dCount");
int ac = (int) ((DataRowView) e.Row.DataItem)["ACount"];
int bc = (int) ((DataRowView) e.Row.DataItem)["BCount"];
int dc = (int) ((DataRowView) e.Row.DataItem)["DCount"];
a.Text = ac < 0 ? "S" : "D";
b.Text = bc < 0 ? "S" : "D";
d.Text = dc < 0 ? "S" : "D";
}
我不知道你想要的'S'和'D字呈现,但你应该能够rejig满足您的需求。
I'm not sure where you want the 'S' and 'D characters rendered, but you should be able to rejig to meet your needs.
这篇关于GridView控件的RowDataBound功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!