C# 在 asp.net 中隐藏 gridView 行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/114521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hide a gridView row in asp.net
提问by Nicholas
I am creating a gridView
that allows adding new rows by adding the controls necessary for the insert into the FooterTemplate
, but when the ObjectDataSource
has no records, I add a dummy row as the FooterTemplate
is only displayed when there is data.
我创建一个gridView
允许通过添加控件所需插入到添加新行FooterTemplate
,但是当ObjectDataSource
没有记录,我添加了一个虚拟的行为FooterTemplate
,当有数据时,才会显示。
How can I hide this dummy row? I have tried setting e.row.visible = false
on RowDataBound
but the row is still visible.
我怎样才能隐藏这个虚拟行?我试过设置e.row.visible = false
,RowDataBound
但该行仍然可见。
回答by Orion Adrian
This is the incorrect usage of the GridView control. The GridView control has a special InsertRow which is where your controls should go.
这是 GridView 控件的错误用法。GridView 控件有一个特殊的 InsertRow,它是你的控件应该去的地方。
回答by mattlant
Maybe try:
也许尝试:
e.Row.Height = Unit.Pixel(0);
This isnt the right answer but it might work in the meantime until you get the right answer.
这不是正确的答案,但在您得到正确答案之前,它可能会同时起作用。
回答by mattlant
Maybe use CSS to set display none?!
也许使用 CSS 设置不显示?!
回答by Serhat Ozgel
I think this is what you need:
我认为这就是你需要的:
<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>
and the codebehind:
和代码隐藏:
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["style"] = "display:none";
}
}
But I do not understand why you are adding your "insert controls" to the footer instead of placing them below the grid.
但我不明白您为什么要将“插入控件”添加到页脚而不是将它们放置在网格下方。
回答by John Miller
You could handle the gridview's databound event and hide the dummy row. (Don't forget to assign the event property in the aspx code):
您可以处理 gridview 的数据绑定事件并隐藏虚拟行。(不要忘记在 aspx 代码中分配事件属性):
protected void GridView1_DataBound(object sender, EventArgs e)
{
if (GridView1.Rows.Count == 1)
GridView1.Rows[0].Visible = false;
}
回答by stefano m
GridView has a special property to access Footer Row, named "FooterRow"
GridView 有一个特殊的属性来访问页脚行,名为“FooterRow”
Then, you cold try yourGrid.FooterRow.Visible = false;
然后,你冷试试 yourGrid.FooterRow.Visible = false;
回答by Rob
I did this on a previous job, but since you can add rows, I always had it visible in the footer row. To make it so that the grid shows up, I bound an empty row of the type that is normally bound
我在以前的工作中这样做过,但是由于您可以添加行,所以我总是在页脚行中看到它。为了使网格显示出来,我绑定了一个通常绑定类型的空行
dim row as Datarow = table.NewRow()
table.AddRow(row)
gridView.DataSource = table
gridView.Databind()
then it has all the columns and then you need. You can access the footer by pulling this:
然后它具有所有列,然后您需要。你可以通过拉这个来访问页脚:
'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)
then to access any of the controls in the footer you would go and do a:
然后要访问页脚中的任何控件,您将执行以下操作:
Dim cntl as Control = footer.FindControl(<Insert Control Name Here>)
I'd assume you'd be able to do a:
我假设你能够做一个:
footer.Visible = false
to make the footer row invisible.
使页脚行不可见。
I hope this helps!
我希望这有帮助!
EditI just figured out what you said. I basically delete the row when I add a new one, but to do this you need to check to see if there are any other rows, and if there are, check to see if there are values in it.
编辑我刚刚弄清楚你说的是什么。我在添加新行时基本上会删除该行,但是要执行此操作,您需要检查是否还有其他行,如果有,请检查其中是否有值。
To delete the dummy row do something like this:
要删除虚拟行,请执行以下操作:
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()
回答by Rob
To make it visible, just use:
要使其可见,只需使用:
Gridview.Rows.Item(i).Attributes.Add("style", "display:block")
And to make it invisible
并让它不可见
Gridview.Rows.Item(i).Attributes.Add("style", "display:none")
回答by DiningPhilanderer
Why are you not using the EmptyDataTemplate? It seems to work great even though I have only been using it for a couple days...
为什么不使用 EmptyDataTemplate?即使我只使用了几天,它似乎也很好用......
回答by Davidson Sousa
You should use DataKeyNames in your GridView:
您应该在 GridView 中使用 DataKeyNames:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="FooID">
<asp:GridView ID="GridView1" runat="server" DataKeyNames="FooID">
And then retrieve it on your code:
GridView1.DataKeys[0].Value.ToString()
然后在您的代码中检索它:
GridView1.DataKeys[0].Value.ToString()
Where "0" is the number of the row you want to get the "FooID"
其中“0”是您要获取“FooID”的行号