动态填充RadGridView

时间:2012-10-11 22:16:23

标签: telerik datasource radgridview

我正在使用RadControls for WinForms 2011 Q3

RadGridView的数据源是根据用户的输入/选择动态生成的 每次生成数据源时,我都会调用SetDatasource2KeyValuesGrid() 我期望看到的是生成的列和gridview中填充的值。 然而,我看到的是生成的列,但没有填充值,即使gridview中的行数与其数据源中的项数匹配(keyValuesList) 我一定错过了一些简单的事情。请帮忙。谢谢

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

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

1 个答案:

答案 0 :(得分:0)

我从列表keyValueList创建一个DataTable,然后将其分配给DataSource,然后它工作 请参阅编辑问题的代码