1

我注意到,当我从 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();
    }




}
4

7 回答 7

7

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

if (String.IsNullOrWhiteSpace(e.Row.Cells[1].Text))
{
    // do something with e.Row
}

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

于 2013-11-25T12:42:52.137 回答
3
row.Cells[1].Text.Trim()

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

row.Cells[1].Text.Replace("&nbsp;", "")
于 2011-11-08T07:51:51.453 回答
1

这也有效。rowDataBound在您的活动下添加这段代码

if (e.Row.Cells[1].Text.Length == 0 || e.Row.Cells[1].Text.Equals("&nbsp;") || e.Row.Cells[1].Text.Equals("") || e.Row.Cells[1].Text.Equals(string.Empty))
                {
                    e.Row.Cells[1].Text = string.Empty;
                }
于 2012-03-02T14:56:04.493 回答
1

利用

txtEditCust_ID.Text = Server.HtmlDecode(row.Cells[1].Text.Trim());
于 2013-06-05T16:56:36.517 回答
0
if (e.Row.RowType != DataControlRowType.Header && e.Row.RowType != DataControlRowType.Footer && e.Row.RowType != DataControlRowType.Pager) 

这将删除为&nbsp;我处理的页眉、页脚和寻呼机(如果您正在使用)行。

于 2014-03-03T19:27:04.007 回答
0

删除if语句,只需使用:

txtEditCust_ID.Text = row.Cells[1].Text.Trim(); 

你正在修剪它,所以它应该删除它&nbsp;

于 2010-11-08T14:36:02.607 回答
0

如果要检查 gridview 单元格值是空还是 null,请使用以下命令:

string decodeCellValue = Context.Server.HtmlDecode(e.Row.Cells[i].Text).Trim();
if(string.IsNullOrEmpty(decodeCellValue))
{
  // Cell value empty or NULL
}
else
{
  // Have some value
}
于 2016-09-02T07:45:07.313 回答