Reputation: 3500
I have a problem with adding a row to a Datagrid in C# WPF.
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?
Upvotes: 2
Views: 5518
Reputation: 629
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.
Upvotes: 1
Reputation: 4539
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.
Upvotes: 0
Reputation: 709
You have to bind the DataGrid to an ObservableCollection. Now whenever you add a new item to the ObservableCollection, the DataGrid would get refreshed with latest changes.
Please take a look at this example: http://www.codeproject.com/Articles/42536/List-vs-ObservableCollection-vs-INotifyPropertyCha
Upvotes: 0
Reputation: 22435
//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
<DataGrid ItemsSource="{Binding MySource}" AutogenerateColumns="true"/>
Upvotes: 1
Reputation: 44595
if the DataGrid
is bound to a data source, the usual way to add/manipulate rows in the control is not acting on the control itself ( which is only showing the content of the bound data source ), but instead, try to simply add / manipulate the content of the datasource itself, for example, add a new struct entry to the struct collection and the DataGrid
will reflect such new entry immediately.
Upvotes: 0