我创建一个gridView
,允许通过添加必要的插入到FooterTemplate
控件添加新行,但是当ObjectDataSource
没有记录,我添加了一个虚拟的行为FooterTemplate
是一个GridView行只有在有数据时才显示。隐藏在asp.net
如何隐藏此虚拟行?我试图在RowDataBound
上设置e.row.visible = false
,但该行仍然可见。
我创建一个gridView
,允许通过添加必要的插入到FooterTemplate
控件添加新行,但是当ObjectDataSource
没有记录,我添加了一个虚拟的行为FooterTemplate
是一个GridView行只有在有数据时才显示。隐藏在asp.net
如何隐藏此虚拟行?我试图在RowDataBound
上设置e.row.visible = false
,但该行仍然可见。
这是GridView控件的不正确用法。 GridView控件有一个特殊的InsertRow,它是你的控件应该去的地方。
也许尝试:
e.Row.Height = Unit.Pixel(0);
这心不是正确的答案,但要等到找到正确答案,它可能会在此期间的工作。
不起作用,仍然显示空行:-( – Nicholas 2008-09-22 12:55:20
也许使用CSS来设置显示没有?
我想这是你所需要的:
<asp:GridView ID="grid" runat="server" AutoGenerateColumns="false" ShowFooter="true" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:TemplateField HeaderText="headertext">
<ItemTemplate>
itemtext
</ItemTemplate>
<FooterTemplate>
insert controls
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
和代码隐藏:
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["style"] = "display:none";
}
}
但我不明白为什么要添加你的“插入控件”的页脚,而不是将他们在网格下面。
您可以处理gridview的数据绑定事件并隐藏虚拟行。 (不要忘了事件属性在ASPX代码分配):
protected void GridView1_DataBound(object sender, EventArgs e)
{
if (GridView1.Rows.Count == 1)
GridView1.Rows[0].Visible = false;
}
GridView中有一个特殊的属性来访问页脚行,名为“FooterRow”
然后,你冷尝试yourGrid.FooterRow。 Visible = false;
我在之前的工作上做过这件事,但既然可以添加行,我总是在页脚行中看到它。为了使网格显示出来,我绑定了一个空行,通常是绑定的类型
dim row as Datarow = table.NewRow()
table.AddRow(row)
gridView.DataSource = table
gridView.Databind()
然后它具有所有列,然后您需要。您可以通过拉动这个访问页脚:
'this will get the footer no matter how many rows there are in the grid.
Dim footer as Control = gridView.Controls(0).Controls(gridView.Controls(0).Controls.Count -1)
然后访问任何控件的页脚,你会去做一个:
Dim cntl as Control = footer.FindControl(<Insert Control Name Here>)
我会假设你能够做一个:
footer.Visible = false
使脚注行不可见。
我希望这有助于!
编辑我刚想清楚你说了什么。当我添加一个新行时,我基本上删除了行,但要做到这一点,您需要检查是否有其他行,如果有,请检查是否有值。
要删除虚拟行做这样的事情:
If mTable.Rows.Count = 1 AndAlso mTable.Rows(0)(<first column to check for null value>) Is DBNull.Value AndAlso mTable.Rows(0)(<second column>) Is DBNull.Value AndAlso mTable.Rows(0)(<thrid column>) Is DBNull.Value Then
mTable.Rows.Remove(mTable.Rows(0))
End If
mTable.Rows.Add(row)
gridView.Datasource = mTable
gridView.Databind()
使其可见,只需使用:
Gridview.Rows.Item(i).Attributes.Add("style", "display:block")
,并使其隐形
Gridview.Rows.Item(i).Attributes.Add("style", "display:none")
为什么你没有使用EmptyDataTemplate?它似乎工作,即使我只用它几天伟大的...
你应该在你的GridView使用的DataKeyNames:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="FooID">
,然后检索它在你的代码: GridView1.DataKeys[0].Value.ToString()
其中,“0”是你想要得到的“FooID”
请尝试以下
行的数量protected void GridView1_DataBound(object sender, EventArgs e)
{
GridView1.Rows[0].Visible = false;
}
这隐藏了我的整个GV。 – SearchForKnowledge 2015-03-25 13:18:49
它可以很容易地通过SQL
USE YourdatabaseName select * from TableName where Column_Name <> ''
-1该问题与SQL无关,他正在尝试使用GridView。 – JustinDoesWork 2012-03-23 19:44:57
做。如果你不想当行/列是空来显示数据:
if (!String.IsNullOrEmpty(item.DataName))
{
e.Row.Visible = false;
}
我总是想显示为页脚是我的插入控件包含在哪里,我想隐藏我添加的DummyRow。 – Nicholas 2008-09-22 12:41:32