Click here to Skip to main content
15,944,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to hide empty row in one particular column. I tried to but negative. Below is my code:


What I have tried:

protected void gvDb_DataBound(object sender, EventArgs e)
    {
        foreach (GridViewRow rw in gvDb.Rows)
        {
            if ((string.IsNullOrEmpty(rw.Cells[1].Text) | (rw.Cells[1].Text == "")))
            {
                rw.Visible = false;
            }
        }
    }
Posted
Updated 14-Jun-17 23:23pm

try

private void gvDb_DataBound(Object sender, GridViewRowEventArgs e)
 {
 if (e.Row.RowType == DataControlRowType.DataRow)      
    if (e.Row.Cells[1].Text == "") 
        e.Row.Visible = false;
 }
 
Share this answer
 
If you debug your code you'll see why it isn't working. You are assuming that the grid view cell for an item with no data is an empty string, but it probably isn't (otherwise your code would work). Depending on the template you are using there might be new lines, or non-breaking spaces. Put a breakpoint on the code and check the actual value of

rw.Cells[1]


and you'll find it isn't what you are expecting.

Anyway, checking the gridview's output is the wrong way of tacking this, you could drive the row's visibility on the underlying data, not how that data is rendered in html. How you do this depends on what you are binding to your row, but you want to use the RowDataBound event which is fired for each row, and in that event query the underlying data for the row and work out if the row is visible.

My gridview

<asp:GridView ID="MyGridView" runat="server" AutoGenerateColumns="true" OnRowDataBound="MyGridView_RowDataBound">
    
</asp:GridView>


This is for binding a DataTable

protected void Page_Load(object sender, EventArgs e)
{
    DataTable data = new DataTable();
    data.Columns.Add("ID", typeof(int));
    data.Columns.Add("Name", typeof(string));

    data.Rows.Add(1, "John");
    data.Rows.Add(2, "");
    data.Rows.Add(3, "Dave");

    // you can set this event in code behind like below if you don't want to use the
    // OnRowDataBound attribute on the asp:GridView

    // MyGridView.RowDataBound += MyGridView_RowDataBound;

    MyGridView.DataSource = data;
    MyGridView.DataBind();
}

protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    // This event if fired for all template types (header, data, footer etc)
    // but we're only interested in the data rows so if this isn't a data
    // row exit the event
    if (e.Row.RowType != DataControlRowType.DataRow)
    {
        return;
    }

    // cast e.Row.DataItem to the underlying data type
    DataRowView data = (DataRowView)e.Row.DataItem;

    // check that data for empty values
    if (string.IsNullOrWhiteSpace((string)data["Name"]))
    {
        e.Row.Visible = false;
    }
}


Or if you are binding to a List of objects

public class MyData
{
    public int ID { get; set; }
    public string Name { get; set; }
}


protected void Page_Load(object sender, EventArgs e)
{
    List<MyData> data = new List<MyData>();
    data.Add(new MyData { ID = 1, Name = "John" });
    data.Add(new MyData { ID = 2, Name = "" });
    data.Add(new MyData { ID = 3, Name = "Dave" });

    // you can set this event in code behind like below if you don't want to use the
    // OnRowDataBound attribute on the asp:GridView

    // MyGridView.RowDataBound += MyGridView_RowDataBound;

    MyGridView.DataSource = data;
    MyGridView.DataBind();
}

protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    // This event if fired for all template types (header, data, footer etc)
    // but we're only interested in the data rows so if this isn't a data
    // row exit the event
    if (e.Row.RowType != DataControlRowType.DataRow)
    {
        return;
    }

    // cast e.Row.DataItem to the underlying data type
    MyData data = (MyData)e.Row.DataItem;

    // check that data for empty values
    if (string.IsNullOrWhiteSpace(data.Name))
    {
        e.Row.Visible = false;
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900