如果某些列为空,如何在GridView中隐藏行?

时间:2015-08-05 09:27:50

标签: c# asp.net visual-studio gridview

我有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;
    }

3 个答案:

答案 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;
                }
            }
        }
    }