空白Gridview单元格将"&nbsp"填充到文本框中

Jos*_*cum 0 asp.net gridview selectedindexchanged

我注意到当我从gridview中的选定行填充文本框时,如果该字段为空,则在文本框中显示" ".

这是我提出的解决方案.我在检查每个单元格之前将其添加到文本框中.

我觉得我要么做错了,要么首先出现这个问题,要么就是有更好的方法来解决这个问题.

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{

    //// Get the currently selected row using the SelectedRow property.
    GridViewRow row = GridView1.SelectedRow;

    // Load data from selected row into textboxes
    if (row.Cells[1].Text.Trim() != " ")
    {
        txtEditCust_ID.Text = row.Cells[1].Text.Trim();
    }




}
Run Code Online (Sandbox Code Playgroud)

yos*_*osh 6

仍然是一个小的黑客,但可能比处理更好&nbsp;.您可以NullDisplayText=" "在GridView列上进行设置<asp:BoundField>,然后使用条件,例如:

if (String.IsNullOrWhiteSpace(e.Row.Cells[1].Text))
{
    // do something with e.Row
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,没有&nbsp;开始.


小智 5

row.Cells[1].Text.Trim()
Run Code Online (Sandbox Code Playgroud)

不适用于&nbsp;,请替换它:

row.Cells[1].Text.Replace("&nbsp;", "")
Run Code Online (Sandbox Code Playgroud)