Problem with adding a new row in RadGridView MVVM

2 Answers 20 Views
GridView
Guarana91
Top achievements
Rank 1
Iron
Guarana91 asked on 10 Mar 2022, 01:06 PM | edited on 10 Mar 2022, 01:08 PM

Hello everyone,

I read the most topics with a similiar problem but nothing had worked for me.

I use WPF with MVVM and have a RadGridView. The binding from my Xaml code is with a button and a CommandParameter that passes the RadGridView to my method.

The Code looks like this

private void LoadData(object obj)
{
    RadGridView rad = obj as RadGridView;
    
    GridViewDataColumn col = new GridViewDataColumn();
    col.DataMemberBinding = new System.Windows.Data.Binding("Name");
    col.Header = "Name";

    rad.Columns.Add(col);

    //How to add now a new row to this?
}

How can I add now a new Raw with code? Something like rad.Rows.Add() does not exist.

Hope anybody can help me :-)

2 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 11 Mar 2022, 03:11 PM

Hello Janko,

RadGridView doesn't have a Rows collection where you can add GridView row elements. Instead, the data API of the control is its ItemsSource collection. So, basically, any operations with the data like, add, remove, edit can be done using the collection assigned to the ItemsSource. For example, if you populate the GridView control with an object called MyRowInfo that has a property "Name" (based on the column definition from your code snippet), then you can use something like the following:

private void LoadData(object obj)
{
	RadGridView rad = obj as RadGridView;
	
	var itemsSource = new ObservableCollection<MyRowInfo>();
	rad.ItemsSource = itemsSource;
	GridViewDataColumn col = new GridViewDataColumn();
	col.DataMemberBinding = new System.Windows.Data.Binding("Name");
	col.Header = "Name";

	rad.Columns.Add(col);

	itemsSource.Add(new MyRowInfo() { Name = "New item" });
}

//-----------------
public class MyRowInfo
{
	public string Name { get; set;}
}

I hope that helps.

Regards,
Martin Ivanov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

0
Guarana91
Top achievements
Rank 1
Iron
answered on 14 Mar 2022, 10:49 AM

Hello Martin,

thanks. I will try this solution :-)

Tags
GridView
Asked by
Guarana91
Top achievements
Rank 1
Iron
Answers by
Martin Ivanov
Telerik team
Guarana91
Top achievements
Rank 1
Iron
Share this question
or