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

[Solved] Row index in currentcellchanged event

3 Answers 489 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.
vedant
Top achievements
Rank 1
vedant asked on 17 Aug 2011, 02:58 PM
Hello,

 I have unique grid that is using SL lightweight datatable to populate cells in my RadGridView. I have to use it because my grid is dynamically created as per users preference about number of rows and columns.

 I have to bind blank rows initially and later user populate the data inside the cells using separate form view. When user select cell I have to fetch data from DB and populate that on form view. In order to do so I need to know row index and column index of selected cell.

 I can find column index easily but problem lies in row index because data in each cell is empty string and RadGridview is trying to lookup data based on datacontext as below: 

RadGridView1.Items.IndexOf(RadGridView1.CurrentCell)here is my Xaml: 
<telerik:RadGridView x:Name="RadGridView1" AutoExpandGroups="True" RowIndicatorVisibility="Collapsed" Grid.Row="1"                  SelectionUnit="FullRow"    CurrentCellChanged="RadGridView1_CurrentCellChanged" SelectedCellsChanged="RadGridView1_SelectedCellsChanged"                SelectionChanged="RadGridView1_SelectionChanged" />

Here is my codebehind:
DataTable table = null;
      public MainPage()
      {
          InitializeComponent();
          Random rnd = new Random();
          table = new DataTable();
          for (int i = 0; i <= 5; i++)
          {
              var coln = "col " + i.ToString();
              table.Columns.Add(new DataColumn() { ColumnName = coln, DataType = typeof(string) });
          }
          for (var r = 0; r < 10; r++)
          {
              DataRow row = new DataRow();
              for (int c = 0; c <= 5; c++)
              {
                  var coln = "col " + c.ToString();
                  row[coln] = "Hello";
              }
              table.Rows.Add(row);
          }
          RadGridView1.ItemsSource = table.ToList();
      }
      private void RadGridView1_CurrentCellChanged(object sender, Telerik.Windows.Controls.GridViewCurrentCellChangedEventArgs e)
      {
          txtitems.Text = "you selected             Row   " + RadGridView1.Items.IndexOf(RadGridView1.CurrentCell)
                          + "        Column  " + e.NewCell.Column.DisplayIndex;
      }
      private void RadGridView1_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
      {
      }
      private void RadGridView1_SelectedCellsChanged(object sender, Telerik.Windows.Controls.GridView.GridViewSelectedCellsChangedEventArgs e)
      {
          //txtitems.Text = "you selected             Row   " + RadGridView1.Items.IndexOf(e.AddedCells[0].Item)
          //               + "        Column  " + e.AddedCells[0].Column.DisplayIndex;
      }

I tried using both types of selectionunit (cell,fullrow), but none worked.
Is there anyway to get selected row index using some other event? I go through documentation but no help so far.
Thanks in advance.
-Vedant
NOTE: you need to get data.cs and dynamic.cs classes in your project from
here


3 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 17 Aug 2011, 03:33 PM
Hello Vedant,


Since getting the index of the row is not reliable - it is dependant on sorting ,scrolling and virtualization ( row recycling) .  Getting the visual index in this context is not recommended.

What I recommend here is to add a hidden column to your datatable with row number. There is no need to show it , it will be just a secure handle to the row.

Then in the event handler you can easily get this row number by accessing the DataContext of the sender row or cell.


Best wishes,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

0
vedant
Top achievements
Rank 1
answered on 17 Aug 2011, 06:56 PM
Hello,

thanks for your reply but I can not really use this solution because my radgridview itemsource is getting generated dynamically (as user decided how many rows and column he wants to see ) . Due to this I have to have autogeneratecolumns = true in my code.

 I am not sure if there is a way to bind a additional column to the radgridview and set it hidden when autogeneratecolumn = true.

 Please refer my test code for details.

Thanks
Vedant
0
Pavel Pavlov
Telerik team
answered on 18 Aug 2011, 12:45 PM
Hi Vedant,

Yes , there is a way to cancel autogenerating the column for your number property. Just handle the AutoGenerating event , and cancel it for the number column :) such as :

where "MyNumberProperty" should be replaced by your property name.
public MainPage()
        {
            InitializeComponent();
             
             
            gridView.AutoGeneratingColumn += new EventHandler<GridViewAutoGeneratingColumnEventArgs>(g_AutoGeneratingColumn);
        }
 
        void g_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
        {
            var column = e.Column as GridViewDataColumn;
            if (column.DataMemberBinding.Path.Path == "MyNumberProperty")
            {
                e.Cancel = true;
            }
        }


Regards,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

Tags
GridView
Asked by
vedant
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
vedant
Top achievements
Rank 1
Share this question
or