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

Dynamic binding

1 Answer 71 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Yuxiao
Top achievements
Rank 1
Yuxiao asked on 20 Jul 2012, 09:47 AM
Hi,
   I have some trouble when I am doing dynamic binding.  I can add column now. But I can't edit the value in the column.
Can anyone give me some suggestion? 
 

Below is my code.


namespace
RadControlsSilverlightApp1
{
 
    public partial class MainPage : UserControl
    {
        string[] columnheaders = { "Model", "Texture", "Retake" };
        ObservableCollection<MyDataRow> _data = new ObservableCollection<MyDataRow>();
 
        public virtual ObservableCollection<MyDataRow> data
        {
            get
            {
                return _data;
            }
            set
            {
                _data = value;
            }
        }
        public MainPage()
        {
 
 
            InitializeComponent();
          
            for (int i = 0; i < 50; ++i)
            {
                MyDataRow row = new MyDataRow();
                foreach (string str in columnheaders)
                {
                    row[str] = new Decimal(1.0);
                }
                _data.Add(row);
            }
            xyz.ItemsSource = data;
            
        }
 
        private void button1_Click(object sender, RoutedEventArgs e)
        {
             
            Telerik.Windows.Controls.GridViewDataColumn gvc = new Telerik.Windows.Controls.GridViewDataColumn();
            gvc.Header = textBox1.Text;
            gvc.UniqueName = textBox1.Text;
            gvc.DataMemberBinding = new Binding(textBox1.Text);
             
            gvc.FilterMemberPath = textBox1.Text;
            gvc.GroupMemberPath = textBox1.Text;
            gvc.SortMemberPath = textBox1.Text;
             
             
            gvc.IsReadOnly = false;
            //gvc.DataType = typeof(Decimal);
            bool allow = true;
            foreach (Telerik.Windows.Controls.GridViewColumn gv in xyz.Columns)
            {
                if (gv.Header.ToString() == textBox1.Text)
                {
                    allow = false;
                    break;
                }
            }
            if (allow)
            {
                foreach (MyDataRow row in data)
                {
                    row[textBox1.Text] = new decimal(3.0);  
                }
 
                xyz.Columns.Add(gvc);
                xyz.Rebind();               
            }
        }
    }
}

1 Answer, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 23 Jul 2012, 04:19 PM
Hello,

You can check our online demos showing editing for dynamic data.

I can see that on a button click you set the DataMemberBinding to be textBox1.Text. This is not a recommended way to work the GridViewDataColumn. Would you please work with the data items rather then relying on the text of a visual element?


Greetings,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
GridView
Asked by
Yuxiao
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Share this question
or