BInding a Custom Cell to the underlying Data Source

6 posts, 1 answers
  1. Andrew
    Andrew avatar
    104 posts
    Member since:
    May 2008

    Posted 04 Apr 2011 Link to this post

    The way I am currently binding to the grid is via the DataTable control defined here: blogs.telerik.com/vladimirenchev/posts/09-04-23/lightweight_datatable_for_your_silverlight_applications.aspx .

    I am looking to create a custom column using the example here: http://www.telerik.com/help/silverlight/gridview-add-button-column.html 

    The control is comprised of both a textbox and a button. How do I use the GridViewColumn in such a way that the textbox I add is bound to the underlying DataTable for that associated dataItem? I need to later access the underlying value of the textbox in order to save the data.
    Thanks.
  2. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 05 Apr 2011 Link to this post

    Hello,

    I believe that there will be no differences between normal collection and the DataTable in this case. 

    Regards,
    Vlad
    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
  3. Andrew
    Andrew avatar
    104 posts
    Member since:
    May 2008

    Posted 05 Apr 2011 Link to this post

    Yes, I have the previous example working in with the DataTable. What I am asking is: how do I override CreateCellElement() in such a way that the TextBox.Text property is bound to the underlying datasource value for that row/column? I need to display the values in each TextBox to the user, and when he clicks a submit button I need to save the data in the bound data source to a database, but it must reflect any changes made to the Text property of each TextBox.

    Below is an example of the simplest implementation of my control:

    public override FrameworkElement CreateCellElement(Telerik.Windows.Controls.GridView.GridViewCell cell, object dataItem)
    {
        FrameworkElement ele = base.CreateCellElement(cell, dataItem);
     
        if (cell.Content != null)
        {
            return (FrameworkElement)cell.Content;
        }
     
        return CreateHeaderControl(cell, dataItem);
    }
     
     
    private FrameworkElement CreateHeaderControl(Telerik.Windows.Controls.GridView.GridViewCell cell, object dataItem)
    {
        FrameworkElement control = null;
        TextBox textBox = null;
     
        textBox = new TextBox();
     
        //How do I bind this Text property to the underlying dataItem value
        //for a dynamically generated class in such a way that the TextBox
        //displays the value from the data source and updates the value in
        //the data source when its Text is changed?
        textBox.Text = ???
     
        control = textBox;
        cell.Content = control;
     
        return control;
    }


  4. Answer
    Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 05 Apr 2011 Link to this post

    Hello,

     You can use the GridViewDataColumn instead GridViewColumn and bind the DataMemberBinding to the TextBox Text property. Similar implementation can be found on this blog post

    Kind regards,
    Vlad
    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
  5. Andrew
    Andrew avatar
    104 posts
    Member since:
    May 2008

    Posted 05 Apr 2011 Link to this post

    Using the described binding with the TwoWay mode does the trick, thanks.
  6. Paulo
    Paulo avatar
    2 posts
    Member since:
    Apr 2011

    Posted 03 Oct 2011 Link to this post

      textBox.SetBinding(ContentControl.ContentProperty, DataMemberBinding);
    
Back to Top