[英]Adding data to a datagrid
I am working on ac# project and I am having a really weird problem. 我正在从事ac#项目,但遇到了一个非常奇怪的问题。
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.
然后,我遍历列表数组并将其填充到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.
以下是其外观的屏幕截图。
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 更新下面是如何在XML中定义数据网格
<DataGrid ColumnWidth="*" AutoGenerateColumns="False" Margin="12,51,12,12" Name="logCatDataGrid" />
Set the AutoGenerateColumns
property of the DataGrid
to True
: 将
DataGrid
的AutoGenerateColumns
属性设置为True
:
<DataGrid ColumnWidth="*" AutoGenerateColumns="True" Margin="12,51,12,12" Name="logCatDataGrid" />
HighCore is correct, and I recommend you follow his guidance. HighCore是正确的,我建议您遵循他的指导。 The
DataSet/DataTable
s are unneeded. 不需要
DataSet/DataTable
。 You could simplify this by simply modifying the code-behind
to the following: 您可以通过简单地将以下
code-behind
修改为以下内容来简化此操作:
private void loadNonVerbose(List<LogCatDetails> logCatDetailsList)
{
logCatDataGrid.ItemsSource = logCatDetailsList;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.