Bind Row number to current item

3 posts, 1 answers
  1. Marcel
    Marcel avatar
    42 posts
    Member since:
    Jan 2014

    Posted 30 Nov 2016 Link to this post

    Hello,

    I am using this example to get a row number in my gridview:
    http://www.telerik.com/forums/how-to-display-the-row-number

    I altered the code from the example and added a dependency property to it, which I am binding to a property of an item from the itemssource collection of the gridview.

    public class RowNumberPresenter : TextBlock
    {
        private GridViewDataControl _parentControl;
        public GridViewDataControl ParentControl
        {
            get { return this._parentControl; }
            private set
            {
                if (this._parentControl != null)
                    this._parentControl.Items.CollectionChanged -= new NotifyCollectionChangedEventHandler(Items_CollectionChanged);
                this._parentControl = value;
                if (this._parentControl != null)
                    this._parentControl.Items.CollectionChanged += new NotifyCollectionChangedEventHandler(Items_CollectionChanged);
            }
        }
      
        public RowNumberPresenter(GridViewDataControl parentControl, object dataItem)
            : base()
        {
            ParentControl = parentControl;
            SetText(dataItem);
        }
      
        private void Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            SetText(this.DataContext);
        }
      
        private void SetText(object dataItem)
        {
            if (this.ParentControl.IsGrouping)
            {
                var group = this.ParentControl.FindGroupByItem(dataItem);
                if (group != null)
                {
                    var items = group.Items as ReadOnlyObservableCollection<object>;
                    if (items != null)
                        this.Text = (items.IndexOf(dataItem) + 1).ToString();
                }
            }
            if (string.IsNullOrWhiteSpace(this.Text))
                this.Text = (this.ParentControl.Items.IndexOf(dataItem) + 1).ToString();
        }
    }

     

    public class RowNumberGridViewColum : GridViewColumn
    {       
        private RowNumberPresenter _rowNumberPresenter;
     
     
        public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)
        {
            _rowNumberPresenter = cell.Content as RowNumberPresenter;
            if (_rowNumberPresenter == null)
                _rowNumberPresenter = new RowNumberPresenter(this.DataControl, dataItem);
                 
            CurrentIndex = Int32.Parse(_rowNumberPresenter.Text);
            return _rowNumberPresenter;
        }
     
        public int CurrentIndex
        {
            get { return (int)GetValue(CurrentIndexProperty); }
            set { SetValue(CurrentIndexProperty, value); }
        }
     
        public static readonly DependencyProperty CurrentIndexProperty =
            DependencyProperty.Register("CurrentIndex", typeof(int), typeof(RowNumberGridViewColum));
    }

     

    <cControlsTelerik:RowNumberGridViewColum 
                    IsReadOnly="True"
                    TabStopMode="Skip"
                    TextAlignment="Center"
                    CurrentIndex="{Binding Path=OrderRowData.SequenceNumber}">
        <cControlsTelerik:RowNumberGridViewColum.Header>
            <TextBlock Text="No.:"/>
        </cControlsTelerik:RowNumberGridViewColum.Header>
    </cControlsTelerik:RowNumberGridViewColum>


    The binding is not working and I am I am a little stuck how to bind the rownumber of the row to my dataobject (a OrderRow object).
    I receive the error that OrderRowData is not a property of OrderViewModel, OrderViewModel is my VM for the whole view?

    The Itemssource of the gridview is set to a Rows property on my OrderViewModel, this Rows collection contains all the OrderRowData instances visible in the grid.

    Any help would be appreciated.

    Regards,

    Marcel

     

  2. Answer
    Stefan
    Admin
    Stefan avatar
    1073 posts

    Posted 05 Dec 2016 Link to this post

    Hello Marcel,

    Thank you for the code provided.

    Speaking in general, the demo that is pointed in the referred forum thread demonstrates a scenario with hard coded row index value. Since you need to extend the demo with logic that relies on data binding, you need to set the binding of the element defined in the CreateCellElement method. This approach is demonstrated in the Create Custom Column Editor topic.

    Additionally, you can take a look at the Custom Column SDK Example. It can be reviewed through the SDK Samples Browser.

    Hopefully, this helps.

    Regards,
    Stefan X1
    Telerik by Progress
    Telerik UI for WPF is ready for Visual Studio 2017 RC! Learn more.
  3. Marcel
    Marcel avatar
    42 posts
    Member since:
    Jan 2014

    Posted 06 Dec 2016 in reply to Stefan Link to this post

    Thanks Stefan, I am not really familiar with customizing a control like this, so this helped a lot.

     

Back to Top