如何用我的列表(t)填充我的wpf数据网格
我有两个名为Breed和PetType的类,它们也是我的sql server数据库中的表 品种等级如下:如何用我的列表(t)填充我的wpf数据网格,wpf,wpfdatagrid,Wpf,Wpfdatagrid,我有两个名为Breed和PetType的类,它们也是我的sql server数据库中的表 品种等级如下: public int Id { get; set; } public string BreedName { get; set; } public string Description { get; set; } public int SortOrder { get; set; } public bool StatusId { get; set; } public int CreatedBy
public int Id { get; set; }
public string BreedName { get; set; }
public string Description { get; set; }
public int SortOrder { get; set; }
public bool StatusId { get; set; }
public int CreatedBy { get; set; }
public string CreatedByName { get; set; }
public int PetTypeId { get; set; } //Inner or left joined from PetTypes Table(stored procedure)
public string PetTypeName { get; set; }
public int Id { get; set; }
public string PetTypeName { get; set; }
public string Description { get; set; }
public int SortOrder { get; set; }
public bool StatusId { get; set; }
public int CreatedBy { get; set; }
public string CreatedByName { get; set; }
PetType类是:
public int Id { get; set; }
public string BreedName { get; set; }
public string Description { get; set; }
public int SortOrder { get; set; }
public bool StatusId { get; set; }
public int CreatedBy { get; set; }
public string CreatedByName { get; set; }
public int PetTypeId { get; set; } //Inner or left joined from PetTypes Table(stored procedure)
public string PetTypeName { get; set; }
public int Id { get; set; }
public string PetTypeName { get; set; }
public string Description { get; set; }
public int SortOrder { get; set; }
public bool StatusId { get; set; }
public int CreatedBy { get; set; }
public string CreatedByName { get; set; }
然后在我的DataAccessLayer项目中
public List<Breed> RetrieveMany(Breed parameters)
{
var items = new List<Breed>();
var command = new SqlCommand {CommandText = "SP_RetrieveBreeds", CommandType = CommandType.StoredProcedure};
command.Parameters.AddWithValue("@Id", parameters.Id).Direction = ParameterDirection.Input;
command.Parameters.AddWithValue("@Name", parameters.BreedName).Direction = ParameterDirection.Input;
command.Parameters.AddWithValue("@PetTypeId", parameters.PetTypeId).Direction = ParameterDirection.Input;
DataTable dt = SqlHelper.GetData(command);
foreach (DataRow row in dt.Rows)
{
var item = new Breed();
item.Id = Convert.ToInt32(row["Id"].ToString());
item.PetTypeId = Convert.ToInt32(row["PetTypeId"].ToString());
item.PetTypeName = (row["PetTypeName"]).ToString();
item.BreedName = row["BreedName"].ToString();
item.Description = row["Description"].ToString();
item.SortOrder = Convert.ToInt32(row["SortOrder"].ToString());
item.CreatedBy = Convert.ToInt32(row["CreatedById"].ToString());
item.CreatedByName = row["CreatedByName"].ToString();
items.Add(item);
}
return items;
}
public List RetrieveMany(品种参数)
{
var items=新列表();
var command=new-SqlCommand{CommandText=“SP_retrievebrids”,CommandType=CommandType.StoredProcedure};
command.Parameters.AddWithValue(“@Id”,Parameters.Id).Direction=ParameterDirection.Input;
command.Parameters.AddWithValue(“@Name”,Parameters.BreedName).Direction=ParameterDirection.Input;
command.Parameters.AddWithValue(“@PetTypeId”,Parameters.PetTypeId).Direction=ParameterDirection.Input;
DataTable dt=SqlHelper.GetData(命令);
foreach(数据行中的数据行)
{
var item=新品种();
item.Id=Convert.ToInt32(第[“Id”].ToString()行);
item.PetTypeId=Convert.ToInt32(第[“PetTypeId”].ToString()行);
item.PetTypeName=(行[“PetTypeName”]).ToString();
item.BreedName=行[“BreedName”].ToString();
item.Description=行[“Description”].ToString();
item.SortOrder=Convert.ToInt32(第[“SortOrder”].ToString()行);
item.CreatedBy=Convert.ToInt32(行[“CreatedById”].ToString());
item.CreatedByName=行[“CreatedByName”].ToString();
项目。添加(项目);
}
退货项目;
}
然后我将itemMgr声明为一个对象。将这些项目放入对象列表中
BreedDataAccess itemMgr = new BreedDataAccess();
public void LoadGridview()
{
Breed BreedParam = RetrieveFilter(searchCrit); //gets all filter parameters for individual searching
//this.itemMgr.EntityList = itemMgr.RetrieveMany(itemMgr.Parameters);
List <Breed> listOfBreeds = new List<Breed>();
listOfBreeds = DataAcessLayer.RetrieveMany(BreedParam);
this.dgvBreeds.AutoGenerateColumns = false;
this.dgvBreeds.ItemSource = this.itemMgr.EntityList;
}
BreedDataAccess itemMgr=new BreedDataAccess();
公共void LoadGridview()
{
Breed BreedParam=RetrieveFilter(searchCrit);//获取单个搜索的所有筛选器参数
//this.itemMgr.EntityList=itemMgr.RetrieveMany(itemMgr.Parameters);
List LISTOFBREDS=新列表();
ListofBrads=DataAcessLayer.RetrieveMany(BreedParam);
this.dgvBreeds.AutoGenerateColumns=false;
this.dgvBreeds.ItemSource=this.itemMgr.EntityList;
}
这就是我为提高分数所做的努力:
<Window x:Class="NeoClinic.WPFTryOut.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid x:Name="dgvBreeds" HorizontalAlignment="Left" VerticalAlignment="Top" Height="319" Width="517"/>
</Grid>
我在windows窗体DataGridView中尝试了该代码,效果很好。但我不知道如何在WPF中执行此操作。我看到您正在处理事件,可能是在窗口/控件的代码后面。这在UI元素和背后的逻辑之间创建了一个强耦合,这反过来使得测试更加困难 我会考虑在视图模型中使用和做这类事情。使用这种方法可以减少UI(视图)和逻辑(视图模型)之间的耦合 我编写了一个非常简单的应用程序(针对.NET4.5)来演示如何将集合绑定到数据网格以及如何删除所选行 以下是XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Width="525"
Height="350">
<Grid>
<StackPanel>
<DataGrid ItemsSource="{Binding People}" SelectedItem="{Binding SelectedItem}" />
<Button Command="{Binding DeleteCommand}">Delete Selected Row</Button>
</StackPanel>
</Grid>
</Window>
Person.cs
using System;
namespace WpfApplication1
{
public class Person
{
public string Forename { get; set; }
public string Surname { get; set; }
}
}
DelegateCommand.cs
using System;
using System.Windows;
using System.Windows.Input;
namespace WpfApplication1
{
public class DelegateCommand : ICommand
{
private readonly Predicate<object> _canExecute;
private readonly Action<object> _execute;
public event EventHandler CanExecuteChanged;
public DelegateCommand(Action<object> execute)
: this(execute, null)
{
}
public DelegateCommand(Action<object> execute,
Predicate<object> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
if (_canExecute == null)
{
return true;
}
return _canExecute(parameter);
}
public void Execute(object parameter)
{
_execute(parameter);
}
public void RaiseCanExecuteChanged()
{
if (CanExecuteChanged != null)
{
CanExecuteChanged(this, EventArgs.Empty);
}
}
}
}
使用系统;
使用System.Windows;
使用System.Windows.Input;
命名空间WpfApplication1
{
公共类DelegateCommand:ICommand
{
私有只读谓词_canExecute;
私有只读操作\u执行;
公共事件处理程序CanExecuteChanged;
公共DelegateCommand(操作执行)
:此(执行,空)
{
}
公共DelegateCommand(操作执行,
谓词(可执行)
{
_执行=执行;
_canExecute=canExecute;
}
公共布尔CanExecute(对象参数)
{
如果(_canExecute==null)
{
返回true;
}
返回_canExecute(参数);
}
public void Execute(对象参数)
{
_执行(参数);
}
public void raisecancecutechanged()
{
如果(CanExecuteChanged!=null)
{
CanExecuteChanged(此为EventArgs.Empty);
}
}
}
}
MainViewModel.cs
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;
namespace WpfApplication1
{
internal class MainViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public MainViewModel()
{
People = new ObservableCollection<Person>();
DeleteCommand = new DelegateCommand(x => DeleteSelectedItem(null));
People.Add(new Person { Forename = "Bob", Surname = "Smith" });
People.Add(new Person { Forename = "Alice", Surname = "Jones" });
}
private void DeleteSelectedItem(object obj)
{
People.Remove(SelectedItem);
SelectedItem = null;
}
public ICommand DeleteCommand { get; set; }
public ObservableCollection<Person> People { get; set; }
private Person selectedItem;
public Person SelectedItem
{
get { return selectedItem; }
set
{
if (selectedItem == value)
return;
selectedItem = value;
OnPropertyChanged();
}
}
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
使用System.Collections.ObjectModel;
使用系统组件模型;
使用System.Runtime.CompilerServices;
使用System.Windows.Input;
命名空间WpfApplication1
{
内部类MainViewModel:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
公共主视图模型()
{
人员=新的可观察集合();
DeleteCommand=newdelegateCommand(x=>DeleteSelectedItem(null));
添加(新人物{Forename=“Bob”,姓氏=“Smith”});
添加(新人物{Forename=“Alice”,姓氏=“Jones”});
}
私有void DeleteSelectedItem(对象对象)
{
人员。删除(选择编辑项);
SelectedItem=null;
}
公共ICommand DeleteCommand{get;set;}
公共可观察集合人员{get;set;}
私人选择编辑;
公众人士选择
{
获取{return selectedItem;}
设置
{
如果(selectedItem==值)
返回;
选择editem=值;
OnPropertyChanged();
}
}
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(handler!=null)handler(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
第一步,您可以试试这个.dgvBreeds.ItemSource=listofBrads,看看是否填充了网格?并将AutoGenerateColumns设置为true。在获取目录中的数据时,为什么要将itemsource设置为this.itemMgr.EntityList?此外,此链接应该可以帮助您。。。尝试设置AutoGenerateColumns=true代码>(当前设置为