本文介绍了如果行包含空单元格,如何在gridview中显示和隐藏按钮字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
i have a gridview in my asp.net application. for example
SN name Date Action
1 mike button(do something).
This is just one out of my gridview rows. i want for every row that has an empty cell, button field cell for the row should show. if there's no empty cell in that row, the button should hide.What i get with my code is that all the button visible becomes false which i donot want.
我尝试了什么:
What I have tried:
<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>
</Columns>
<PagerSettings FirstPageText="First" LastPageText="Last" Mode="NumericFirstLast" PageButtonCount="5" />
代码落后
code behind
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;
}
}
}
}
推荐答案
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
switch (e.Row.RowType)
{
case DataControlRowType.DataRow:
DataRowView myDataRowView = (DataRowView)e.Row.DataItem;
if (String.IsNullOrEmpty(myDataRowView["yourfieldnames"].ToString()))
{
Button status = (Button)e.Row.FindControl("out");
if (status != null)
{
status.visible = false;
}
}
break;
}
}
Button status = (Button)e.Row.FindControl("out");
if (string.IsNullOrWhiteSpace(e.Row.Cells[i].Text))
status.Visible = true;
else
status.Visible = false;
详情请见: []
这篇关于如果行包含空单元格,如何在gridview中显示和隐藏按钮字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!