I'm new to WPF, but as life goes, I have to bind to a ObservableCollection of Business Objects to a RadGridView in Code Behind, because the number of GridViewDataColumns is dynamic. Here I have an example (in my real application, my business class contains fields for sales data and the radgridview should be able to show a different number of sales periods).
Example:
=======
Business Object:
----------------------
public class Person : INotifyPropertyChanged
{
private string _Firstname;
private string _Lastname;
public evebt PropertyChangedEventHandler PropertyChanged;
public string Firstname
{
get { return _Firstname; }
set
{
_Firstname = value;
OnPropertyChanged("_Firstname");
}
}
public string Lastname
{
get { return _Lastname; }
set
{
_Lastname = value;
OnPropertyChanged("_Lastname");
}
}
protected void OnPropertyChanged(string strPropName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(strPropName));
}
}
}
ObservableCollection:
-----------------------------
public class Persons : ObservableCollection<Person>
{
public Persons() : base()
{
}
}
ExamplePage:
-------------------
public partial class ExamplePage:Page
{
Persons _p;
public ExamplePage()
{
InitializeComponent();
//Pseudo-Code: the ObservableCollection ist filled up in the DAL
_p = DAL.GetPersons();
//How or what is the way of binding the ObservableCollection to the radgridview1??
//Possibility 1:
radgridview1.DataContext = _p;
//Possibility 2:
radgridview1.ItemsSource= _p;
//Possibility ...: anything else?
GridViewDataColumn colFirstname = new GridViewDataColumn();
colFirstname.Header = "Firstname";
colFirstname.UniqueName = "cFirstname";
//I think, that here is the right place for column-bindung, but how?
radgridview1.Columns.Add(colFirstname);
GridViewDataColumn colFirstname = new GridViewDataColumn();
colLastname.Header = "Lastname";
colLastname.UniqueName = "cLastname";
//I think, that here is the right place for column-bindung, but how?
radgridview1.Columns.Add(colLastname);
}
}
Please help me!
Kind regards
Michael
9 Answers, 1 is accepted
You should use the Possibility2 when it comes to binding the source collection of the GridView. Please check this help article as well.
Once you create the column you can set its binding like so:
GridViewDataColumn column =
new
GridViewDataColumn();
column.DataMemberBinding =
new
System.Windows.Data.Binding(
"LastName"
);
I hope this is helpful.
Regards,
Didie
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
I just tested your suggestion (and I think I tried it before by myself), but I get the following error (see attached screenshot; sorry: german version of vs)???
Bye
Michael
The exception says that something is null.
I have tested using the following code that is working fine:
<
telerik:RadGridView
Grid.Row
=
"0"
Name
=
"clubsGrid"
AutoGenerateColumns
=
"False"
Margin
=
"5"
>
</
telerik:RadGridView
>
public
MainWindow()
{
InitializeComponent();
MyViewModel model =
new
MyViewModel();
clubsGrid.ItemsSource = model.Clubs;
GridViewDataColumn column =
new
GridViewDataColumn();
column.DataMemberBinding =
new
System.Windows.Data.Binding(
"Name"
);
clubsGrid.Columns.Add(column);
}
Please check if your objects are all available and all the bindings are spelled the right way.
Kind regards,
Didie
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
I cannot see any spelling error. Also when I check the ObservableCollection-Object right before setting the radgridview.itemssource, I can see, that there is one object in that collection (my test data record).
Here is my business object "Absatz":
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.ComponentModel;
namespace
FutureView.BusinessObjects
{
public
class
Absatz :INotifyPropertyChanged
{
private
string
_strArtint;
private
string
_strArtin4;
private
string
_strArtin4Text;
private
string
_strArtin3;
private
string
_strArtin3Text;
private
string
_strArtin2;
private
string
_strArtin2Text;
private
string
_strArtext;
private
string
_strArtbzl;
private
string
_strMdevsx;
private
decimal
_decVjIstKum;
//wird errechnet
private
decimal
_decLjIstKum;
//wird errechnet
private
decimal
_decVjIstMon1;
private
decimal
_decLjIstMon1;
private
decimal
_decPlanMon1;
private
decimal
_decVjIstMon2;
private
decimal
_decLjIstMon2;
private
decimal
_decPlanMon2;
private
decimal
_decVjIstMon3;
private
decimal
_decLjIstMon3;
private
decimal
_decPlanMon3;
private
decimal
_decVjIstMon4;
private
decimal
_decLjIstMon4;
private
decimal
_decPlanMon4;
private
decimal
_decVjIstMon5;
private
decimal
_decLjIstMon5;
private
decimal
_decPlanMon5;
private
decimal
_decVjIstMon6;
private
decimal
_decLjIstMon6;
private
decimal
_decPlanMon6;
private
decimal
_decVjIstMon7;
private
decimal
_decLjIstMon7;
private
decimal
_decPlanMon7;
private
decimal
_decVjIstMon8;
private
decimal
_decLjIstMon8;
private
decimal
_decPlanMon8;
private
decimal
_decVjIstMon9;
private
decimal
_decLjIstMon9;
private
decimal
_decPlanMon9;
private
decimal
_decVjIstMon10;
private
decimal
_decLjIstMon10;
private
decimal
_decPlanMon10;
private
decimal
_decVjIstMon11;
private
decimal
_decLjIstMon11;
private
decimal
_decPlanMon11;
private
decimal
_decVjIstMon12;
private
decimal
_decLjIstMon12;
private
decimal
_decPlanMon12;
private
decimal
_decVjIstMon13;
private
decimal
_decLjIstMon13;
private
decimal
_decPlanMon13;
private
decimal
_decVjIstMon14;
private
decimal
_decLjIstMon14;
private
decimal
_decPlanMon14;
private
decimal
_decVjIstMon15;
private
decimal
_decLjIstMon15;
private
decimal
_decPlanMon15;
public
event
PropertyChangedEventHandler PropertyChanged;
public
string
strArtint
{
get
{
return
_strArtint;
}
set
{
_strArtint = value;
}
}
public
string
strArtin4
{
get
{
return
_strArtin4;
}
set
{
_strArtin4 = value;
}
}
public
string
strArtin4Text
{
get
{
return
_strArtin4Text;
}
set
{
_strArtin4Text = value;
}
}
public
string
strArtin3
{
get
{
return
_strArtin3;
}
set
{
_strArtin3 = value;
}
}
public
string
strArtin3Text
{
get
{
return
_strArtin3Text;
}
set
{
_strArtin3Text = value;
}
}
public
string
strArtin2
{
get
{
return
_strArtin2;
}
set
{
_strArtin2 = value;
}
}
public
string
strArtin2Text
{
get
{
return
_strArtin2Text;
}
set
{
_strArtin2Text = value;
}
}
public
string
strArtext
{
get
{
return
_strArtext;
}
set
{
_strArtext = value;
}
}
public
string
strArtbzl
{
get
{
return
_strArtbzl;
}
set
{
_strArtbzl = value;
}
}
public
string
strMdevsx
{
get
{
return
_strMdevsx;
}
set
{
_strMdevsx = value;
}
}
public
decimal
decVjIstKum
{
get
{
return
_decVjIstKum;
}
set
{
_decVjIstKum = value;
}
}
public
decimal
decLjIstKum
{
get
{
return
_decLjIstKum;
}
set
{
_decLjIstKum = value;
}
}
public
decimal
decVjIstMon1
{
get
{
return
_decVjIstMon1;
}
set
{
_decVjIstMon1 = value;
OnPropertyChanged(
"_decVjIstMon1"
);
}
}
public
decimal
decLjIstMon1
{
get
{
return
_decLjIstMon1;
}
set
{
_decLjIstMon1 = value;
OnPropertyChanged(
"_decLjIstMon1"
);
}
}
public
decimal
decPlanMon1
{
get
{
return
_decPlanMon1;
}
set
{
_decPlanMon1 = value;
OnPropertyChanged(
"_decPlanMon1"
);
}
}
public
decimal
decVjIstMon2
{
get
{
return
_decVjIstMon2;
}
set
{
_decVjIstMon2 = value;
OnPropertyChanged(
"_decVjIstMon2"
);
}
}
public
decimal
decLjIstMon2
{
get
{
return
_decLjIstMon2;
}
set
{
_decLjIstMon2 = value;
OnPropertyChanged(
"_decLjIstMon2"
);
}
}
public
decimal
decPlanMon2
{
get
{
return
_decPlanMon2;
}
set
{
_decPlanMon2 = value;
OnPropertyChanged(
"_decPlanMon2"
);
}
}
public
decimal
decVjIstMon3
{
get
{
return
_decVjIstMon3;
}
set
{
_decVjIstMon3 = value;
OnPropertyChanged(
"_decVjIstMon3"
);
}
}
public
decimal
decLjIstMon3
{
get
{
return
_decLjIstMon3;
}
set
{
_decLjIstMon3 = value;
OnPropertyChanged(
"_decLjIstMon3"
);
}
}
public
decimal
decPlanMon3
{
get
{
return
_decPlanMon3;
}
set
{
_decPlanMon3 = value;
OnPropertyChanged(
"_decPlanMon3"
);
}
}
public
decimal
decVjIstMon4
{
get
{
return
_decVjIstMon4;
}
set
{
_decVjIstMon4 = value;
OnPropertyChanged(
"_decVjIstMon4"
);
}
}
public
decimal
decLjIstMon4
{
get
{
return
_decLjIstMon4;
}
set
{
_decLjIstMon4 = value;
OnPropertyChanged(
"_decLjIstMon4"
);
}
}
public
decimal
decPlanMon4
{
get
{
return
_decPlanMon4;
}
set
{
_decPlanMon4 = value;
OnPropertyChanged(
"_decPlanMon4"
);
}
}
public
decimal
decVjIstMon5
{
get
{
return
_decVjIstMon5;
}
set
{
_decVjIstMon5 = value;
OnPropertyChanged(
"_decVjIstMon5"
);
}
}
public
decimal
decLjIstMon5
{
get
{
return
_decLjIstMon5;
}
set
{
_decLjIstMon5 = value;
OnPropertyChanged(
"_decLjIstMon5"
);
}
}
public
decimal
decPlanMon5
{
get
{
return
_decPlanMon5;
}
set
{
_decPlanMon5 = value;
OnPropertyChanged(
"_decPlanMon5"
);
}
}
public
decimal
decVjIstMon6
{
get
{
return
_decVjIstMon6;
}
set
{
_decVjIstMon6 = value;
OnPropertyChanged(
"_decVjIstMon6"
);
}
}
public
decimal
decLjIstMon6
{
get
{
return
_decLjIstMon6;
}
set
{
_decLjIstMon6 = value;
OnPropertyChanged(
"_decLjIstMon6"
);
}
}
public
decimal
decPlanMon6
{
get
{
return
_decPlanMon6;
}
set
{
_decPlanMon6 = value;
OnPropertyChanged(
"_decPlanMon6"
);
}
}
public
decimal
decVjIstMon7
{
get
{
return
_decVjIstMon7;
}
set
{
_decVjIstMon7 = value;
OnPropertyChanged(
"_decVjIstMon7"
);
}
}
public
decimal
decLjIstMon7
{
get
{
return
_decLjIstMon7;
}
set
{
_decLjIstMon7 = value;
OnPropertyChanged(
"_decLjIstMon7"
);
}
}
public
decimal
decPlanMon7
{
get
{
return
_decPlanMon7;
}
set
{
_decPlanMon7 = value;
OnPropertyChanged(
"_decPlanMon7"
);
}
}
public
decimal
decVjIstMon8
{
get
{
return
_decVjIstMon8;
}
set
{
_decVjIstMon8 = value;
OnPropertyChanged(
"_decVjIstMon8"
);
}
}
public
decimal
decLjIstMon8
{
get
{
return
_decLjIstMon8;
}
set
{
_decLjIstMon8 = value;
OnPropertyChanged(
"_decLjIstMon8"
);
}
}
public
decimal
decPlanMon8
{
get
{
return
_decPlanMon8;
}
set
{
_decPlanMon8 = value;
OnPropertyChanged(
"_decPlanMon8"
);
}
}
public
decimal
decVjIstMon9
{
get
{
return
_decVjIstMon9;
}
set
{
_decVjIstMon9 = value;
OnPropertyChanged(
"_decVjIstMon9"
);
}
}
public
decimal
decLjIstMon9
{
get
{
return
_decLjIstMon9;
}
set
{
_decLjIstMon9 = value;
OnPropertyChanged(
"_decLjIstMon9"
);
}
}
public
decimal
decPlanMon9
{
get
{
return
_decPlanMon9;
}
set
{
_decPlanMon9 = value;
OnPropertyChanged(
"_decPlanMon9"
);
}
}
public
decimal
decVjIstMon10
{
get
{
return
_decVjIstMon10;
}
set
{
_decVjIstMon10 = value;
OnPropertyChanged(
"_decVjIstMon10"
);
}
}
public
decimal
decLjIstMon10
{
get
{
return
_decLjIstMon10;
}
set
{
_decLjIstMon10 = value;
OnPropertyChanged(
"_decLjIstMon10"
);
}
}
public
decimal
decPlanMon10
{
get
{
return
_decPlanMon10;
}
set
{
_decPlanMon10 = value;
OnPropertyChanged(
"_decPlanMon10"
);
}
}
public
decimal
decVjIstMon11
{
get
{
return
_decVjIstMon11;
}
set
{
_decVjIstMon11 = value;
OnPropertyChanged(
"_decVjIstMon11"
);
}
}
public
decimal
decLjIstMon11
{
get
{
return
_decLjIstMon11;
}
set
{
_decLjIstMon11 = value;
OnPropertyChanged(
"_decLjIstMon11"
);
}
}
public
decimal
decPlanMon11
{
get
{
return
_decPlanMon11;
}
set
{
_decPlanMon11 = value;
OnPropertyChanged(
"_decPlanMon11"
);
}
}
public
decimal
decVjIstMon12
{
get
{
return
_decVjIstMon12;
}
set
{
_decVjIstMon12 = value;
OnPropertyChanged(
"_decVjIstMon12"
);
}
}
public
decimal
decLjIstMon12
{
get
{
return
_decLjIstMon12;
}
set
{
_decLjIstMon12 = value;
OnPropertyChanged(
"_decLjIstMon12"
);
}
}
public
decimal
decPlanMon12
{
get
{
return
_decPlanMon12;
}
set
{
_decPlanMon12 = value;
OnPropertyChanged(
"_decPlanMon12"
);
}
}
public
decimal
decVjIstMon13
{
get
{
return
_decVjIstMon13;
}
set
{
_decVjIstMon13 = value;
OnPropertyChanged(
"_decVjIstMon13"
);
}
}
public
decimal
decLjIstMon13
{
get
{
return
_decLjIstMon13;
}
set
{
_decLjIstMon13 = value;
OnPropertyChanged(
"_decLjIstMon13"
);
}
}
public
decimal
decPlanMon13
{
get
{
return
_decPlanMon13;
}
set
{
_decPlanMon13 = value;
OnPropertyChanged(
"_decPlanMon13"
);
}
}
public
decimal
decVjIstMon14
{
get
{
return
_decVjIstMon14;
}
set
{
_decVjIstMon14 = value;
OnPropertyChanged(
"_decVjIstMon14"
);
}
}
public
decimal
decLjIstMon14
{
get
{
return
_decLjIstMon14;
}
set
{
_decLjIstMon14 = value;
OnPropertyChanged(
"_decLjIstMon14"
);
}
}
public
decimal
decPlanMon14
{
get
{
return
_decPlanMon14;
}
set
{
_decPlanMon14 = value;
OnPropertyChanged(
"_decPlanMon14"
);
}
}
public
decimal
decVjIstMon15
{
get
{
return
_decVjIstMon15;
}
set
{
_decVjIstMon15 = value;
OnPropertyChanged(
"_decVjIstMon15"
);
}
}
public
decimal
decLjIstMon15
{
get
{
return
_decLjIstMon15;
}
set
{
_decLjIstMon15 = value;
OnPropertyChanged(
"_decLjIstMon15"
);
}
}
public
decimal
decPlanMon15
{
get
{
return
_decPlanMon15;
}
set
{
_decPlanMon15 = value;
OnPropertyChanged(
"_decPlanMon15"
);
}
}
protected
void
OnPropertyChanged(
string
strPropName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if
(handler !=
null
)
{
handler(
this
,
new
PropertyChangedEventArgs(strPropName));
}
}
}
}
Here my Page-Code:
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.Collections.ObjectModel;
using
FutureView.BusinessObjects;
using
FutureView.BLL;
using
Telerik.Windows.Controls;
using
System.Data;
namespace
FutureView.Gui.Wpf
{
/// <summary>
/// Interaktionslogik für PageAbsatzpflege.xaml
/// </summary>
public
partial
class
PageAbsatzpflege : Page
{
FV_Kunde _kunde;
Zeitraum _zeitraum;
ObservableCollection<Absatz> _absaetze =
null
;
//just for testing
DataTable _dtAbsaetze;
public
PageAbsatzpflege()
{
InitializeComponent();
}
public
PageAbsatzpflege(Zeitraum zeitraum, FV_Kunde kunde)
{
InitializeComponent();
_zeitraum = zeitraum;
_kunde = kunde;
lblKunde.Content = _kunde.hahi +
" "
+ _kunde.hantext.Trim();
//Daten binden
AbsatzpflegeBLL objBLL =
new
AbsatzpflegeBLL();
_absaetze =
new
ObservableCollection<Absatz>();
_absaetze = objBLL.GetAbsaetze(_zeitraum, _kunde.hahi);
//rgvAbsaetze.ItemsSource = _absaetze;
//Just for testing
_dtAbsaetze =
new
DataTable();
_dtAbsaetze = objBLL.GetAbsaetzeAsDataTable(_zeitraum, _kunde.hahi);
//standardmäßig wird die 12 Monatsansicht/Geschäftsjahresansicht aufgerufen
Ansicht_aufbauen(_zeitraum,
"EGJ"
);
}
private
void
btnPage_schliessen_Click(
object
sender, RoutedEventArgs e)
{
NavigationService.GoBack();
}
private
void
Ansicht_aufbauen(Zeitraum zr,
string
typ)
{
...
GridViewColumnGroup colGrpArtikeldaten =
new
GridViewColumnGroup();
colGrpArtikeldaten.Name =
"colGrpArtikeldaten"
;
colGrpArtikeldaten.Header =
"Artikeldaten"
;
rgvAbsaetze.ColumnGroups.Add(colGrpArtikeldaten);
GridViewColumnGroup colGrpKum =
new
GridViewColumnGroup();
colGrpKum.Name =
"colGrpKum"
;
colGrpKum.Header =
"Kum."
;
rgvAbsaetze.ColumnGroups.Add(colGrpKum);
GridViewColumnGroup colGrpSum =
new
GridViewColumnGroup();
colGrpSum.Name =
"colGrpSum"
;
if
(typ ==
"GJ"
)
{
colGrpSum.Header = zr.GJ_Von.Substring(4, 2) +
"."
+ zr.GJ_Von.Substring(0, 4) +
" - "
+ zr.GJ_Bis.Substring(4, 2) +
"."
+ zr.GJ_Bis.Substring(0, 4);
}
else
if
(typ ==
"EGJ"
)
{
colGrpSum.Header = zr.GJ_Von.Substring(4, 2) +
"."
+ zr.GJ_Von.Substring(0, 4) +
" - "
+ zr.Erw_GJ_Bis.Substring(4, 2) +
"."
+ zr.Erw_GJ_Bis.Substring(0, 4);
}
rgvAbsaetze.ColumnGroups.Add(colGrpSum);
//*********************************************************************************************************************
//HERE IS THE ERROR-PLACE
rgvAbsaetze.ItemsSource = _absaetze;
GridViewDataColumn gvdcArtext =
new
GridViewDataColumn();
gvdcArtext.Header =
"Artikel-Nr."
;
gvdcArtext.UniqueName =
"Artext"
;
gvdcArtext.DataMemberBinding =
new
Binding(
"strArtext"
);
gvdcArtext.ColumnGroupName =
"colGrpArtikeldaten"
;
this
.rgvAbsaetze.Columns.Add(gvdcArtext);
....
}
}
}
Please look at the block marked as "Error-Place".
Thank you
Michael
I have created a simple project to test the "ERROR-PLACE". So far I was not able to get any error.
Please check the attached sample and let me know what I should change in order to get the exception.
Didie
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
Kind regards,
Didie
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
I wanted to check your sample code, but when I tried to download the zip-file, I got the exception, that it is not possible to find the file!?!?
Can you check/reup the sample?
Good Evening
Michael
I'm very sorry, that I wasted your time. I thought, that the error is at this place, that I marked in the code above. Last night, I took my laptop and coded my own example. And it worked!
So what is the problem with my real application and/or my corporate computer?
15 minutes ago, it dawns on me. The error was, that I also use GridViewExpressionColumns, but at this moment, I don't have defined the expressions!!!
So for this, the problem is solved.
Now I have a new problem. After the data is bind to the grid, it takes a long time that the "form" shows up?!? If there are any hints, then please post. But now I will also check the forum, if there are other threads for this fact. Perhaps I will have to create a new thread. We will see.
Many thanks.
Michael
Can you verify if you have any binding expression errors in the Visual Studio Output window?
Greetings,Vlad
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>