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

How can I create a "column number" column?

4 Answers 64 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Alex
Top achievements
Rank 1
Alex asked on 18 Mar 2010, 12:42 AM
How can I create a column that is has numbers in it that automatically increments as more rows are added to the table?
(This sounds like a trivial question but I've found it unexpectedly difficult to do it :S)

The column has to be able to retroactively modify it self as well (e.g. If someone adds/removes rows mid way through the table, it has to re-order it self)


Context:
Essentially my gridview is showing a normalized table, so the gridviews table (gvtable) will have it's primary key as
foreign key (primary key from main table) + line number from column

4 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 18 Mar 2010, 07:42 AM
Hi,

Please check this demo for more info. if you want to recalculate the number on delete you will need to refresh the grid (or your collection bound to the grid) since the grid is enough smart to not refresh others rows when a row is deleted.

Sincerely yours,
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.
0
Alex
Top achievements
Rank 1
answered on 22 Mar 2010, 04:16 AM
Hi Vlad, thanks for the reply. The example does do what it was designed for, but I need it for something a little bit different. As the example also has the problem of creating duplicate numbers when you add/remove (as you will end up cycling back to a previous index, e.g. create 1 create 2 create 3, delete 1 delete 2, create 2, create 3, end result -> 3 2 3, and as these numbers will be the index, duplicate occurrences will result in crashing)

Is there a way to manually do it via code behind? (I'm not very experienced with the grid view). e.g. Can you do something like this:

foreach item in column
 item.value = item.cell.row
next item
And then I'll tie this to the add/delete row events


---------------------------------------------

I had tried to manually do something on the add row event, but I had noticed that if I deleted one of the rows I created by this, it would crash. Could you also help me find out why this is happening?

Basically, I have a grid view (gv_cashsale), bound via a DomainDataSource from RIA services. The DDS uses a data model called CashSale_Item. And I tried to handle the add new row event like this:

 
        private void gv_cashsale_AddingNewDataItem(object sender, GridViewAddingNewEventArgs e) 
        { 
            RadGridView gv = (RadGridView)sender; 
            CashSale_Item csi = new CashSale_Item(); 
             
            csi.receipt = System.Convert.ToInt64(tb_receipt.Text); 
            csi.line = e.OwnerGridViewItemsControl.Items.Count; 
            e.NewObject = csi; 
        } 
 

Essentially I feed in a value into the cashsale_item.receipt and cashsale_item.line (which is the unique counter). This all seems to work fine, but if I delete one of these rows, the Silverlight app crahes and goes to the blank screen.

The grid it self is as follows:
        <riaControls:DomainDataSource x:Name="CashSale_Item_Data" LoadSize="5" QueryName="GetCashSale_Items"
            <riaControls:DomainDataSource.DomainContext> 
                <ds:CashSale_DomainContext/> 
            </riaControls:DomainDataSource.DomainContext> 
            <riaControls:DomainDataSource.FilterDescriptors> 
                <riadata:FilterDescriptorCollection> 
                    <riadata:FilterDescriptor PropertyPath="receipt" Operator="IsEqualTo"
                        <riaControls:ControlParameter ControlName="tb_receipt" PropertyName="Text" RefreshEventName="TextChanged"/> 
                    </riadata:FilterDescriptor> 
                </riadata:FilterDescriptorCollection> 
            </riaControls:DomainDataSource.FilterDescriptors> 
        </riaControls:DomainDataSource> 
 
                                <telerikGridView:RadGridView Name="gv_cashsale" Height="161" ShowGroupPanel="False" ShowGroupFooters="False"  
                                                             AutoGenerateColumns="False" CanUserReorderColumns="False" CanUserSortColumns="False"  
                                                             IsFilteringAllowed="False" BeginningEdit="gv_cashsale_BeginningEdit"  
                                                             ItemsSource="{Binding ElementName=CashSale_Item_Data, Path=Data, Mode=TwoWay}" ShowColumnFooters="True"  
                                                             AddingNewDataItem="gv_cashsale_AddingNewDataItem" Deleting="gv_cashsale_Deleting"
                                    <telerikGridView:RadGridView.Columns> 
                                        <telerikGridView:GridViewDataColumn Header="" Width="30" DataMemberBinding="{Binding line}" IsReadOnly="True"  
                                                                            SortingState="Ascending" IsVisible="True" x:Name="gv_cashsale_line" /> 
 
                                        <telerikGridView:GridViewDataColumn Header="Code" Width="100" DataMemberBinding="{Binding code}"/> 
                                        <telerikGridView:GridViewDataColumn Header="Description" Width="*" DataMemberBinding="{Binding description}"/> 
                                        <telerikGridView:GridViewDataColumn Header="Qty" Width="40" DataMemberBinding="{Binding quantity}"
                                            <telerikGridView:GridViewDataColumn.AggregateFunctions> 
                                                <telerikData:SumFunction SourceField="quantity" /> 
                                            </telerikGridView:GridViewDataColumn.AggregateFunctions> 
                                        </telerikGridView:GridViewDataColumn> 
                                        <telerikGridView:GridViewDataColumn Header="Tax" Width="65" DataMemberBinding="{Binding taxall}"
                                            <telerikGridView:GridViewDataColumn.AggregateFunctions> 
                                                <telerikData:SumFunction SourceField="taxall" ResultFormatString="{}{0:c}"/> 
                                            </telerikGridView:GridViewDataColumn.AggregateFunctions> 
                                        </telerikGridView:GridViewDataColumn> 
                                        <telerikGridView:GridViewDataColumn Header="Discount" Width="75" DataMemberBinding="{Binding discount}" 
                                                                            DataFormatString="{}{0:c}"
                                            <telerikGridView:GridViewDataColumn.AggregateFunctions> 
                                                <telerikData:SumFunction SourceField="discount" ResultFormatString="{}{0:c}"/> 
                                            </telerikGridView:GridViewDataColumn.AggregateFunctions> 
                                        </telerikGridView:GridViewDataColumn> 
 
                                        <telerikGridView:GridViewDataColumn Header="Amount" Width="75" DataMemberBinding="{Binding totalex}" 
                                                                            DataFormatString="{}{0:c}" IsVisible="False"
                                            <telerikGridView:GridViewDataColumn.AggregateFunctions> 
                                                <telerikData:SumFunction SourceField="totalex" ResultFormatString="{}{0:c}" /> 
                                            </telerikGridView:GridViewDataColumn.AggregateFunctions> 
                                        </telerikGridView:GridViewDataColumn> 
 
                                        <telerikGridView:GridViewDataColumn Header="Amount" Width="75" DataMemberBinding="{Binding totalinc}" 
                                                                            DataFormatString="{}{0:c}"
                                            <telerikGridView:GridViewDataColumn.AggregateFunctions> 
                                                <telerikData:SumFunction SourceField="totalinc" ResultFormatString="{}{0:c}" /> 
                                            </telerikGridView:GridViewDataColumn.AggregateFunctions> 
                                        </telerikGridView:GridViewDataColumn>           
                                         
... 
 
                                    </telerikGridView:RadGridView.Columns> 
                                </telerikGridView:RadGridView> 
 



 

0
Alex
Top achievements
Rank 1
answered on 22 Mar 2010, 04:28 AM
Hi Vlad, thanks for the reply. The example does do what it was designed for, but I need it for something a little bit different. As the example also has the problem of creating duplicate numbers when you add/remove (as you will end up cycling back to a previous index, e.g. create 1 create 2 create 3, delete 1 delete 2, create 2, create 3, end result -> 3 2 3, and as these numbers will be the index, duplicate occurrences will result in crashing)

Is there a way to manually do it via code behind? (I'm not very experienced with the grid view). e.g. Can you do something like this:

foreach item in column
 item.value = item.cell.row
next item
And then I'll tie this to the add/delete row events


---------------------------------------------

I had tried to manually do something on the add row event, but I had noticed that if I deleted one of the rows I created by this, it would crash. Could you also help me find out why this is happening?

Basically, I have a grid view (gv_cashsale), bound via a DomainDataSource from RIA services. The DDS uses a data model called CashSale_Item. And I tried to handle the add new row event like this:

 
        private void gv_cashsale_AddingNewDataItem(object sender, GridViewAddingNewEventArgs e) 
        { 
            RadGridView gv = (RadGridView)sender; 
            CashSale_Item csi = new CashSale_Item(); 
             
            csi.receipt = System.Convert.ToInt64(tb_receipt.Text); 
            csi.line = e.OwnerGridViewItemsControl.Items.Count; 
            e.NewObject = csi; 
        } 
 

Essentially I feed in a value into the cashsale_item.receipt and cashsale_item.line (which is the unique counter). This all seems to work fine, but if I delete one of these rows, the Silverlight app crahes and goes to the blank screen.

The grid it self is as follows:
        <riaControls:DomainDataSource x:Name="CashSale_Item_Data" LoadSize="5" QueryName="GetCashSale_Items"
            <riaControls:DomainDataSource.DomainContext> 
                <ds:CashSale_DomainContext/> 
            </riaControls:DomainDataSource.DomainContext> 
            <riaControls:DomainDataSource.FilterDescriptors> 
                <riadata:FilterDescriptorCollection> 
                    <riadata:FilterDescriptor PropertyPath="receipt" Operator="IsEqualTo"
                        <riaControls:ControlParameter ControlName="tb_receipt" PropertyName="Text" RefreshEventName="TextChanged"/> 
                    </riadata:FilterDescriptor> 
                </riadata:FilterDescriptorCollection> 
            </riaControls:DomainDataSource.FilterDescriptors> 
        </riaControls:DomainDataSource> 
 
                                <telerikGridView:RadGridView Name="gv_cashsale" Height="161" ShowGroupPanel="False" ShowGroupFooters="False"  
                                                             AutoGenerateColumns="False" CanUserReorderColumns="False" CanUserSortColumns="False"  
                                                             IsFilteringAllowed="False" BeginningEdit="gv_cashsale_BeginningEdit"  
                                                             ItemsSource="{Binding ElementName=CashSale_Item_Data, Path=Data, Mode=TwoWay}" ShowColumnFooters="True"  
                                                             AddingNewDataItem="gv_cashsale_AddingNewDataItem" Deleting="gv_cashsale_Deleting"
                                    <telerikGridView:RadGridView.Columns> 
                                        <telerikGridView:GridViewDataColumn Header="" Width="30" DataMemberBinding="{Binding line}" IsReadOnly="True"  
                                                                            SortingState="Ascending" IsVisible="True" x:Name="gv_cashsale_line" /> 
 
                                        <telerikGridView:GridViewDataColumn Header="Code" Width="100" DataMemberBinding="{Binding code}"/> 
                                        <telerikGridView:GridViewDataColumn Header="Description" Width="*" DataMemberBinding="{Binding description}"/> 
                                        <telerikGridView:GridViewDataColumn Header="Qty" Width="40" DataMemberBinding="{Binding quantity}"
                                            <telerikGridView:GridViewDataColumn.AggregateFunctions> 
                                                <telerikData:SumFunction SourceField="quantity" /> 
                                            </telerikGridView:GridViewDataColumn.AggregateFunctions> 
                                        </telerikGridView:GridViewDataColumn> 
                                        <telerikGridView:GridViewDataColumn Header="Tax" Width="65" DataMemberBinding="{Binding taxall}"
                                            <telerikGridView:GridViewDataColumn.AggregateFunctions> 
                                                <telerikData:SumFunction SourceField="taxall" ResultFormatString="{}{0:c}"/> 
                                            </telerikGridView:GridViewDataColumn.AggregateFunctions> 
                                        </telerikGridView:GridViewDataColumn> 
                                        <telerikGridView:GridViewDataColumn Header="Discount" Width="75" DataMemberBinding="{Binding discount}" 
                                                                            DataFormatString="{}{0:c}"
                                            <telerikGridView:GridViewDataColumn.AggregateFunctions> 
                                                <telerikData:SumFunction SourceField="discount" ResultFormatString="{}{0:c}"/> 
                                            </telerikGridView:GridViewDataColumn.AggregateFunctions> 
                                        </telerikGridView:GridViewDataColumn> 
 
                                        <telerikGridView:GridViewDataColumn Header="Amount" Width="75" DataMemberBinding="{Binding totalex}" 
                                                                            DataFormatString="{}{0:c}" IsVisible="False"
                                            <telerikGridView:GridViewDataColumn.AggregateFunctions> 
                                                <telerikData:SumFunction SourceField="totalex" ResultFormatString="{}{0:c}" /> 
                                            </telerikGridView:GridViewDataColumn.AggregateFunctions> 
                                        </telerikGridView:GridViewDataColumn> 
 
                                        <telerikGridView:GridViewDataColumn Header="Amount" Width="75" DataMemberBinding="{Binding totalinc}" 
                                                                            DataFormatString="{}{0:c}"
                                            <telerikGridView:GridViewDataColumn.AggregateFunctions> 
                                                <telerikData:SumFunction SourceField="totalinc" ResultFormatString="{}{0:c}" /> 
                                            </telerikGridView:GridViewDataColumn.AggregateFunctions> 
                                        </telerikGridView:GridViewDataColumn>           
                                         
... 
 
                                    </telerikGridView:RadGridView.Columns> 
                                </telerikGridView:RadGridView> 
 



 

0
Vlad
Telerik team
answered on 23 Mar 2010, 12:09 PM
Hello,

Here is an example how to modify the column to achieve your goal:
using System.Windows;
using System.Windows.Controls;
 
namespace SilverlightApplication1
{
    public class MyColumn : Telerik.Windows.Controls.GridViewColumn
    {
        bool attached = false;
 
        public override FrameworkElement CreateCellElement(Telerik.Windows.Controls.GridView.GridViewCell cell, object dataItem)
        {
            TextBlock textBlock = cell.Content as TextBlock;
 
            if (textBlock == null)
            {
                textBlock = new TextBlock();
            }
 
            textBlock.Text = (this.DataControl.Items.IndexOf(dataItem) + 1).ToString();
 
            if(!attached)
            {
                this.DataControl.Items.CollectionChanged += Items_CollectionChanged;
            }
 
            return textBlock;
        }
 
        void Items_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            attached = true;
 
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add ||
                e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
            {
                this.DataControl.Items.Refresh();
            }
        }
    }
}


Sincerely yours,
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.
Tags
GridView
Asked by
Alex
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Alex
Top achievements
Rank 1
Share this question
or