空白 Gridview 单元格填充“ ”进入文本框
我注意到,当我从 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();
}
}
I've noticed that when i populate textboxes from a selected row in a gridview that if the field is blank it displays " " in the textbox.
Here is the solution I came up with. I check each cell before adding it to the textbox.
I get the feeling that I'm either doing something wrong to have this problem in the first place or that there is a better way to handle this.
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
仍然是一个小黑客,但可能比处理
更好。您可以在 GridView 列
上设置NullDisplayText=" "
,然后使用条件,例如:在本例中,没有
& nbsp;
首先。Still a minor hack, but probably better than dealing with
. You can set
NullDisplayText=" "
on GridView column<asp:BoundField>
and then use condition like for example:In this case, there is no
to begin with.
不适用于
,请替换它:
is not working for
, replace it instead:
这也有效。在您的
rowDataBound
事件下添加这段代码This works too. Add this piece of code under your
rowDataBound
event使用
use
删除
if
语句,只需使用:您正在修剪它,因此它应该删除
。
Remove the
if
statement, just use:You are trimming it so it should remove the
anyway.
这会删除为我处理
的页眉、页脚和分页器(如果您正在使用)行。
This removes the header, footer, and pager (if you are using) rows which took care of the
for me.
如果要检查 gridview 单元格值是否为空或 null,请使用以下命令:
If you want to check the gridview cell value whether empty or null, use this: