如何在wpf中显示嵌套的数据网格? [英] How to display nested datagrids in wpf?

查看:111
本文介绍了如何在wpf中显示嵌套的数据网格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hii,





我需要填充多个嵌套的数据网格



就像我有一个第一个数据网格,当第一个数据网格中的一行被点击或在此行下面选中时,将显示第二个数据网格,其中包含第一个数据网格中所选行的相关信息。

所以我已经编码了这个mu ch ..但我的pblm是dat ..我得到的输出是一个空数据网格...

下面我写的是我到目前为止所做的编码。 。

这里我需要的输出就像..第一个网格显示大学列表及其一般细节,如位置,校长,副总裁等。当点击每一行时,它应显示下面的其他网格显示提供的分支,没有座位,没有项目,工作人员等。



我使用可观察的集合和MVVM做到了..



plz帮助我在这里找到错误..



MainWindow.Xaml

< window x:class = WpfApplication1.Views.MainWindowxmlns:x =#unknown>

xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation

xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml

xmlns:ViewModel =clr-namespace:WpfApplication1.ViewModels

Title =MainWindowHeight =350Width =789FontSize =16>

< window.datacontext>

< viewmodel:class1 x:name =class1xmlns:viewmodel =#unknown>





< grid>

< Label Content =COLLEGE INFORMATIONHeight =37Horizo​​ntalAlignment =LeftMargin =228,35,0,0Name =label1VerticalAlignment =TopWidth =268 Foreground =#FFEF0F0FFontFamily =Comic Sans MSFontSize =20/>

< datagrid autogeneratecolumns =Falseheight =200horizo​​ntalalignment =Leftmargin = 40,78,0,0name =dataGrid1verticalalignment =Topwidth =683isenabled =Trueisreadon ly =Trueitemssource ={Binding col}selectionunit =FullRow>

< datagrid.rowdetailstemplate>

< datatemplate>

< datagrid x:name =Innergridautogeneratecolumns =Falseitemssource ={Binding ElementName = BranchList,Path = col}isenabled =Trueisreadonly =Trueselectionunit =FullRowwidth = 500>

< datagrid.columns>

< datagridtextcolumn header =IDbinding ={Binding BID}>

< datagridtextcolumn header =Branchbinding ={Binding BranchName}>

< datagridtextcolumn header =Seatsbinding ={Binding SeatNo}>

< datagridtextcolumn header =Staffbinding ={Binding Staff}>

< datagridtextcolumn header =Mini Projectsbinding ={Binding Minipjct}>

< datagridtextcolumn header =Major Projectbinding ={Binding Majorpjct}>









< datagrid.columns>

< datagridtextcolumn binding ={Binding ID}width =100header =ID>

< datagridtextcolumn binding ={Binding Names}width =110header =College Name>

< datagridtextcolumn binding ={Binding Uni}width = 100header =University>

< datagridtextcolumn binding ={Binding Loc}width =135header =College Location>

< ; datagridtextcolumn binding ={Binding Pri}width =100header =Principal>

< datagridtextcolumn binding ={Binding VP}width =150header =Vise总统>





b / b










< br $>










class1.cs



使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Text;

使用System.Collections.ObjectModel;

使用System.Configuration;

使用System.ComponentModel;

使用System.Data;

使用System.Data.SqlClient;

使用WpfApplication1.Models;

//使用System.Data.EntityModel;

命名空间WpfApplication1.ViewModels

{

公共类Class1:INotifyPropertyChanged

{

模型m1 =新模型();

private ObservableCollection< college> c1 = new ObservableCollection< college>();

public ObservableCollection< college> col

{

get {return c1; }

set

{

c1 = value;

OnPropertyChanged(col);

}

}



public Class1()

{

initializeload();



}

private void initializeload()

{

试试

{

DataTable table = m1.getData();

//大学学院=新学院();

for(int i = 0; i< table.Rows.Count; ++ i)

{

College college = new College();

college.ID =(int)table.Rows [i] [0];

college.Names = table.Rows [i] [1] .ToString() ;

college.Uni = table.Rows [i] [2] .ToString();

college.Loc = table.Rows [i] [3] .ToString();

college.Pri = table.Rows [i] [4] .ToString();

college.VP = table.Rows [i] [5] .ToString();

DataTable table1 = m1.getdata1();

for(int j = 0; j< table1.Rows.Count; ++ j)

{

分支br =新分支();

br.BID =(int)table1.Rows [j] [0];

br.Colid =(int)table1.Rows [j] [1];

br.BranchName = table1.Rows [j] [2] .ToString();

br.SeatNo = table1.Rows [j] [3] .ToString();

br.Staff = table1.Rows [j] [ 4] .ToString();

br.MiniPjct = table1.Rows [j] [5] .ToString();

br.Majorpjct = table1.Rows [j ] [6] .ToString();

college.BranchList.Add(br);

}

// college.BranchList.Add (br);

col.Add(大学);

}

}

catch(例外e)

{

Console.WriteLi ne(e.Message);

}

}



公共事件PropertyChangedEventHandler PropertyChanged;

public void OnPropertyChanged(string propertyname)

{

var handler = PropertyChanged;

if(handler!= null)

handler(this,new PropertyChangedEventArgs(propertyname));

}

}

公共类模型

{

public DataTable getData()

{

SqlConnection con = new SqlConnection(Data Source = NESTAERO-014; Initial Catalog = nest1; User ID = sa; Password = nest123 @!);

DataTable ndt = new DataTable();

con.Open();

SqlDataAdapter da = new SqlDataAdapter(选择ID,名称,大学,位置,校长,大学副校长,con);

da.Fill(ndt);

返回ndt;

}

public DataTable getdata1()

{

SqlConnection con = new SqlConnection(Data Source = NESTAERO-014; Initial Catalog = nest1; User ID = sa; Password = nest123 @!);

DataTable ndt1 = new DataTable();

con。 Open();

SqlDataAdapter da = new SqlDataAdapter(select ID,CID,Branchname,SeatNo,staff,Miniproject,Majorpjct from courses cu cu join college co where cu.ID = co.ID,con );

da.Fill(ndt1);

返回ndt1;

}

}



}



college.cs

使用系统;

使用System.Collections.Generic;

使用System.Linq;

使用System.Text;

使用System.Data;

使用System.Data.SqlClient;

使用System.Configuration;

使用System.ComponentModel;

使用System.Collections.ObjectModel;



名称空间WpfApplication1.Models

{

公共类学院:INotifyPropertyChanged

{

公立学院()

{

}

私人int id;

public int ID

{

get {return id; }

set

{

id = value;

OnPropertyChanged(ID);

}

}

私人字符串名称;

公共字符串名称

{

得到{返回姓名; }

set

{

names = value;

OnPropertyChanged(Names);

}

}

private string uni;

public string Uni

{

得到{return uni; }

set

{

uni = value;

OnPropertyChanged(Uni);

}

}

私人字符串loc;

公共字符串Loc

{

get {return loc; }

set

{

loc = value;

OnPropertyChanged(Loc);

}

}

私人字符串pri;

公共字符串Pri

{

得到{return pri; }

set

{

pri = value;

OnPropertyChanged(Pri);

}

}

私人字符串vp;

公共字符串VP

{

get {return vp; }

set

{

vp = value;

OnPropertyChanged(VP);

}

}

// Model1 m2 = new Model1();

private List< branch> br =新列表< branch>();

public List< branch> BranchList

{

get

{

return br;

}

套装

{

br =价值;



}

}

公共事件PropertyChangedEventHandler PropertyChanged;

//私立大学校友;

protected void OnPropertyChanged(字符串属性名称)

{

var handler = PropertyChanged;

if(handler!= null)

handler(this,new PropertyChangedEventArgs(propertyname) );

}

}



}





Branch.cs



使用系统;

使用系统.Collections.Generic;

使用System.Linq;

使用System.Text;

使用System.Data;

使用System.ComponentModel;

使用System.Collections.ObjectModel;

使用System.Configuration;

使用System .Data.SqlClient;

命名空间WpfApplication1.Models

{

公共类分支:INotifyPropertyChanged

{

public Branch()

{

}

public int bid;

public int BID

{

获得{return bid; }

set

{

bid = value;

OnPropertyChanged(BID);

}

}

public int colid;

public int Colid

{

得到{return colid; }

set

{

colid = value;

OnPropertyChanged(Colid);

}

}

公共字符串brname;

公共字符串BranchName

{

get {return brname; }

set

{

brname = value;

OnPropertyChanged(BranchName);

}

}

公共字符串座位;

公共字符串SeatNo

{

获得{返回座位;}

设置

{

座位=价值;

OnPropertyChanged( SeatNo);

}

}

公共字符串员工;

公共字符串员工

{

get {return staff; }

set

{

staff = value;

OnPropertyChanged(Staff);

}

}

公共字符串mini;

公共字符串MiniPjct

{

get {return mini;}

set {

mini = value;

OnPropertyChanged(Minipjct);

}

}

公共字符串专业;

public string Majorpjct

{

get {return major;}

set {

major = value;

OnPropertyChanged(Majorpjct);

}

}

公共事件PropertyChangedEventHandler PropertyChanged;

public void OnPropertyChanged(string propertyname)

{

var handler = PropertyChanged;

if(handler!= null)

handler(this,new PropertyChangedEventArgs(propertyname));

}



}

}



Mainwindow .xaml.cs



使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Text;

使用System.Windows;

使用System.Windows.Controls;

使用System.Windows.Data;

使用System.Windows.Documents;

使用System.Windows.Input;

使用System.Windows.Media;

使用System.Windows.Media.Imaging;

使用System.Windows.Navigation;

使用System.Windows .Shapes;

使用System.Data;

使用System.ComponentModel;

使用System.Data.SqlClient;

使用System.Collections;



名称空间WpfApplication1.View s

{

///< summary>

/// MainWindow.xaml的交互逻辑

///



公共部分类MainWindow:Window

{

public MainWindow()

{



InitializeComponent();



}



}

}

Hii ,


I need to populate the multiple nested datagrids

like i have a first datagrid when a row in the first datagrid is clicked or selected below this row a second datagrid with related information from the row selected in the first datagrid will be displayed.
So far i have coded this mu ch.. but my pblm is dat.. the out put i am getting is a empty datagrid...
Below i am writing the coding i have done so far...
Here the output i need is like.. first grid shows a list of colleges and their general details like location,principal,VP etc.. when each row is clicked it should show an other grid below that which displays the branches offered, no of seats, no of projects,staff etc..

I did it using observable collection and MVVM..

plz do help me out in finding the error here..

MainWindow.Xaml
<window x:class="WpfApplication1.Views.MainWindow" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ViewModel="clr-namespace:WpfApplication1.ViewModels"
Title="MainWindow" Height="350" Width="789" FontSize="16">
<window.datacontext>
<viewmodel:class1 x:name="class1" xmlns:viewmodel="#unknown">


<grid>
<Label Content="COLLEGE INFORMATION" Height="37" HorizontalAlignment="Left" Margin="228,35,0,0" Name="label1" VerticalAlignment="Top" Width="268" Foreground="#FFEF0F0F" FontFamily="Comic Sans MS" FontSize="20" />
<datagrid autogeneratecolumns="False" height="200" horizontalalignment="Left" margin="40,78,0,0" name="dataGrid1" verticalalignment="Top" width="683" isenabled="True" isreadonly="True" itemssource="{Binding col}" selectionunit="FullRow">
<datagrid.rowdetailstemplate>
<datatemplate>
<datagrid x:name="Innergrid" autogeneratecolumns="False" itemssource="{Binding ElementName=BranchList,Path=col}" isenabled="True" isreadonly="True" selectionunit="FullRow" width="500">
<datagrid.columns>
<datagridtextcolumn header="ID" binding="{Binding BID}">
<datagridtextcolumn header="Branch" binding="{Binding BranchName}">
<datagridtextcolumn header="Seats" binding="{Binding SeatNo}">
<datagridtextcolumn header="Staff" binding="{Binding Staff}">
<datagridtextcolumn header="Mini Projects" binding="{Binding Minipjct}">
<datagridtextcolumn header="Major Project" binding="{Binding Majorpjct}">




<datagrid.columns>
<datagridtextcolumn binding="{Binding ID}" width="100" header="ID">
<datagridtextcolumn binding="{Binding Names}" width="110" header="College Name">
<datagridtextcolumn binding="{Binding Uni}" width="100" header=" University">
<datagridtextcolumn binding="{Binding Loc}" width="135" header=" College Location">
<datagridtextcolumn binding="{Binding Pri}" width="100" header=" Principal">
<datagridtextcolumn binding="{Binding VP}" width="150" header=" Vise President ">














class1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Configuration;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using WpfApplication1.Models;
//using System.Data.EntityModel;
namespace WpfApplication1.ViewModels
{
public class Class1:INotifyPropertyChanged
{
Model m1=new Model();
private ObservableCollection<college> c1 = new ObservableCollection<college>();
public ObservableCollection<college> col
{
get { return c1; }
set
{
c1=value;
OnPropertyChanged("col");
}
}

public Class1()
{
initializeload();

}
private void initializeload()
{
try
{
DataTable table = m1.getData();
// College college = new College();
for (int i = 0; i < table.Rows.Count; ++i)
{
College college = new College();
college.ID = (int)table.Rows[i][0];
college.Names = table.Rows[i][1].ToString();
college.Uni = table.Rows[i][2].ToString();
college.Loc = table.Rows[i][3].ToString();
college.Pri = table.Rows[i][4].ToString();
college.VP = table.Rows[i][5].ToString();
DataTable table1 = m1.getdata1();
for (int j = 0; j < table1.Rows.Count; ++j)
{
Branch br = new Branch();
br.BID = (int)table1.Rows[j][0];
br.Colid = (int)table1.Rows[j][1];
br.BranchName = table1.Rows[j][2].ToString();
br.SeatNo = table1.Rows[j][3].ToString();
br.Staff = table1.Rows[j][4].ToString();
br.MiniPjct = table1.Rows[j][5].ToString();
br.Majorpjct = table1.Rows[j][6].ToString();
college.BranchList.Add(br);
}
// college.BranchList.Add(br);
col.Add(college);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}

public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyname)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyname));
}
}
public class Model
{
public DataTable getData()
{
SqlConnection con = new SqlConnection("Data Source=NESTAERO-014;Initial Catalog=nest1;User ID=sa;Password=nest123@!");
DataTable ndt = new DataTable();
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select ID,Name,University,Location,principal,VP from college", con);
da.Fill(ndt);
return ndt;
}
public DataTable getdata1()
{
SqlConnection con = new SqlConnection("Data Source=NESTAERO-014;Initial Catalog=nest1;User ID=sa;Password=nest123@!");
DataTable ndt1 = new DataTable();
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select ID,CID,Branchname,SeatNo,staff,Miniproject,Majorpjct from courses cu join college co where cu.ID=co.ID", con);
da.Fill(ndt1);
return ndt1;
}
}

}

college.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace WpfApplication1.Models
{
public class College : INotifyPropertyChanged
{
public College()
{
}
private int id;
public int ID
{
get { return id; }
set
{
id = value;
OnPropertyChanged("ID");
}
}
private string names;
public string Names
{
get { return names; }
set
{
names = value;
OnPropertyChanged("Names");
}
}
private string uni;
public string Uni
{
get { return uni; }
set
{
uni = value;
OnPropertyChanged("Uni");
}
}
private string loc;
public string Loc
{
get { return loc; }
set
{
loc = value;
OnPropertyChanged("Loc");
}
}
private string pri;
public string Pri
{
get { return pri; }
set
{
pri = value;
OnPropertyChanged("Pri");
}
}
private string vp;
public string VP
{
get { return vp; }
set
{
vp = value;
OnPropertyChanged("VP");
}
}
//Model1 m2 = new Model1();
private List<branch> br = new List<branch>();
public List<branch> BranchList
{
get
{
return br;
}
set
{
br = value;

}
}
public event PropertyChangedEventHandler PropertyChanged;
// private College coll;
protected void OnPropertyChanged(string propertyname)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyname));
}
}

}


Branch.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Data.SqlClient;
namespace WpfApplication1.Models
{
public class Branch:INotifyPropertyChanged
{
public Branch()
{
}
public int bid;
public int BID
{
get { return bid; }
set
{
bid = value;
OnPropertyChanged("BID");
}
}
public int colid;
public int Colid
{
get { return colid; }
set
{
colid = value;
OnPropertyChanged("Colid");
}
}
public string brname;
public string BranchName
{
get { return brname; }
set
{
brname = value;
OnPropertyChanged("BranchName");
}
}
public string seat;
public string SeatNo
{
get{return seat;}
set
{
seat=value;
OnPropertyChanged("SeatNo");
}
}
public string staff;
public string Staff
{
get { return staff; }
set
{
staff = value;
OnPropertyChanged("Staff");
}
}
public string mini;
public string MiniPjct
{
get{return mini;}
set{
mini=value;
OnPropertyChanged("Minipjct");
}
}
public string major;
public string Majorpjct
{
get{return major;}
set{
major=value;
OnPropertyChanged("Majorpjct");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyname)
{
var handler= PropertyChanged;
if(handler!=null)
handler(this,new PropertyChangedEventArgs(propertyname));
}

}
}

Mainwindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Collections;

namespace WpfApplication1.Views
{
/// <summary>
/// Interaction logic for MainWindow.xaml
///

public partial class MainWindow : Window
{
public MainWindow()
{

InitializeComponent();

}

}
}

推荐答案

我会尝试以简单的方式解释(对于示例部门和人员)



Hi , I''ll try to explain in a simple way (for example departments and persons)

public class Person{
public int Id{get;set;}
public string Name {get;set;}
}

public class Department{
public int Id{get;set;}
public string Name{get;set;}
public ObservableCollection<person> Persons {get;set;}
}

public class MainVM:ViewModelBase
{
  //in properties below do not forget to inject INPC approach
  public Person SelectedPerson {get;set}
  public Department SelectedDepartment {get;set;}
  
  public ObservanleCollection<department> Departments {get;set;}
}</department></person>







你的窗口:




your window:

<window ....="" datacontext="{Binding">
<stackpanel>
...
<datagrid x:name="dgDeps" itemssource="{Binding Departments}" xmlns:x="#unknown">
SelectedItem="{Binding SelectedDepartment, Mode=TwoWay}".... />

<datagrid x:name="dgPersons" itemssource="{Binding SelectedDeoartment.Persons}">
SelectedItem="{Binding SelectedPerson, Mode=TwoWay}"
</datagrid></datagrid></stackpanel></window>





幕后发生了什么事,当您在MainVM中填充Departmnets属性时

dgDeps自动绑定到所有值并显示它,

点击某个部门行后,dgPersons将根据选择的部门刷新绑定。



希望这会有所帮助)



whats going on behind the scene, when you populate Departmnets property in MainVM
dgDeps automatically binds to all values and display it,
after you clicked on some department row, dgPersons will refresh bindings according to seleted department.

Hope this will help )


这篇关于如何在wpf中显示嵌套的数据网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆