【问题标题】:Change GridView BoundField value on DataBound event在 DataBound 事件上更改 GridView BoundField 值
【发布时间】:2012-10-05 14:23:01
【问题描述】:

我有 GridView(动态创建)和 BoundFields。我想更改 DataBound 事件的 BoundField 值。该值包含布尔值(True / False),我需要将它们更改为“Active”/“Inactive”。如果这不是动态 GridView,我会使用 TemplateField,但是,由于我是动态创建 GridView,最简单的方法是在 BoundField 中进行。

但我不明白如何更改它。

这是我正确触发的 DataBound 事件:

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

【问题讨论】:

  • 这对我来说似乎并不容易。我试图在网上找到一些不错的简单示例,但找不到。另外对于其他一些列,我需要调用其他方法来格式化数据。
  • 我有一个类似的场景:许多 Gridviews 使用绑定字段。默认情况下,布尔值呈现为“真”或“假”。我希望将它们翻译成德语,如“Ja”/“Nein”。请参阅我的答案...

标签: .net gridview


【解决方案1】:

对于 BoundFields,您不能使用 FindControl 在 TemplateField 中查找控件来设置它的 Text 属性。相反,您设置 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";
        }
    } 
} 

除此之外,您甚至可以在动态的GridView 中使用TemplateFields

How to add TemplateField programmatically

【讨论】:

  • 我还发布了我的代码,这也适用于您不知道哪些是布尔字段的情况。
【解决方案2】:

就我而言,我什至不知道包含 bool 值的列的名称或索引。因此,在第一次检查中,我检查单元格的值是“真”还是“假”,如果是,我记得它的索引。在此之后,我知道索引,如果没有,我什么都不做,如果我找到了,那么我会重放它的值。

这是我的工作代码:

// 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");
            }
        }
    }
}

好消息是,这适用于任何网格视图。只需附加事件即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-06
    • 1970-01-01
    • 2011-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-16
    相关资源
    最近更新 更多