0

I am working on a c# project and I am having a really weird problem.

I'm reading in a file and processing the line and storing the results in separate variables within a list array. I am then going through the list array and populating it into the datagrid.

It successfully loading the file and its a creating a new row for line in the file but each line is blank and there are no columns being displayed. Below is a screenshot of how it looks.

View if datagrid after loading file

When I debug it I can view the contents of the variables and all the text is there as expected. Below is the code I am using

private void loadNonVerbose(List<LogCatDetails> logCatDetailsList)
        {
            DataSet ds = new DataSet();
            DataTable table = new DataTable();
            DataColumn logLevel = new DataColumn("Log Level", typeof(string));
            DataColumn tag = new DataColumn("Tag", typeof(string));
            DataColumn processID = new DataColumn("Process ID", typeof(int));
            DataColumn message = new DataColumn("Message", typeof(string));

            table.Columns.Add(logLevel);
            table.Columns.Add(tag);
            table.Columns.Add(processID);
            table.Columns.Add(message);

            ds.Tables.Add(table);

            int i = 0;
            foreach (LogCatDetails logCatDetails in logCatDetailsList)
            {
                DataRow row = table.NewRow();
                row[logLevel] = logCatDetails.LogLevel.ToString();
                row[tag] = logCatDetails.Tag;
                row[processID] = logCatDetails.ProcessID;
                row[message] = logCatDetails.Message;
                table.Rows.Add(row);
                if (i == 10)
                {
                    break;
                }
                i++;
            }

            logCatDataGrid.ItemsSource = ds.Tables[0].DefaultView;
        }

Thanks for any help you can provide

UPDATE Below is how the datagrid is defined in the XML

<DataGrid ColumnWidth="*" AutoGenerateColumns="False" Margin="12,51,12,12" Name="logCatDataGrid" />
5
  • Why don't you create a proper ViewModel (or even Model) to store your data? that thing row[loglevel], row[tag], row[etc] is horrible. Also, I see you're basically wrapping that LogCatDetails stuff in the horrible thing. Why don't you bind the DataGrid directly to the LogCatDetailsList?
    – Fede
    Commented Feb 17, 2013 at 23:45
  • Boardy, Please show your XAML.
    – Khan
    Commented Feb 17, 2013 at 23:55
  • @JefferyKhan I have updated the question to include the xaml for the datagrid.
    – Boardy
    Commented Feb 17, 2013 at 23:57
  • @HighCore. I have tried setting the itemssource to the list but it does the same thing
    – Boardy
    Commented Feb 17, 2013 at 23:58
  • That grid has no columns. That's why you're seeing nothing. Still, my point stands. Remove all that DataRow stuff and bind the DataGrid directly to the List<LogCatDetails>.
    – Fede
    Commented Feb 17, 2013 at 23:59

1 Answer 1

1

Set the AutoGenerateColumns property of the DataGrid to True:

<DataGrid ColumnWidth="*" AutoGenerateColumns="True" Margin="12,51,12,12" Name="logCatDataGrid" />

HighCore is correct, and I recommend you follow his guidance. The DataSet/DataTables are unneeded. You could simplify this by simply modifying the code-behind to the following:

private void loadNonVerbose(List<LogCatDetails> logCatDetailsList)
{
    logCatDataGrid.ItemsSource = logCatDetailsList;
}
2
  • 1
    Even if you do this, remove the DataTable stuff. Its not necessary at all and adds a lot of unneded code.
    – Fede
    Commented Feb 18, 2013 at 0:01
  • Thanks for your help both of you, I have taken your advice on binding the list directly to the data grid instead of building everything manually. Forgot you can do that. Thanks again
    – Boardy
    Commented Feb 18, 2013 at 0:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.