我的asp.net应用程序中有一个gridview。例如
SN名称日期行动
1个按钮(做某事)
这只是我的gridview行中的一行。我希望每行都有一个空单元格,该行的按钮字段单元格应显示。如果该行中没有空单元格,则该按钮应隐藏。我的代码得到的是所有可见的按钮都变为假,我不想要。
这是我背后的代码
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" Height="326px" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="5" style="text-align: left; margin-left: 169px" Width="1069px" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnRowEditing="GridView1_RowEditing">
<Columns>
<asp:BoundField HeaderText="S/N" DataField="SN" />
<asp:BoundField HeaderText="First Name" DataField="FirstName" />
<asp:BoundField HeaderText="Address" DataField="Address" />
<asp:BoundField HeaderText="Phone Number" DataField="PhoneNumber" />
<asp:BoundField HeaderText="Sex" DataField="Sex" />
<asp:BoundField HeaderText="Reason" DataField="Reason" />
<asp:BoundField HeaderText="SignIn" DataField="SignIn_Time" />
<asp:BoundField HeaderText="SignOut" DataField="Signout_Time" />
<asp:TemplateField HeaderText="Action" Visible="True">
<ItemTemplate>
<asp:Button ID="out" runat="server" Text="Sign out" CommandName="SignOut" CommandArgument='<%# Eval("SN") %>' CssClass="active"/>
</ItemTemplate>
</asp:TemplateField>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i =0 ; i < e.Row.Cells.Count; i ++)
{
if (e.Row.Cells[i].Text == " ")
{
Button status = (Button)e.Row.FindControl("out");
status.Visible = true;
}
else
{
Button status = (Button)e.Row.FindControl("out");
status.Visible = false;
}
}
}
}
答案 0 :(得分:1)
尝试使用此代码
string.IsNullOrEmpty(e.Row.Cells[i].Text)
或强>
e.Row.Cells[i].Text.Equals(" ")
答案 1 :(得分:0)
您正在做的是检查整个网格中的所有单元格。解决方法是检查每个RowDataBound事件中的所有单元格。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var status = (Button) e.Row.FindControl("out");
// Show button if at least one cell is empty.
status.Visible = e.Row.Cells.Cast<TableCell>()
.Any(cell => string.IsNullOrEmpty(cell.Text));
}
}
通常,数据库不应该为空数据返回
,除非您有意将它们存储在数据库中。