动态填充RadGridView [英] Fill RadGridView dynamically

查看:74
本文介绍了动态填充RadGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用RadControls for WinForms 2011 Q3

I am using RadControls for WinForms 2011 Q3

RadGridView的数据源是根据用户的输入/选择动态生成的
每当数据源为生成后,我将调用SetDatasource2KeyValuesGrid()
我希望看到的是生成的列和在gridview中填充的值。
但是我看到的是生成了列,但是没有填充值,即使gridview中的行数与其datasource(keyValuesList)中的项目数匹配也是如此。
我一定错过了一些简单的东西。请帮忙。谢谢

The datasource for a RadGridView is dynamically generated based on users' input/selection Everytime when a datasource is generated, I will call SetDatasource2KeyValuesGrid() What I expect to see is columns generated and values filled in gridview. However what I see is columns generated but no value filled even though the number of rows in gridview match the number of items in its datasource(keyValuesList) I must have missed something simple. Please help. thanks

编辑:
我从列表keyValueList创建了一个DataTable,然后将其分配给DataSource,然后它起作用了
只是想知道是否有一个更好的方法。谢谢

I create a DataTable from the list keyValueList, and then assign it to DataSource, then it works Just wonder if there's a better way. thanks

private void CreateTableSetDatasource(List<FeedKeyValueOneSet>) keyValueList)
{
    if(keyValueList==null) return;

    var table = new DataTable();
    table.Columns.Add("Check");
    foreach (var feedKeyValueOneSet in keyValueList)
    {
        var oneset = feedKeyValueOneSet.KeyValueOneSet;               
        foreach (var oneKey in oneset)
        {
            table.Columns.Add(oneKey.key);
        }
        break;
    }

    foreach (var feedKeyValueOneSet in keyValueList)
    {
        var oneset = feedKeyValueOneSet.KeyValueOneSet;
        var numOfCol = oneset.Length + 1;
        var obj = new object[numOfCol];
        obj[0] = "false";
        int idx = 1;
        foreach (var oneKey in oneset)
        {
            obj[idx] = oneKey.value;
            idx++;
        }
        table.Rows.Add(obj);
    }
    radGridKeyValues.DataSource = table;
}


private void SetDatasource2KeyValuesGrid()
{
    if (radGridKeyValues.Columns != null) radGridKeyValues.Columns.Clear();
    radGridKeyValues.AutoGenerateColumns = false;
    radGridKeyValues.EnableFiltering = false;
    radGridKeyValues.ShowFilteringRow = false;
    radGridKeyValues.ShowHeaderCellButtons = false;
    radGridKeyValues.AllowDragToGroup = false;
    radGridKeyValues.AllowAddNewRow = false;
    radGridKeyValues.EnableGrouping = false;

    var keyValueList = (List<FeedKeyValueOneSet>)TimeSeries.FeedValuesCache[m_strFeedName + "_KEYVALUES"];
    if(keyValueList==null) return;

    GridViewDataColumn checkBoxColumn = new GridViewCheckBoxColumn("columnState", "columnState");
    checkBoxColumn.HeaderText = string.Empty;
    if (radGridKeyValues.Columns != null) radGridKeyValues.Columns.Add(checkBoxColumn);

    foreach (var feedKeyValueOneSet in keyValueList)
    {
        var oneset = feedKeyValueOneSet.KeyValueOneSet;
        foreach (var oneKey in oneset)
        {
            var textboxCol = new GridViewTextBoxColumn(oneKey.key, oneKey.key);
            textboxCol.Width = 150;
            textboxCol.ReadOnly = true;
            if (radGridKeyValues.Columns != null) radGridKeyValues.Columns.Add(textboxCol);
        }
        break;
    }

    radGridKeyValues.DataSource = keyValueList;
}

public class FeedKeyValueOneSet
{
    public FeedFieldValues[] KeyValueOneSet;
}

public class FeedFieldValues
{
    public string key { get; set; }
    public string value { get; set; }
}


推荐答案

我从列表中的keyValueList,然后将其分配给DataSource,然后它可以工作
,请参见编辑该问题的代码

I create a DataTable from the list keyValueList, and then assign it to DataSource, then it works see code in edit to the question

这篇关于动态填充RadGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆