Problem binding columns by code

8 posts, 0 answers
  1. Michael Hilgers
    Michael Hilgers avatar
    76 posts
    Member since:
    Apr 2010

    Posted 21 Dec 2012 Link to this post

    Hello Everyone,

    let's start with my code:

    The grid
    <telerik:RadGridView HorizontalAlignment="Left" Margin="0,139,0,0" Name="radGridViewRights" VerticalAlignment="Top" Width="1069" Height="349" IsFilteringAllowed="False" AutoGenerateColumns="False" CanUserFreezeColumns="False" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False" CanUserDeleteRows="False" CanUserInsertRows="False" CanUserSelect="False" ShowGroupPanel="False" ShowColumnHeaders="True"/>

    The code behind:
    GridViewDataColumn col1 = new GridViewDataColumn();
    col1.UniqueName = "groups";
    col1.Header = "";
    col1.DataMemberBinding = new System.Windows.Data.Binding("moduleDesc");
    col1.Width = 180; 
     
    this.radGridViewRights.Columns.Add(col1);
     
    foreach (KeyValuePair<int, string> kvp in GDC.groupDescIds)
    {
        GridViewCheckBoxColumn col = new GridViewCheckBoxColumn();
        col.UniqueName = kvp.Value;
        col.Header = kvp.Value;
        col.DataMemberBinding = new System.Windows.Data.Binding("group_" + kvp.Key);
        col.Width = 80;
     
        this.radGridViewRights.Columns.Add(col);
    }
     
    ObservableCollection<ModuleGroupRightsDataSource> data = new ObservableCollection<ModuleGroupRightsDataSource>();
     
    ModuleGroupRightsDataSource obj = new ModuleGroupRightsDataSource();
    obj.moduleDesc = GDC.moduleDesc_Articles;
    obj.group_1 = true;
    obj.group_3 = true;
    obj.group_8 = true;
     
    data.Add(obj);
     
    obj = new ModuleGroupRightsDataSource();
    obj.moduleDesc = GDC.moduleDesc_Partsgroups;
    obj.group_3 = true;
    obj.group_8 = true;
     
    data.Add(obj);
     
    this.radGridViewRights.ItemsSource = data;

    The data class
    public class ModuleGroupRightsDataSource
    {
        public string moduleDesc = "";
        public Boolean group_1 = false;
        public Boolean group_2 = false;
        public Boolean group_3 = false;
        public Boolean group_4 = false;
        public Boolean group_5 = false;
        public Boolean group_6 = false;
        public Boolean group_7 = false;
        public Boolean group_8 = false;
        public Boolean group_9 = false;
        public Boolean group_10 = false;
    }


    As you may see, i try to bind the data from the 'ModuleGroupRightsDataSource' objects to the columns of the grid. But it's not binding. The rows for the entrys in the itemssource are created (so the grid finds out that there are two rows to show), but the cells are empty and show no values.

    I can't find my mistake ... anyone any ideas or hints?

    Best Regards and a pleasant Doomsday ;)

    Michael
  2. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 21 Dec 2012 Link to this post

    Hi Michael,

     You need properties for your class - currently you have fields only. :)

    Greetings,
    Vlad
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. Michael Hilgers
    Michael Hilgers avatar
    76 posts
    Member since:
    Apr 2010

    Posted 21 Dec 2012 Link to this post

    Hey Vlad,

    thanks for your fast reply! But this didn't help ...

    Here are my changes:

    The data class:
    public class ModuleGroupRightsDataSource
    {
        private string moduleDesc = "";
        public string ModuleDesc
        {
            get { return moduleDesc; }
            set { moduleDesc = value; }
        }
     
        private Boolean group_1 = false;
        public Boolean Group_1
        {
            get { return group_1; }
            set { group_1 = value; }
        }
     
        private Boolean group_2 = false;
        public Boolean Group_2
        {
            get { return group_2; }
            set { group_2 = value; }
        }
     
        private Boolean group_3 = false;
        public Boolean Group_3
        {
            get { return group_3; }
            set { group_3 = value; }
        }
     
        private Boolean group_4 = false;
        public Boolean Group_4
        {
            get { return group_4; }
            set { group_4 = value; }
        }
     
        private Boolean group_5 = false;
        public Boolean Group_5
        {
            get { return group_5; }
            set { group_5 = value; }
        }
     
        private Boolean group_6 = false;
        public Boolean Group_6
        {
            get { return group_6; }
            set { group_6 = value; }
        }
     
        private Boolean group_7 = false;
        public Boolean Group_7
        {
            get { return group_7; }
            set { group_7 = value; }
        }
     
        private Boolean group_8 = false;
        public Boolean Group_8
        {
            get { return group_8; }
            set { group_8 = value; }
        }
     
        private Boolean group_9 = false;
        public Boolean Group_9
        {
            get { return group_9; }
            set { group_9 = value; }
        }
     
        private Boolean group_10 = false;
        public Boolean Group_10
        {
            get { return group_10; }
            set { group_10 = value; }
        }
    }

    The code behind
    GridViewDataColumn col1 = new GridViewDataColumn();
    col1.UniqueName = "groups";
    col1.Header = "";
    col1.DataMemberBinding = new System.Windows.Data.Binding("ModuleDesc");
    col1.Width = 180; 
     
    this.radGridViewRights.Columns.Add(col1);
     
    foreach (KeyValuePair<int, string> kvp in GDC.groupDescIds)
    {
        GridViewCheckBoxColumn col = new GridViewCheckBoxColumn();
        col.UniqueName = kvp.Value;
        col.Header = kvp.Value;
        col.DataMemberBinding = new System.Windows.Data.Binding("Group_" + kvp.Key);
        col.Width = 80;
     
        this.radGridViewRights.Columns.Add(col);
    }
     
    ObservableCollection<ModuleGroupRightsDataSource> data = new ObservableCollection<ModuleGroupRightsDataSource>();
     
    ModuleGroupRightsDataSource obj = new ModuleGroupRightsDataSource();
    obj.ModuleDesc = GDC.moduleDesc_Articles;
    obj.Group_1 = true;
    obj.Group_3 = true;
    obj.Group_8 = true;
     
    data.Add(obj);
     
    obj = new ModuleGroupRightsDataSource();
    obj.ModuleDesc = GDC.moduleDesc_Partsgroups;
    obj.Group_3 = true;
    obj.Group_8 = true;
     
    data.Add(obj);
     
    this.radGridViewRights.ItemsSource = data;
  4. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 21 Dec 2012 Link to this post

    Hello,

     Do you have any binding expression errors in the Visual Studio Output window? 

    Kind regards,
    Vlad
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  5. Michael Hilgers
    Michael Hilgers avatar
    76 posts
    Member since:
    Apr 2010

    Posted 21 Dec 2012 Link to this post

    Oh, yes, there are binding expressing errors .... never looked into the output windows ...

    I resolved the issue. What i've changed is the unique name property of the columns. If they MATCH the binding field name, it works, if it NOT MATCHES the binding field name, it's not working. Is this behaviour a bug or is this by design?

    Kind Regards,
    Michael
  6. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 21 Dec 2012 Link to this post

    Hello,

     You do not need to set UniqueName if you have DataMemberBinding. 

    Kind regards,
    Vlad
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  7. Michael Hilgers
    Michael Hilgers avatar
    76 posts
    Member since:
    Apr 2010

    Posted 21 Dec 2012 Link to this post

    Okay, thanks for you help and guidance to the right direction ;)

    Have a nice Christmas!
  8. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 21 Dec 2012 Link to this post

    Happy Holidays! :)

    All the best,
    Vlad
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top