Place an "X" where a GridView Cell is Blank

If you are dealing with some data and you know that the GridView cell can be blank and you want to display some text instead of the blank cell then you can use the following code.

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

 OnRowDataBound="GridView1_RowDataBound">
            <Columns>
                <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" />
                <asp:BoundField DataField="Name" HeaderText="Name" />
                <asp:TemplateField HeaderText="Test Column">
                    <ItemTemplate>
                        <asp:Label ID="lblPath" Text=
'<%# Eval("PicturePath") %

>' runat="server"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

And this is the GridView code in the code behind:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        
if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label myLabel = 
new Label();
            myLabel = (Label)e.Row.FindControl("lblPath");
            
string rowValue = myLabel.Text;

            
if (rowValue == null || rowValue.Length < 1)
            {
                myLabel.Text = "X";
            }
        }
    }
Ohh by the way you can also use T-SQL queries to fill the cell with some text. But if you are planning to displaying any image instead of the blank cell then using the GridView_RowDataBound event is a better choice.

 

powered by IMHO 1.3

Print | posted @ Friday, December 30, 2005 11:47 PM

Twitter