【问题标题】:GridView and RowDataBound eventGridView 和 RowDataBound 事件
【发布时间】:2014-08-30 11:31:48
【问题描述】:

我可以在 GridView 中编辑单行。

但是当在 MySQL 数据库中字段 "card" 的值不为空时,我需要在 GridView 的单行中停止编辑(并以关闭的形式传递该行州)。

我尝试了这个解决方案,但是在 GridView 中,即使字段 “card” 的值为 null,我的 GridView 的所有行都处于 “关闭状态”,为什么?

RowDataBound 事件中插入了条件,对吗?

下面是我的代码。

非常感谢您在解决这个问题时给我的任何帮助。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var obj = ((DataRowView)e.Row.DataItem)["card"];
        ImageButton edit = (ImageButton)e.Row.FindControl("imgbtnEdit");

        if (obj != null)
        {
            edit.Enabled = false;
            edit.ToolTip = "Closed state";
        }
    }


    if (e.Row.RowType == DataControlRowType.Pager)
    {
        DropDownList ddl = (DropDownList)(e.Row.FindControl("ddlpages"));
        Label lblPageCount = (Label)e.Row.FindControl("lblPageCount");

        if (lblPageCount != null)
            lblPageCount.Text = GridView1.PageCount.ToString();

        for (int i = 1; i <= GridView1.PageCount; i++)
        {
            ddl.Items.Add(i.ToString());
        }

        ddl.SelectedIndex = GridView1.PageIndex;

        if (GridView1.PageIndex == 0)
        {
            ((ImageButton)e.Row.FindControl("ImageButton1")).Visible = false;
            ((ImageButton)e.Row.FindControl("ImageButton2")).Visible = false;
        }

        if (GridView1.PageIndex + 1 == GridView1.PageCount)
        {
            ((ImageButton)e.Row.FindControl("ImageButton3")).Visible = false;
            ((ImageButton)e.Row.FindControl("ImageButton4")).Visible = false;
        }
    }
}

【问题讨论】:

    标签: c# mysql asp.net gridview


    【解决方案1】:

    这是因为DBNull.Valuenull 是两个不同的东西。

    var obj = ((DataRowView)e.Row.DataItem)["card"];
    if (!DBNull.Value.Equals(obj))
    {
        edit.Enabled = false;
        edit.ToolTip = "Closed state";
    }
    

    这将检查“card”值是否为空数据库值,而不是空对象。

    【讨论】:

      猜你喜欢
      • 2018-08-13
      • 2011-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-14
      相关资源
      最近更新 更多