【问题标题】:How do you bind the DataTextField to a dictionary value using the key如何使用键将 DataTextField 绑定到字典值
【发布时间】:2013-09-04 03:06:00
【问题描述】:

我正在使用 Telerik 的 RadGrid,我希望能够将动态创建的列的 DataTextField 绑定到我用作数据源的对象内的字典内的值。

我的课程结构如下:

public class MyClass
{
    public AnObject ThisObject = null;
    public Dictionary<int, ClassWithStuff> AnotherObject = new Dictionary<int, ClassWithStuff>();
}

我正在动态创建列,我想以这种方式设置列的 DataTextField:

foreach (var item in ListOfPeople.OrderBy(r => r.FormattedName))
{
    var col = new GridHyperLinkColumn();
    rgGrid.MasterTableView.Columns.Add(col);
    col.HeaderText = item.FormattedName;
    col.NavigateUrl = string.Format("~/Person.aspx?id={0}", item.OID);
    col.UniqueName = item.OID.ToString();
    col.DataTextField = "AnotherObject[" + item.OID + "].Person.FormattedName";
}

如果数据源已设置为 MyClass 的对象列表,并且在填充该字典时,OID 将用作字典键。

这基本上是一个带有 MySQL 后端的数据透视表的 radgrid 视图。这是否可能,或者对于 DataTextField 而言,钻入字典项是否太多?我在这里看到的大多数问题都与下拉菜单有关,这不是我正在尝试的。

【问题讨论】:

    标签: c# mysql dictionary radgrid


    【解决方案1】:

    我遇到了完全相同的问题。

    我发现将 GridHyperLinkColumnDataTextField 绑定到字典值的唯一方法是在网格的 ItemDataBound 事件上。

    以下示例基于您的示例结构:

    ...
    // set temporary the ID as a text
    col.Text = item.OID.ToString();
    YourGrid.ItemDataBound += OnYourGridItemBound;
    ...
    private static void OnYourGridItemBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        GridDataItem dataBoundItem = e.Item as GridDataItem;
        if (dataBoundItem != null)
        {
            foreach (TableCell cell in dataBoundItem.Cells)
            {
                if (cell.Controls.Count > 0)
                {
                    var link = cell.Controls[0] as HyperLink;
                    if (link != null)
                    {
                        var dataItem = dataBoundItem.DataItem as MyClass;
                        var id = link.Text;
                        link.Text =                                     
                           dataItem.AnotherObject[id].Person.FormattedName;
                    }
                }
            }
        }
    }
    

    我希望这对你有用。

    【讨论】:

      猜你喜欢
      • 2012-04-07
      • 1970-01-01
      • 2010-09-13
      • 1970-01-01
      • 2011-11-04
      • 1970-01-01
      • 2011-06-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多