Dealing with GridView Empty Cells
By AzamSharp
Views: 12329

Introduction:

Consider a situation where you are displaying data in the GridView control and some of the cells does not contain anything and hence does not display anything. In this article we will see that how you can show certain messages when the cell inside the GridView control does not contain any data.

Populating the GridView Control:

The first task is to populate the GridView control. Here is the code that populates the GridView control.

protected void Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack)

{

BindData();

}

}

private void BindData()

{

// make the query

string query = "SELECT * FROM Customer";

SqlConnection myConnection = new SqlConnection(ConnectionString);

SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);

DataSet ds = new DataSet();

ad.Fill(ds, "Customer");

GridView1.DataSource = ds;

GridView1.DataBind();

}

private string ConnectionString

{

get { return @"Server=localhost;Database=MyDatabase;Trusted_Connection=true"; }

}

Here is what our GridView looks like:

As you can see in the above screen shot that some values of the FilePath column does not contain anything. Let's display an "X' where the file path does not exists.

Display an "X" where the column value does not exists:

Let's see how we can display an "X" where the value of the FilePath column does not exist.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"

ForeColor="#333333" GridLines="None" OnRowDataBound="GridView1_RowDataBound">

<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />

<Columns>

<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" />

<asp:BoundField DataField="Name" HeaderText="Name" />

<asp:TemplateField HeaderText="FilePath">

<ItemTemplate>

<asp:Label ID="lblFilePath" Text='<%# Eval("PicturePath") %>' runat="server"></asp:Label>

<asp:Image ID="imgPath" Visible="false" runat="server" />

</ItemTemplate>

</asp:TemplateField>

</Columns>

<RowStyle BackColor="#E3EAEB" />

<EditRowStyle BackColor="#7C6F57" />

<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />

<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />

<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />

<AlternatingRowStyle BackColor="White" />

</asp:GridView>

Now, let's take a look at the code behind:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

Label myLabel = (Label)e.Row.FindControl("lblFilePath");

string text = myLabel.Text;

if (text == null || text.Length < 1)

{

myLabel.Text = "X";

}

}

}

Now if you ran the page you will see that when the data for the FilePath column does not exists it will display an "X" sign.

let's make it little fancy and display an image instead of the "X" sign.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

Label myLabel = (Label)e.Row.FindControl("lblFilePath");

string text = myLabel.Text;

Image myImage = (Image)e.Row.FindControl("imgPath");

myImage.ImageUrl = "Images/icon-delete.gif";

if (text == null || text.Length < 1)

{

myImage.Visible = true;

}

}

}

 

Take a look at the screen shot below:

I hope you liked the article. Please also download the complete code at the end of this article.

If you are one of those thousands of people who are benefited from GridViewGuy articles and want to show your love then you will be interested in giving a donation.

Make a Donation

Once, again thank you very much and remember its because of you FINE people that this website is up and running.

 

Export Button is a custom control that let's you export your DataGrid or TextBox data to several different formats. The control is extremely easy to use and also exposes design time features. In this article I will discuss some of the features of the Export Button and how it benefits the developer.

BUY IT NOW

 

 

By AzamSharp


Enter Comment/Feedback
  •  
  •  
  •  
  •  
  •  

Comments/Feedbacks
Subject: Question
Name: Alfonso Calderon
Date: 6/22/2007 3:07:44 PM
Comment:
I have an gridview that gets its data from an excel worksheet throug a dataset, I add the columns in the designer, I have one button column with an edit button, I have serval bound field columns, and I have one template column with a label in the itemtemplate and a dropdownlist in the edititemtemplate.
My problem is that when I click the edit button nothing happens, no textboxes and no dropdownlist.
I try to catch the onrowediting event but I don't know where to go from there.
Thanks in advance and best regards.
AC
Subject:  
Name: Alee
Date: 8/21/2007 7:02:19 PM
Comment:
When I populate the Gridview, and if one of the cells is blank, i.e. a blank was returned from my query. That cell now has a value of " " when I check if my cell is blank or not. How can I get rid of the value " ". The value " " does not show on the grid, but when I debug the code and check if the LEN(row.cells(1).text) = 0, it retruns false.

Thanks.

Subject: Need Help!
Name: Michael
Date: 12/3/2007 9:38:55 PM
Comment:
I also have the same problem.

Any suggestion?
Subject: Try Following Code
Name: Pankaj Madhukar Unhale
Date: 12/4/2007 1:35:52 AM
Comment:
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
if (e.Row.Cells[i].Text.ToString() == " ")
{
Response.Write("hi
");
}
}
}
Subject: RE: Thanks!
Name: AzamSharp
Date: 12/5/2007 1:39:59 PM
Comment:
Hi Pankaj Madhukar Unhale, Thanks for helping out on KoffeeKoder by replying to the user's question.
Subject: Empty Data Cells
Name: Menon
Date: 4/1/2008 1:05:45 AM
Comment:
I am facing a problem with Edit and delete images. When the page loads for the first time i am able to hide the Edit and Delete Images if there is no Data in GridView. Now i have a Button in the page for Update. The Button is inside Update Panel. The GridView is also in Separate UpdatePanel. Now When i click the Button. The GridView Suddenely Displays the Edit and Delete Button? It doesnt go in the DataBound method when i click on the Update button. I dont know what i can do? Any suggestions or help would be appreciated,

Thanks
Subject: Particular Color for Text
Name: Agnes Loyola
Date: 4/27/2008 11:34:35 PM
Comment:
how to display the text X for null data in Red Color or some other Color



Join WebHost4Life.com






Copyright GridViewGuy 2007-2008