如何在C#WPF中向数据网格添加行
在C#WPF中向数据网格添加行时,我遇到了一个问题 我已经为数据创建了一个结构:如何在C#WPF中向数据网格添加行,c#,wpf,datagrid,C#,Wpf,Datagrid,在C#WPF中向数据网格添加行时,我遇到了一个问题 我已经为数据创建了一个结构: public struct MyData { public int id { set; get; } public string title { set; get; } public int jobint { set; get; } public DateTime lastrun { set; get; } public DateTime nextrun { set; get
public struct MyData
{
public int id { set; get; }
public string title { set; get; }
public int jobint { set; get; }
public DateTime lastrun { set; get; }
public DateTime nextrun { set; get; }
}
private void Add_Data_Grid_Row(object sender, RoutedEventArgs e)
{
Button button = sender as Button;
DockPanel panel = button.Parent as DockPanel;
DataGrid usedDataGrid = panel.Children.OfType<DataGrid>().FirstOrDefault();
usedDataGrid.Items.Add(new MyData { id = 11123, title = "King", jobint = 1993123, lastrun = DateTime.Today, nextrun = DateTime.Today });
}
以及添加数据的方法:
public struct MyData
{
public int id { set; get; }
public string title { set; get; }
public int jobint { set; get; }
public DateTime lastrun { set; get; }
public DateTime nextrun { set; get; }
}
private void Add_Data_Grid_Row(object sender, RoutedEventArgs e)
{
Button button = sender as Button;
DockPanel panel = button.Parent as DockPanel;
DataGrid usedDataGrid = panel.Children.OfType<DataGrid>().FirstOrDefault();
usedDataGrid.Items.Add(new MyData { id = 11123, title = "King", jobint = 1993123, lastrun = DateTime.Today, nextrun = DateTime.Today });
}
private void添加数据网格行(对象发送方,路由目标)
{
按钮按钮=发送器为按钮;
DockPanel panel=按钮。父级为DockPanel;
DataGrid usedDataGrid=panel.Children.OfType().FirstOrDefault();
usedDataGrid.Items.Add(新的MyData{id=11123,title=“King”,jobit=1993123,lastrun=DateTime.Today,nextrun=DateTime.Today});
}
你能帮我个忙吗?如果
DataGrid
绑定到数据源,通常在控件中添加/操作行的方法不是对控件本身进行操作(它只显示绑定数据源的内容),而是尝试简单地添加/操作数据源本身的内容,例如,向结构集合中添加一个新的结构条目,DataGrid
将立即反映该新条目。//使用ObservableCollection
//Use ObservableCollection
public ObservableCollection<MyData> MySource {get;set;}
//initialize once, eg. ctor
this.MySource = new ObservableCollection<MyData>();
//add items
this.MySource.Add(new MyData { id = 11123, title = "King", jobint = 1993123, lastrun = DateTime.Today, nextrun = DateTime.Today});
//set the itemssource
usedDataGrid.ItemsSource = this.MySource;
公共ObservableCollection MySource{get;set;}
//初始化一次,例如
this.MySource=新的ObservableCollection();
//添加项目
添加(新的MyData{id=11123,title=“King”,jobit=1993123,lastrun=DateTime.Today,nextrun=DateTime.Today});
//设置项目资源
usedDataGrid.ItemsSource=this.MySource;
或者采用MVVM方式,使用绑定而不是代码隐藏并设置itemssource
如果未将AutogenerateColumns设置为true,则必须使用绑定定义列
<DataGrid ItemsSource="{Binding MySource}" AutogenerateColumns="true"/>
您必须将DataGrid绑定到ObservableCollection。现在,每当您向ObservableCollection添加一个新项时,DataGrid都会使用最新的更改进行刷新
请看一看这个例子:您的数据结构必须实现枚举接口。对于这个feture,您可以将它添加到ObservableCollection或实现IEnumerable的任何类中(类似于列表,但它不提供可观察性,而是将您的数据添加到列表中),这样就为您提供了基本的WPF元素实现 此外,如果有必要,还必须实现INotifyPropertyChange接口,用于处理模型绑定事件操作。
DataTable dt=newdatatable();
DataTable dt = new DataTable();
DataColumn column;
DataRow row;
DataView view;
row = new DataRow();
dt.Rows.Add(row);
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "id";
dt.Columns.Add(column);
column = new DataColumn();
column.DataType = Type.GetType("System.String");
column.ColumnName = "item";
dt.Columns.Add(column);
for (int i = 0; i < 10; i++)
{
row = dt.NewRow();
row["id"] = i;
row["item"] = "item " + i.ToString();
dt.Rows.Add(row);
}
view = new DataView(dt);
dataView1.ItemsSource = view;
数据列;
数据行;
数据视图;
行=新数据行();
dt.行。添加(行);
column=newdatacolumn();
column.DataType=System.Type.GetType(“System.Int32”);
column.ColumnName=“id”;
dt.Columns.Add(列);
column=newdatacolumn();
column.DataType=Type.GetType(“System.String”);
column.ColumnName=“项”;
dt.Columns.Add(列);
对于(int i=0;i<10;i++)
{
row=dt.NewRow();
行[“id”]=i;
行[“项”]=“项”+i.ToString();
dt.行。添加(行);
}
视图=新数据视图(dt);
dataView1.ItemsSource=视图;
其中dataView1=数据网格的名称