renathy
renathy

Reputation: 5355

Change GridView BoundField value on DataBound event

I have GridView (created dynamically) with BoundFields. I want to change BoundField value on DataBound event. This value contains Boolean values (True / False), I need to change them to "Active"/"Inactive". If this would not be dynamic GridView, I would use TemplateField, but, as I am creating GridView dynamically, the easiest way is to do in BoundField.

But I do not understand how exactly to change it.

This is my DataBound event that is fired correctly:

protected void gr_RowDataBound(object sender, GridViewRowEventArgs  e)
    {
        DataRowView drv = (DataRowView)e.Row.DataItem;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (drv["IsRegistered"] != DBNull.Value)
            {
                bool val = Convert.ToBoolean(drv["IsRegistered"]);
                //???? HOW TO CHANGE PREVIOUS VALUE TO NEW VALUE (val) HERE?
            }
        } 
    }

Upvotes: 3

Views: 12317

Answers (2)

Tillito
Tillito

Reputation: 7858

In my case, I did not even know the name or the index of the columns containing the bool value. Therefore, in the first go I check if the value of the cell is "True" or "False", and if so, I remember its index. After this, I know the index and if there are none, I do not do anything, if I found any, then I replays its value.

This is my working code:

// Cache of indexes of bool fields
private List<int> _boolFieldIndexes;

private void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //-- if I checked and there are no bool fields, do not do anything
    if ((_boolFieldIndexes == null) || _boolFieldIndexes.Any())
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //-- have I checked the indexes before?
            if (_boolFieldIndexes == null)
            {
                _boolFieldIndexes = new List<int>();
                for (int i = 0; i < e.Row.Cells.Count; i++)
                {
                    if ((e.Row.Cells[i].Text == "True") || (e.Row.Cells[i].Text == "False"))
                    {
                        // remember which column is a bool field
                        _boolFieldIndexes.Add(i);
                    }
                }
            }
            //-- go through the bool columns:
            foreach (int index in _boolFieldIndexes)
            {
                //-- replace its value:
                e.Row.Cells[index].Text = e.Row.Cells[index].Text
                    .Replace("True", "Ja")
                    .Replace("False", "Nein");
            }
        }
    }
}

The good thing is, that this works for any gridview. Just attach the event.

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460138

With BoundFields you cannot use FindControl to find a control in a TemplateField to set it's Text property for instance. Instead you set the Cell-Text:

protected void gr_RowDataBound(object sender, GridViewRowEventArgs  e)
{
    DataRowView drv = (DataRowView)e.Row.DataItem;
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (drv["IsRegistered"] != DBNull.Value)
        {
            bool val = Convert.ToBoolean(drv["IsRegistered"]);
             // assuming that the field is in the third column
            e.Row.Cells[2].Text =  val ? "Active" : "Inactive";
        }
    } 
} 

Apart from that, you can use TemplateFields even in a dynamic GridView.

How to add TemplateField programmatically

Upvotes: 7

Related Questions