首页 文章

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

提问于
浏览
0

我有 GridView 显示有关作业的一些信息, GridView 中的一个选项是选择原因 . 如果一个原因是't selected then I don' t想要在 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 回答

  • 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

    使用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;
            }
        }
    }
    
  • 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;
                    }
                }
            }
        }
    

相关问题