隐藏gridview中的空行 [英] Hide empty row in gridview
问题描述
I want to hide empty row in one particular column. I tried to but negative. Below is my code:
我的尝试:
What I have tried:
protected void gvDb_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow rw in gvDb.Rows)
{
if ((string.IsNullOrEmpty(rw.Cells[1].Text) | (rw.Cells[1].Text == "")))
{
rw.Visible = false;
}
}
}
推荐答案
尝试
try
private void gvDb_DataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
if (e.Row.Cells[1].Text == "")
e.Row.Visible = false;
}
如果您调试代码,您将看到它无法正常工作的原因。您假设没有数据的项目的网格视图单元格是空字符串,但它可能不是(否则您的代码将起作用)。根据您使用的模板,可能会有新行或不间断空格。在代码上设置断点并检查实际值
If you debug your code you'll see why it isn't working. You are assuming that the grid view cell for an item with no data is an empty string, but it probably isn't (otherwise your code would work). Depending on the template you are using there might be new lines, or non-breaking spaces. Put a breakpoint on the code and check the actual value of
rw.Cells[1]
你会发现它不是你所期望的。
无论如何,检查gridview的输出是解决这个问题的错误方法是,你可以提高行对底层数据的可见性,而不是如何在html中呈现数据。如何执行此操作取决于绑定到行的内容,但是您希望使用针对每一行触发的RowDataBound事件,并在该事件中查询该行的基础数据,并确定该行是否可见。 br />
我的gridview
and you'll find it isn't what you are expecting.
Anyway, checking the gridview's output is the wrong way of tacking this, you could drive the row's visibility on the underlying data, not how that data is rendered in html. How you do this depends on what you are binding to your row, but you want to use the RowDataBound event which is fired for each row, and in that event query the underlying data for the row and work out if the row is visible.
My gridview
<asp:GridView ID="MyGridView" runat="server" AutoGenerateColumns="true" OnRowDataBound="MyGridView_RowDataBound">
</asp:GridView>
这是用于绑定DataTable
This is for binding a DataTable
protected void Page_Load(object sender, EventArgs e)
{
DataTable data = new DataTable();
data.Columns.Add("ID", typeof(int));
data.Columns.Add("Name", typeof(string));
data.Rows.Add(1, "John");
data.Rows.Add(2, "");
data.Rows.Add(3, "Dave");
// you can set this event in code behind like below if you don't want to use the
// OnRowDataBound attribute on the asp:GridView
// MyGridView.RowDataBound += MyGridView_RowDataBound;
MyGridView.DataSource = data;
MyGridView.DataBind();
}
protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
// This event if fired for all template types (header, data, footer etc)
// but we're only interested in the data rows so if this isn't a data
// row exit the event
if (e.Row.RowType != DataControlRowType.DataRow)
{
return;
}
// cast e.Row.DataItem to the underlying data type
DataRowView data = (DataRowView)e.Row.DataItem;
// check that data for empty values
if (string.IsNullOrWhiteSpace((string)data["Name"]))
{
e.Row.Visible = false;
}
}
或者如果你绑定到一个对象列表
Or if you are binding to a List of objects
public class MyData
{
public int ID { get; set; }
public string Name { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
List<MyData> data = new List<MyData>();
data.Add(new MyData { ID = 1, Name = "John" });
data.Add(new MyData { ID = 2, Name = "" });
data.Add(new MyData { ID = 3, Name = "Dave" });
// you can set this event in code behind like below if you don't want to use the
// OnRowDataBound attribute on the asp:GridView
// MyGridView.RowDataBound += MyGridView_RowDataBound;
MyGridView.DataSource = data;
MyGridView.DataBind();
}
protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
// This event if fired for all template types (header, data, footer etc)
// but we're only interested in the data rows so if this isn't a data
// row exit the event
if (e.Row.RowType != DataControlRowType.DataRow)
{
return;
}
// cast e.Row.DataItem to the underlying data type
MyData data = (MyData)e.Row.DataItem;
// check that data for empty values
if (string.IsNullOrWhiteSpace(data.Name))
{
e.Row.Visible = false;
}
}
这篇关于隐藏gridview中的空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!