gpt4 book ai didi

asp.net - GridView的RowDataBound函数

转载 作者:行者123 更新时间:2023-12-04 01:21:59 24 4
gpt4 key购买 nike

我有一个 DataTable包含 3 个字段:ACount , BCountDCount .如果ACount < 0然后我需要在 GridView 的列之一中显示“S” .如果ACount > 0然后我必须在该列(标签中)中显示“D”。与 BCount 相同和 DCount .如何在 RowDataBound 中执行此条件检查功能?

最佳答案

GridView OnRowDataBound事件是你的 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”字符,但您应该能够重新调整以满足您的需要。

关于asp.net - GridView的RowDataBound函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/645415/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com