简体   繁体   English

如何在C#WPF中向Datagrid添加行

[英]How do I add a row to a Datagrid in C# WPF

I have a problem with adding a row to a Datagrid in C# WPF. 我在C#WPF中向Datagrid添加一行时遇到问题。

I have made a Struct for the data: 我已经为数据制作了结构:

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; }
}

and a method to add the data: 以及添加数据的方法:

  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  });

        }

Can you help me out somehow? 你能以某种方式帮助我吗?

 //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;

or go the MVVM way and use Binding instead of codebehind and setting the itemssource if you dont set AutogenerateColumns to true you have to define your columns with bindings 或采用MVVM的方式,并使用Binding而不是codebehind并设置itemssource,如果您未将AutogenerateColumns设置为true,则必须使用绑定定义列

 <DataGrid ItemsSource="{Binding MySource}" AutogenerateColumns="true"/>
        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;

Where dataView1 = Name of your Datagrid. 其中dataView1 = Datagrid的名称。

如果将DataGrid绑定到数据源,则在DataGrid中添加/操作行的通常方法并不对控件本身起作用(仅显示绑定数据源的内容),而是尝试简单地添加/操作数据源本身的内容,例如,将新的结构项添加到struct集合中,然后DataGrid将立即反映出该新项。

You have to bind the DataGrid to an ObservableCollection. 您必须将DataGrid绑定到ObservableCollection。 Now whenever you add a new item to the ObservableCollection, the DataGrid would get refreshed with latest changes. 现在,每当您向ObservableCollection添加新项目时,DataGrid都会随着最新更改而刷新。

Please take a look at this example: http://www.codeproject.com/Articles/42536/List-vs-ObservableCollection-vs-INotifyPropertyCha 请看以下示例: http : //www.codeproject.com/Articles/42536/List-vs-ObservableCollection-vs-INotifyPropertyCha

Your Data Structure must be implement an enumeratic interface. 您的数据结构必须实现枚举接口。 For this feture you can add it into an ObservableCollection or any Class which implements IEnumerable (like lists but this not provide observablity but adds your data to list) so this provide you to basic WPF elements implementation Also you have to implement the INotifyPropertyChange interface for regarding model binded events operations if it is a necessaty for you. 为此,您可以将其添加到ObservableCollection或实现IEnumerable的任何类中(例如列表,但这不提供可观察性,但将数据添加到列表中),因此这为您提供了基本的WPF元素实现。此外,您还必须实现INotifyPropertyChange接口以实现如果需要,请对模型绑定事件进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM