如何测试gridview一行是否为空asp.net

How to test if a row of gridview is empty asp.net

我在页面加载时有一个空的网格视图。 在 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;
            }


        }

    }

谢谢。

试试这个代码:

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
}

我找到了解决方案 空单元格的文本等于   所以我添加了这一行并且它有效

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