Fill RadGridView dynamically

前提是你 提交于 2019-12-25 05:25:24

问题


I am using RadControls for WinForms 2011 Q3

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

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

回答1:


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



来源:https://stackoverflow.com/questions/12849414/fill-radgridview-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!