[英]Populating a program with data.. Visual Studio 2012 & SQL Server Management Studio
I'm trying to populate a database I creaed for a project and got this error... 我正在尝试填充为某个项目创建的数据库并收到此错误...
'System.Windows.Controls.Grid' does not contain a definition for 'ItemsSource' and no extension method 'ItemsSource' accepting a first argument of type 'System.Windows.Controls.Grid' could be found (are you missing a using directive or an assembly reference?) 'System.Windows.Controls.Grid'不包含'ItemsSource'的定义,找不到扩展方法'ItemsSource'接受类型为'System.Windows.Controls.Grid'的第一个参数(是否缺少using指令或装配参考?)
Code: 码:
namespace WalkthroughV2 {
///<summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private List<Student> studentList;
}
private void Refresh()
{
StudentService studentService = new StudentService();
studentList = studentService.GetAll();
grdData.ItemsSource = studentList;
}
}
}
You have to add rows and columns programmatically, as shown in this document . 您必须以编程方式添加行和列,如本文档所示。
A small example: 一个小例子:
Grid myGrid = new Grid();
ColumnDefinition colDef1 = new ColumnDefinition();
RowDefinition rowDef1 = new RowDefinition();
myGrid.ColumnDefinitions.Add(colDef1);
myGrid.RowDefinitions.Add(rowDef1);
// Add the first text cell to the Grid
TextBlock txt1 = new TextBlock();
txt1.Text = "2005 Products Shipped";
txt1.FontSize = 20;
txt1.FontWeight = FontWeights.Bold;
Grid.SetColumnSpan(txt1, 3);
Grid.SetRow(txt1, 0);
A full tutorial can be found in the same document . 完整的教程可以在同一文档中找到。
To solve your problem of why ItemsSource
is not available; 解决您为什么无法使用ItemsSource
的问题; you are using a Grid. 您正在使用网格。 In your WPF-Project, there is also a DataGrid
available. 在您的WPF项目中,还有一个DataGrid
可用。
A tutorial shown in this document explains how to display data in a DataGrid
. 本文档中显示的教程介绍了如何在DataGrid
显示数据。
ObjectQuery<Product> products = dataEntities.Products;
var query =
from product in products
where product.Color == "Red"
orderby product.ListPrice
select new { product.Name, product.Color, CategoryName = product.ProductCategory.Name, product.ListPrice };
dataGrid1.ItemsSource = query.ToList();
And the DataGrid has to be placed inside a Grid: 并且DataGrid必须放置在 Grid内:
<Window x:Class="DataGridSQLExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="450"
Loaded="Window_Loaded">
<Grid>
<DataGrid Name="dataGrid1" />
</Grid>
</Window>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.