This is a migrated thread and some comments may be shown as answers.

Create Grid View Columns on the fly

5 Answers 170 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Kiran
Top achievements
Rank 1
Kiran asked on 24 Aug 2010, 02:55 PM

Hi

I have a requirement where i need to generate some columns of a grid dynamically at runtime. I have found a solution you have provided as part of another ticket and I updated the code to see if it works for my situation. In my case i need to show checkboxes and allow the user to edit it.
I am attaching the modified source code.

ICollection<EmployeeRecord> employeeRecords;
// Get some mock data
           employeeRecords = GetDummyData();
           GridViewDataColumn tempDataColumn = new GridViewDataColumn();
           //Add the known columns
           tempDataColumn=new GridViewDataColumn() 
           
               UniqueName = "EmployeeName"
               , DataMemberBinding = new Binding("EmployeeName")
           };
           tempDataColumn.IsReadOnly = true;
           this.grid.Columns.Add(tempDataColumn);
           tempDataColumn = new GridViewDataColumn()
           {
               UniqueName = "ID"
               ,
               DataMemberBinding = new Binding("ID")
           };
           tempDataColumn.IsReadOnly = true;
           this.grid.Columns.Add(tempDataColumn);
             
             
           // Now add the dynamic number of columns
             
           // Determines the maximum number of months that any employee has worked.
           int maxNumberOfMonths = employeeRecords.Max((x) => x.RolePermission.Count);
           for (int i = 0; i < maxNumberOfMonths; i++)
           {
               tempDataColumn=new GridViewDataColumn()
               {
                   UniqueName = "Read" + (i + 1)
                   , DataMemberBinding = new Binding("RolePermission[" + i + "]")
                   , DataType = typeof(bool) 
               };
               tempDataColumn.EditorSettings=new CheckBoxEditorSettings();
               tempDataColumn.IsReadOnly = false;
               this.grid.Columns.Add(tempDataColumn);
           }
             
             
           // Finally, bind the grid
           this.grid.ItemsSource = employeeRecords;

 

 

I have to solve following problems

1. I am able to uncheck the check boxes but it is getting reset when I move to next row i need this persisted so that I can save those values to the database.
2. I need to hide checkboxes for some rows under some columns based on a value is it possible directly something i can set while creating the columns.

Thanks & Regards
Kiran

5 Answers, 1 is accepted

Sort by
0
Yavor Georgiev
Telerik team
answered on 24 Aug 2010, 04:48 PM
Hi Kiran,

 Could you please try to set the mode of your binding for the RolePermission columns to TwoWay?

You can use also set a DataTemplateSelector to the RolePermission columns' CellTemplate and CellEditTemplate properties that will return an empty DataTemplate, effectively making those cells blank. You can read more about template selectors here.

All the best,
Yavor Georgiev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Kiran
Top achievements
Rank 1
answered on 24 Aug 2010, 06:57 PM
I am not sure as to how to set Binding Two Way as I set the ItemSource during runtime.

<telerik:RadGridView Name="grid" AutoGenerateColumns="False"/>
 and set it dynamically during runtime with below code.

this.grid.ItemsSource = employeeRecords;

My problem is how do i link both these and make it two way binding.

Regards
Kiran
0
Yavor Georgiev
Telerik team
answered on 27 Aug 2010, 11:21 AM
Hello Kiran,

 I meant that you should change your code to look like:

DataMemberBinding = new Binding("RolePermission[" + i + "]") { Mode = BindingMode.TwoWay }

All the best,
Yavor Georgiev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Kiran
Top achievements
Rank 1
answered on 04 Sep 2010, 07:12 PM
Hi

This works. Thanks now i need to get to solve my next problem. Is it possible to bind a custom object instead of a bool.

public class PermissionValue
    {
  
        public short PermissionTypeID
        {
            get;
            set;
        }
  
        public string PermissionTypeDescription
        {
            get;
            set;
        }
  
        public bool IsAvailable
        {
            get;
            set;
        }
  
        public bool Value
        {
            get;
            set;
        }
  
    }
  
    public class EmployeeRecord
    {
        public string EmployeeName
        {
            get;
            set;
        }
  
        public int ID
        {
            get;
            set;
        }
  
        public int ParentID
        {
            get;
            set;
        }
  
        List<PermissionValue> rolePermission = new List<PermissionValue>();
        public IList<PermissionValue> RolePermission
        {
            get { return this.rolePermission; }
        }
    }
When I try to use the below code nothing get's bound. Is there anything else I need to do.

tempDataColumn = new GridViewDataColumn()
                {
                    Header=permissionValue.PermissionTypeDescription
                    ,
                    UniqueName = permissionValue.PermissionTypeDescription
                    ,
                    DataMemberBinding = new Binding("RolePermission[" + i + "].Value") { Mode = BindingMode.TwoWay }
                    ,
                    DataType = typeof(bool)
                };

Thanks
Kiran
0
Kiran
Top achievements
Rank 1
answered on 06 Sep 2010, 07:13 AM
Hi

This seems to be working fine now. I have another problem which i opened as new thread.

Regards
Kiran
Tags
GridView
Asked by
Kiran
Top achievements
Rank 1
Answers by
Yavor Georgiev
Telerik team
Kiran
Top achievements
Rank 1
Share this question
or