【发布时间】:2008-09-22 12:23:36
【问题描述】:
我正在创建一个gridView,它允许通过将插入所需的控件添加到FooterTemplate 中来添加新行,但是当ObjectDataSource 没有记录时,我添加了一个虚拟行,因为FooterTemplate 是有数据时才显示。
如何隐藏这个虚拟行?我尝试在RowDataBound 上设置e.row.visible = false,但该行仍然可见。
【问题讨论】:
我正在创建一个gridView,它允许通过将插入所需的控件添加到FooterTemplate 中来添加新行,但是当ObjectDataSource 没有记录时,我添加了一个虚拟行,因为FooterTemplate 是有数据时才显示。
如何隐藏这个虚拟行?我尝试在RowDataBound 上设置e.row.visible = false,但该行仍然可见。
【问题讨论】:
您可以处理 gridview 的数据绑定事件并隐藏虚拟行。 (不要忘记在 aspx 代码中分配事件属性):
protected void GridView1_DataBound(object sender, EventArgs e)
{
if (GridView1.Rows.Count == 1)
GridView1.Rows[0].Visible = false;
}
【讨论】:
请尝试以下方法
protected void GridView1_DataBound(object sender, EventArgs e)
{
GridView1.Rows[0].Visible = false;
}
【讨论】:
我认为这是你需要的:
<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";
}
}
但我不明白您为什么要将“插入控件”添加到页脚而不是将它们放在网格下方。
【讨论】:
不妨试试:
e.Row.Height = Unit.Pixel(0);
这不是正确的答案,但在您得到正确答案之前它可能会起作用。
【讨论】:
也许用 CSS 来设置不显示?!
【讨论】:
这是 GridView 控件的错误使用。 GridView 控件有一个特殊的 InsertRow,这是您的控件应该去的地方。
【讨论】:
GridView 有一个特殊的属性可以访问 Footer Row,名为“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()
【讨论】:
您为什么不使用 EmptyDataTemplate?即使我只使用了几天,它似乎也很有效......
【讨论】:
您应该在 GridView 中使用 DataKeyNames:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="FooID">
然后在您的代码中检索它:
GridView1.DataKeys[0].Value.ToString()
其中“0”是您要获取“FooID”的行号
【讨论】:
要使其可见,只需使用:
Gridview.Rows.Item(i).Attributes.Add("style", "display:block")
让它不可见
Gridview.Rows.Item(i).Attributes.Add("style", "display:none")
【讨论】:
如果不想在列/行为空时显示数据:
if (!String.IsNullOrEmpty(item.DataName))
{
e.Row.Visible = false;
}
【讨论】:
通过SQL可以轻松完成
USE YourdatabaseName select * from TableName where Column_Name <> ''
【讨论】: