Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Empty Gridview data from populating " " into textbox

I have this code that populates a textbox based on a cell in the selected row of a gridview

 protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

        txtComment.Text = row.Cells[14].Text.Trim();

    }

It displays   in the txtComment textbox if Cell[14] has no data.

Is there a way to prevent the   from appearing when there is no data in the cell of the selected row?


Edit I tried this and it didn't work

if (row.Cells[14].Text.Trim().Length > 1)
{
    txtComment.Text = row.Cells[14].Text.Trim();
}
else
{
    txtComment.Text = row.Cells[14].Text = "";
}

===================================================================

This worked

if (row.Cells[14].Text.Trim()!=" ")
{
    txtComment.Text = row.Cells[14].Text.Trim();
}
else
{
    txtComment.Text = row.Cells[14].Text = "";
}
like image 284
Joshua Slocum Avatar asked Sep 17 '10 18:09

Joshua Slocum


2 Answers

Use this:

Server.HtmlDecode()

For example:

txtComment.Text = Server.HtmlDecode(row.Cells[14].Text);

It handles not only spaces, but also other conversions like ampersands(&) and etc.

like image 110
Carlitos12 Avatar answered Oct 15 '22 16:10

Carlitos12


Use NullDisplayText=" "

     <asp:BoundField DataField="EmployeeName" HeaderText="Name" NullDisplayText=" "/>
like image 35
Alex Z Avatar answered Oct 15 '22 16:10

Alex Z