MoonKnight
MoonKnight

Reputation: 23833

Adding Rows and Columns to a WPF DataGrid at Run-Time

All, I am new to WPF. I want to read a .resx file and all the related files with different cultures; for example a default 'en-GB' .resx file called 'SomeFile.resx' and 'SomeFile.de-DE.resx' the German analogue. Of course I could have a vast number of different .resx files relating to various cultures.

So, I want to read all the different culture resources into a single DataGrid, so that I have something like

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

et al. My problem is the best way to add this data to a DataGrid called dataGrid. So I currently get the available non-default .resx file into a Dictionary<string, string>

Dictionary<string, string> cultureDict = new Dictionary<string, string>();

Then I plan to loop through the different .resx files (for the numerous cultures) using an ResXResourceReader to read the .resx files like so

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

I had planned to create a List<Dictionary<string, string>> to hold the information from the readers for the different culture and then loop through the List, and add the rows.

My question is What is the best and most efficient way to put the data returned from these readers into the DataGrid bearing in mind the number of cultures is variable and potentially large?

Thanks for your time.

Note: I can add the columns

DataGridTextColumn itemColumn = new DataGridTextColumn();
itemColumn.Header = "Item Number";
itemColumn.Binding = new Binding("Item Number");
dataGrid.Columns.Add(itemColumn);

But it is the best way to add the rows for this particular case.

Upvotes: 2

Views: 2389

Answers (2)

Ghotekar Rahul
Ghotekar Rahul

Reputation: 342

add new row in datagrid using observablecollection ItemCollection

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

Upvotes: 0

apomene
apomene

Reputation: 14389

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

Upvotes: 1

Related Questions