Reputation: 220
I have a DataGrid
with three columns (step number, requirement number, description). Once the application is run the user shall choose an excel spreadsheet and I am pulling information from this spreadsheet to fill into my DataGrid
. In my code I already have the strings I need i just need to know how to programmatically add a row then fill said row with these strings.
Heres my DataGrid
:
<DataGrid x:Name="dataGrid" Margin="10,167,11,10"
SelectionChanged="dataGrid_SelectionChanged" MinColumnWidth="22">
<DataGrid.Columns>
<DataGridTextColumn Header="Step Number" />
<DataGridTextColumn Header="Requirement" />
<DataGridTextColumn Header="Description" Width="*" />
</DataGrid.Columns>
</DataGrid>
Here is the loop I want to be creating rows in:
foreach (KeyValuePair<string, List<int>> Req in reqLocations)
{
//create row
// ADD INFO FROM DICTIONARY INTO A ROW
}
Upvotes: 0
Views: 2802
Reputation: 12751
After agreeing with comments, if you still want to look into.... below is simple example for your XAML
Create a simple class with all your data column headers
public class dataGriditems
{
public string StepNumber { get; set; }
public string Requirement { get; set; }
public string Description { get; set; }
}
In the XAML add the binding for each column and ensure the binding name is same as the class properties, that you are going add as item source (In this case the above class)
<DataGridTextColumn Header="Step Number" Binding="{Binding StepNumber}" />
<DataGridTextColumn Header="Requirement" Binding="{Binding Requirement}" />
<DataGridTextColumn Header="Description" Binding="{Binding Description}" Width="*" />
In the XAML set AutoGenerateColumns="False" for data grid.
<DataGrid x:Name="dataGrid" Margin="10,167,11,10"
SelectionChanged="dataGrid_SelectionChanged" MinColumnWidth="22" AutoGenerateColumns="False">
In the code, create a simple list of objects of above defined class and assign the list to dataGrid.ItemsSource
as shown below
List<dataGriditems> items = new List<dataGriditems>();
items.Add(new dataGriditems() { StepNumber = "one", Requirement = "req1", Description = "desc1"});
items.Add(new dataGriditems() { StepNumber = "two", Requirement = "req2", Description = "desc2" });
items.Add(new dataGriditems() { StepNumber = "three", Requirement = "req3", Description = "desc3" });
items.Add(new dataGriditems() { StepNumber = "four", Requirement = "req4", Description = "desc4" });
dataGrid.ItemsSource = items;
Upvotes: 1