Scorch Scorch - 3 months ago 12
C# Question

Fill a DataGrid from top to down

I'm creating a DataGrid that is filled by values from a dictionary that contains a string as a key and a List as a value. The key should be the header of the column and the contents of the list were contents of that column:

header1(key) header2(key) header3(key)
value1fromList value1fromList value1fromList
value2fromList value2fromList value2fromList
value3fromList value3fromList


For that, I'm creating a DataTable that is filled with this values and used as the ItemSource for the DataGrid. My issue is that I don't know how to fill the DataTable this way. My solution just fills the DataTable with some gaps and overwrites existing values.

http://oi64.tinypic.com/20f45eh.jpg

public void FillDataGrid(Dictionary<string, List<string>> valuedict)
{
myGrid.Columns.Clear();
var dt = new DataTable();
DataRow row = null;
foreach(KeyValuePair<string, List<string>> entry in valuedict)
{
dt.Columns.Add(entry.Key);
foreach (string word in entry.Value)
{
row = dt.NewRow();
row[entry.Key] = word;
}
dt.Rows.Add(row);
}
myGrid.ItemsSource = dt.AsDataView();
}

Answer

You current code is trying to put non-tabular data into a tabular structure.

You are adding rows to the DataTable before you have created all of the columns.

Your first iteration through your outer foreach loop, you have exactly 1 column (header1). Then inside your inner foreach loop, you are adding rows to your DataTable that only has one column at that point. So after your first iteration, your table will look like this:

header1    
value1fromList
value2fromList
value3fromList

In your second iteration through the outer foreach loop you are adding the second column (header2) and then adding rows to your DataTable that now has 2 columns.

At the end of your second iteration through the outer foreach loop, your table would look like this:

    header1           header2      
1)   value1fromList   
2)   value2fromList   
3)   value3fromList   
4)                    value1fromList2
5)                    value2fromList2    

And when you add header3 to the mix, the data for header3's values would occupy rows 6, 7, and 8.

Now I'm not exactly sure what you are trying to do, but it sounds like you are trying to have a grid that looks like this:

     header1(key)     header2(key)      header3(key)
1)   value1fromList   value1fromList    value1fromList
2)   value2fromList   value2fromList    value2fromList   
3)   value3fromList                     value3fromList              

One way would be to add all of your columns to the table first, and then iterate the largest list adding all 3 columns values to the row at the same time.

foreach(KeyValuePair<string, List<string>> entry in valuedict)
{
    dt.Columns.Add(entry.Key);
}

And then you would iterate the number of items in your largest list adding the values for each column at the same time.

int i = 0;
foreach (string word in MyLongestList.Value)
{
    row = dt.NewRow();
    row[firstList.key] = firstList[i];
    row[secondList.key] = secondList[i];
    row[MyLongestList.key] = word;
    dt.Rows.Add(row);
    i++;
}

Personally, I don't think the structure of your presentation reflects the data accurately. You are putting non-tabular data into a tabular structure. But, it can be done.

Comments