送分了!WPF数据绑定的一个菜鸟问题!

dgw198906 2011-04-12 09:31:39
定义一个实体类
public class Counter
{public int Id { get; set; }
public string No { get; set; }
};

然后定义一个泛型集合类IEnumerable<Counter> Get,用这个实体类为强类型,将一个数据库的数据读入到这个泛型集合中,返回值是Counter实体类。
那么我想请教的问题是怎么绑定这个泛型集合去datagrid?极度菜鸟的问题,各位大牛帮帮忙!送分了
...全文
91 点赞 收藏 10
写回复
10 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
dgw198906 2011-04-13
IEnumerable<Counter> result=DbOperation.ExecuteAstSqlStringAccessor<Counter>(command, ticketHistoryMapper);

数据写入代码
回复
暖枫无敌 2011-04-12
你返回的不就是实体类的集合嘛,什么两层数据,不懂
回复
dgw198906 2011-04-12
两层数据的,绑进去空值了。
回复
暖枫无敌 2011-04-12

datagrid.ItemsSource = GetTicketHistoryByOrder(2).ToList();
回复
机器人 2011-04-12
就是楼上的做法啊。 IEnumerable<Counter> 直接付给 DataGrid.ItemSource 有什么问题?
回复
dgw198906 2011-04-12


public static IEnumerable<Counter> GetTicketHistoryByOrder(string tellerId)
{
var ticketHistoryMapper = MapBuilder<Counter>.BuildAllProperties();
var command = DbOperation.GetDbCommand(string.Format("select * from FES_QUEUE_TicketHistory where TellerID='{0}'", tellerId));
var results = DbOperation.ExecuteAstSqlStringAccessor<Counter>(command, ticketHistoryMapper);
}


回复
dgw198906 2011-04-12
还是没明白我题目的用意。数据在实体类中,而实体类又在泛型集合中,等包裹了两层。我想问的是如何直接能绑定到datagrid中。
附数据从数据库中导入的方法:(注:用的enterprise library的数据连接)

public static IEnumerable<Counter> GetTicketHistoryByOrder(string tellerId)
{
var ticketHistoryMapper = MapBuilder<Counter>.BuildAllProperties();
var command = DbOperation.GetDbCommand(string.Format("select * from FES_QUEUE_TicketHistory where TellerID='{0}'", tellerId));
var results = DbOperation.ExecuteAstSqlStringAccessor<Counter>(command, ticketHistoryMapper);
}
回复
暖枫无敌 2011-04-12

MainPage.xaml内容:
<UserControl x:Class="SL1015.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" Loaded="UserControl_Loaded">

<sdk:DataGrid Name="dataGrid1" AutoGenerateColumns="False" GridLinesVisibility="All">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Header="UserID" Binding="{Binding UserID}" IsReadOnly="True"></sdk:DataGridTextColumn>
<sdk:DataGridTextColumn Header="UserName" Binding="{Binding UserName}" IsReadOnly="True"></sdk:DataGridTextColumn>
<sdk:DataGridTemplateColumn Header="管理">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="删除"></TextBlock>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
</UserControl>

MainPage.xaml.cs的内容如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SL1015
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();

dataGrid1.ItemsSource = GetDataList();
}

private List<Student> GetDataList()
{
List<Student> list = new List<Student>();
list.Add(new Student(1, "张三"));
list.Add(new Student(2, "李四"));
list.Add(new Student(3, "王二"));
list.Add(new Student(4, "麻子"));

return list;
}

}
}

实体类Student.cs内容如下:

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SL1015
{
public class Student
{
public Student(int id, string name)
{
this.UserID = id;
this.UserName = name;
}
public int UserID { get; set; }
public string UserName { get; set; }
}
}
回复
暖枫无敌 2011-04-12
错了,是ItemsSource 属性
回复
暖枫无敌 2011-04-12
DataContext属性即可
datagrid.DataContext = 方法返回的实体类集合;
回复
相关推荐
发帖
.NET技术前瞻
创建于2007-09-28

1.3w+

社区成员

.NET技术 .NET技术前瞻
申请成为版主
帖子事件
创建了帖子
2011-04-12 09:31
社区公告
暂无公告