WPF DataGrid binding difficulties

Posted by Jasmin Pvvlovic on Stack Overflow See other posts from Stack Overflow or by Jasmin Pvvlovic
Published on 2012-06-16T13:28:47Z Indexed on 2012/06/16 15:16 UTC
Read the original article Hit count: 170

Filed under:
|
|

This is the class:

public class TrainingData
{
    public string Training { get; set; }  
}

And this is the rest of the code in MainWindow:

Excel.Workbook xlWorkbook = xlApp.Workbooks.Open("D:/excel.xlsx");
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
List <TrainingData> tData= new List <TrainingData>();

int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
//int k = 0; 

for (int i = 1; i <= rowCount; i++)
{
    tData.Add(new TrainingData() { Training = xlRange.Cells[i, 1].Value2.ToString() });
    //MessageBox.Show(tData[k].Training); 
    //k++;
}

Prikaz.ItemsSource = tData;

DataGrid:

<DataGrid AutoGenerateColumns="False" Height="120" HorizontalAlignment="Left" Margin="12,12,0,0" Name="Prikaz" VerticalAlignment="Top" Width="105" ItemsSource="{Binding}">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Header" />
                </DataGrid.Columns>
            </DataGrid>`

So, Prikaz is my DataGrid. tData is List of TrainingData objects. If I uncomment these three lines I can test if I have imported information from excel file correctly, and yes, that works just fine.

So why am I getting empty DataGrid? It has right number of rows and only one column, that's ok, but there are no data in it. I used this line: Prikaz.ItemsSource = tData; to bind my objects list and DataGrid. Training is declared public so it should be present in DataGrid. What could be causing the problem?

© Stack Overflow or respective owner

Related posts about wpf

Related posts about binding