Hamamelis
Hamamelis

Reputation: 2115

GridView and RowDataBound event

I can edit single row in the GridView.

But when in MySQL database the value of field "card" is not null I need stopping the edit in this single row of GridView (and passed the row in closed state).

I tried this solution, but in GridView I've all rows of GridView in "closed state" even when the value of field "card" is null, why?

The condition is inserted in RowDataBound event, it's correct?

My code below.

I would greatly appreciate any help you can give me in working this problem.

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;
        }
    }
}

Upvotes: 0

Views: 721

Answers (1)

j.f.
j.f.

Reputation: 3949

This is because DBNull.Value and null are two different things.

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

This will check to see if the "card" value is a null database value, rather than a null object.

Upvotes: 1

Related Questions