您好 我正在使用datagrid视图。数据被添加到表格中,从监视窗口中,我可以看到表格中包含1行但数据网格中没有显示任何内容。
以下是代码:
DataTable PopulateDataGrids(DataGrid grid, List<ReterivedEmailData> data)
{
DataTable table = new DataTable();
table.Columns.Add("Sr No");
table.Columns.Add("Company Name");
table.Columns.Add("Email");
int count = 0;
DataRow row;
foreach (ReterivedEmailData item in data)
{
count++;
row = table.NewRow();
row["Sr No"] = count.ToString();
row["Company Name"] = item.Name;
row["Email"] = item.Email;
table.Rows.Add(row);
}
return table;
}
这是方法调用:
dataGridBoatCompanyList.DataContext = PopulateDataGrids(dataGridBoatCompanyList, boat);
XAML标记:
<DataGrid AutoGenerateColumns="True" Height="322" HorizontalAlignment="Left" Margin="6,6,0,0" Name="dataGridBoatCompanyList" VerticalAlignment="Top" Width="500" />
答案 0 :(得分:2)
尝试将ItemsSource="{Binding}"
添加到您的网格
<DataGrid DataContext="{Binding}" AutoGenerateColumns="True" Height="322" HorizontalAlignment="Left" Margin="6,6,0,0" Name="dataGridBoatCompanyList" VerticalAlignment="Top" Width="500" />
另外,您在方法PopulateDataGrids()
中没有使用第一个参数。如果您要做的就是填充网格,那么您可以跳过该方法并使用
dataGridBoadCompanyList.ItemsSource= new ObservableCollection<ReterivedEmailData>(boat);
编辑:
如果我试试这个
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
dataGridBoatCompanyList.ItemsSource =
new ObservableCollection<ReterivedEmailData>(new[]
{
new ReterivedEmailData
{Email = "dfsd", Name = "fadsfds"}
});
}
并且
<DataGrid ItemsSource="{Binding}" AutoGenerateColumns="True" Height="322" HorizontalAlignment="Left" Margin="6,6,0,0" Name="dataGridBoatCompanyList" VerticalAlignment="Top" Width="500" />
我得到一个像这样的填充列表
答案 1 :(得分:1)
很难说没有看到你的DataGrid的XAML定义,但也许你的意思是
dataGridBoatCompanyList.ItemsSource = PopulateDataGrids(dataGridBoatCompanyList, boat);