【问题标题】:How to test if a row of gridview is empty asp.net如何测试一行gridview是否为空asp.net
【发布时间】:2018-04-04 15:38:30
【问题描述】:

我在页面加载时有一个空的网格视图。 在 RowDataBound 事件中,我将下拉列表添加到最后一个单元格 问题是 ddl 在页面加载时显示。 如果行的单元格为空,我想隐藏它

protected void grw_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList ddl = e.Row.FindControl("ddlAccepted") as DropDownList;
            if (null != ddl)
            {
                string acceptedKey = (e.Row.FindControl("lblAccepted") as Label).Text;
                string acceptedValue = "";
                if (acceptedKey == "True")
                {
                    acceptedValue = "Oui";
                }
                else if (acceptedKey == "False")
                {
                    acceptedValue = "Non";
                }

                ddl.Items.Add(new ListItem("Non", "False"));
                ddl.Items.Add(new ListItem("Oui", "True"));

                ddl.SelectedValue = acceptedValue;
            }


        }

    }

谢谢。

【问题讨论】:

  • 你是如何填充gridview的?
  • string requete = "select '' as id,'' as caseNumber, "'' as intervener,'' as requester,'' as SendDate, '' as recipient, "'' as ReceptionDate,'' as StateCode,'' as IsAccepted"; grw.DataSource = GetEmptyDataSet(requete); grw.DataBind();
  • GetEmptyDataSet 返回一个没有行或有空行的 DataSet?
  • public static DataTable GetEmptyDataSet(string query) { DataTable dataTable = null; SqlConnection connection = null; try { connection = Open(); SqlCommand command = new SqlCommand(query, connection); SqlDataAdapter adapter = new SqlDataAdapter(command); dataTable = new DataTable(); adapter.Fill(dataTable); } finally { Close(connection); } return dataTable; }

标签: asp.net gridview webforms dropdown rowdatabound


【解决方案1】:

我找到了解决方案 空单元格的文本等于  所以我添加了这一行,它的工作原理

if (e.Row.Cells[0].Text != " ")

【讨论】:

    【解决方案2】:

    试试这个代码:

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // Check if the first two cells of row are not empty
        if (e.Row.Cells[0].Text != "" || e.Row.Cells[1].Text != "")
        {
            DropDownList ddl = e.Row.FindControl("ddlAccepted") as DropDownList;
            if (null != ddl)
            {
                string acceptedKey = (e.Row.FindControl("lblAccepted") as Label).Text;
                string acceptedValue = "";
                if (acceptedKey == "True")
                {
                    acceptedValue = "Oui";
                }
                else if (acceptedKey == "False")
                {
                    acceptedValue = "Non";
                }
    
                ddl.Items.Add(new ListItem("Non", "False"));
                ddl.Items.Add(new ListItem("Oui", "True"));
    
                ddl.SelectedValue = acceptedValue;
            }
        }
    }
    

    只需将此if 支票添加到代码中:

    // Check if the first two cells of row are not empty
    if (e.Row.Cells[0].Text != "" || e.Row.Cells[1].Text != "")
    {
        //... do some other code here
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-27
      • 2011-09-17
      • 2011-10-11
      • 2011-02-04
      • 2019-03-22
      • 2011-11-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多