在运行时将行和列添加到WPF DataGrid

时间:2013-03-26 16:38:53

标签: c# wpf datagrid

全部,我是WPF的新手。我想读一个.resx文件和所有不同文化的相关文件;例如,名为'SomeFile.resx'和'SomeFile.de-DE.resx'的默认'en-GB'.resx文件是德语类似物。当然,我可以拥有与各种文化相关的大量不同的.resx文件。

所以,我想把所有不同的文化资源都读成一个DataGrid,所以我有类似的东西

Resource Index | Resource Key | English (Default) | German
1              | SomeKey1     | Run Script        | Skript Ausführen 
2              | SomeKey2     | OK                | OK

等。我的问题是将此数据添加到名为DataGrid的{​​{1}}的最佳方法。所以我目前将可用的非默认.resx文件转换为dataGrid

Dictionary<string, string>

然后我计划使用Dictionary<string, string> cultureDict = new Dictionary<string, string>(); 循环浏览不同的.resx文件(针对众多文化)来读取.resx文件,如此

ResXResourceReader

我原本计划创建一个Dictionary<string, string> resourceMap = new Dictionary<string, string>(); public static void Func(string fileName) { ResXResourceReader rsxr = new ResXResourceReader(fileName); foreach (DictionaryEntry d in rsxr) { resourceMap.Add(d.Key.ToString(),d.Value.ToString()); } rsxr.Close(); } public string GetResource(string resourceId) { return resourceMap[resourceId]; } 来保存读者对不同文化的信息,然后遍历List<Dictionary<string, string>>,然后添加行。

我的问题是将这些读者返回的数据放入List的最佳和最有效的方法是什么?记住文化的数量是多变的,可能很大? < / p>

感谢您的时间。

注意:我可以添加列

DataGrid

但这是为这种特殊情况添加行的最佳方式。

2 个答案:

答案 0 :(得分:1)

public static void Display_Grid(DataGrid d, List<string> S1)
{
    ds = new DataSet();
    DataTable dt = new DataTable();
    ds.Tables.Add(dt);

    DataColumn cl = new DataColumn("Item Number", typeof(string));
    cl.MaxLength = 200;
    dt.Columns.Add(cl);

    int i = 0;
    foreach (string s in S1)
    {
        DataRow rw = dt.NewRow();
        rw["Item Number"] = S1[i];
        i++;
    }
    d.ItemsSource = ds.Tables[0].AsDataView();
}

答案 1 :(得分:0)

使用observablecollection ItemCollection在datagrid中添加新行

    itemmodel model=new itemmodel ();
model.name='Rahul';
ItemCollection.add(model);