DM Ibtissam
DM Ibtissam

Reputation: 63

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

I have a empty grid view at the page load . In the RowDataBound event I add a dropdownlist to the last cell The problem is that the ddl is shown at the page load. I want to hide it if the cells of row are empty

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


        }

    }

enter image description here Thank you.

Upvotes: 0

Views: 1987

Answers (2)

DM Ibtissam
DM Ibtissam

Reputation: 63

I found the solution The empty cells have the Text equals to   So I add this line and it works

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

Upvotes: 0

user1773603
user1773603

Reputation:

Try this code:

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

Simply add this if check to the code:

// 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
}

Upvotes: 0

Related Questions