我有GridView
显示有关作业的一些信息,GridView
中的一个选项是选择原因。如果没有选择原因,那么我不想在GridView
中显示该行。如何检查Reason列中是否有值,如果它为空,我该如何隐藏该行?
private GridView BuildDebriefGridView()
{
GridView gv = new GridView();
gv.AutoGenerateColumns = false;
//gv.RowDataBound +=
gv.Columns.Add(new BoundField { HeaderText = "Job No", DataField = "JobNo" });
gv.Columns.Add(new BoundField { HeaderText = "Qty Rcvd", DataField = "QtyRcvd" });
gv.Columns.Add(new BoundField { HeaderText = "Reason", DataField = "Reason" });
gv.Columns.Add(new BoundField { HeaderText = "Comment", DataField = "Comment" });
gv.Attributes.Add("style", "width:100%");
return gv;
}
答案 0 :(得分:1)
尝试以下内容;
private void gvTransportListResults_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.Cells["Reason"].Text == "")
e.Row.Visible = false;
}
或者如果是复选框
private void gvTransportListResults_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(((CheckBox)e.Row.FindControl("YourCheckboxID")).Checked == false)
e.Row.Visible = false;
}
答案 1 :(得分:1)
使用GridView_RowDataBound事件可以实现此目的。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[ReasonCellNumber].Text == "Reason")
{
e.Row.Visible = false;
}
}
}
答案 2 :(得分:1)
void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
JobPieceSerialNo item = e.Row.DataItem as JobPieceSerialNo;
if (item != null)
{
if (string.IsNullOrEmpty(item.Reason))
{
e.Row.Visible = false;
}
}
}
}