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.
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.
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. |