How to get the current row index and column index of the selected GridTemplateColumn ?

I have created a gridtemplatecolumn inside the viewcell, I have created a label with tap gesture in there i need a row and column index. when i tap the label.

I have attached my code.


  var column = new GridTemplateColumn();

  var dtt = new DataTemplate(() => {

         return new DateTimeTextBox();

   });

 column.CellTemplate = dtt;

dataGrid.Columns.Add(column);

dataGrid.ItemSource= DataTable;


    public class DateTimeTextBox : ViewCell

   {

            StackLayout mainLayout = new StackLayout();

            mainLayout.Orientation = StackOrientation.Horizontal;

     

            var label = new Label();

             label .Text = "Tap Here";

            var tap = new TapGestureRecognizer();

            tap.Tapped += (s, e) =>

            {

                // handle the tap

                // Need Row and Column Index

            };




    }


7 Replies 1 reply marked as answer

SV Suja Venkatesan Syncfusion Team February 16, 2022 02:08 PM UTC

Hi Bharath, 

You can get the RowIndex and ColumnIndex of tapped label from its parent which is GridCell as like below code snippet. 

Code snippet: 
public class DateTimeTextBox : ViewCell 
{ 
        StackLayout stack; 
        Label label; 
        TapGestureRecognizer tap; 
        public DateTimeTextBox() 
        { 
            stack = new StackLayout() { Orientation = StackOrientation.Vertical }; 
            tap = new TapGestureRecognizer(); 
            tap.Tapped += Tap_Tapped; 
            label = new Label() { Text = "test" }; 
            label.GestureRecognizers.Add(tap); 
            stack.Children.Add(label); 
            View = stack; 
        } 
        private void Tap_Tapped(object sender, EventArgs e) 
        { 
          var rowindex=  ((sender as Label).Parent.Parent as GridCell).DataColumn.RowIndex; 
          var colindex=((sender as Label).Parent.Parent as GridCell).DataColumn.ColumnIndex; 
          Debug.WriteLine("RowIndex" + rowindex + "\t" + "ColumnIndex" + colindex + "\n"); 
        } 
} 

Please refer the below sample for your reference.   

Please let us know if you need any further assistance. 

Regards, 
Suja 


Marked as answer

BH Bharath February 17, 2022 12:40 PM UTC

tq it worked.How do i assign value on GridTemplate> Label View. i am adding row on sfdatagrid dynamically with the values.



SV Suja Venkatesan Syncfusion Team February 18, 2022 07:38 PM UTC

Hi Bharath,


Currently, we are checking the possibility to achieve your requirement on our end. We will update you with further details on or before February 22,2022. We appreciate your patience until then.


Regards,
Suja




SV Suja Venkatesan Syncfusion Team February 23, 2022 04:06 AM UTC

Hi Bharath, 

You can achieve your requirement “Assign value on GridTemplate Label View” as like below code snippet. 

Code snippet: 
public DataGridPage() 
{ 
     InitializeComponent(); 
      dataGrid.AutoGenerateColumns = false; 
      var column = new GridTemplateColumn(); 
      column.MappingName = "FirstName"; 
      var dtt = new DataTemplate(() => 
      { 
        return new DateTimeTextBox(); 
      }); 
      column.CellTemplate = dtt; 
      dataGrid.Columns.Add(column); 
 
} 
 
public class DateTimeTextBox : ViewCell 
{ 
        StackLayout stack; 
        Label label; 
        TapGestureRecognizer tap; 
        public DateTimeTextBox() 
        { 
            stack = new StackLayout() { Orientation = StackOrientation.Vertical }; 
            tap = new TapGestureRecognizer(); 
            tap.Tapped += Tap_Tapped; 
            label = new Label(); 
            label.SetBinding(Label.TextProperty, new Binding($"[{0}]")); 
            label.GestureRecognizers.Add(tap); 
            stack.Children.Add(label); 
            View = label; 
        } 
} 

In below sample link, we have also attached a simple sample with GridTemplate based on requirement for your reference. 

Please let us know if you need any further assistance. 

Regards, 
Suja 



BH Bharath replied to Suja Venkatesan February 23, 2022 12:25 PM UTC

tq it worked If I have several columns how do I get the column index on Viewcell?



SV Suja Venkatesan Syncfusion Team February 24, 2022 02:54 PM UTC

Hi Bharath, 

Currently, we are checking the possibility to achieve your requirement on our end. We will update you with further details on or before February 28,2022. We appreciate your patience until then. 

Regards, 
Suja 



SV Suja Venkatesan Syncfusion Team February 28, 2022 02:28 PM UTC

Hi Bharath, 

You can achieve your requirement “Get the column index on ViewCell  if SfDataGrid with more columns” as like below code snippet. 

Code Snippet: 
for (int i=0; i < PeopleDT.Columns.Count ; i++) 
{ 
  var column = new GridTemplateColumn(); 
  column.MappingName = PeopleDT.Columns[i].ColumnName; 
  column.CellTemplate = GetTemplate(i); 
  dataGrid.Columns.Add(column); 
} 
  dataGrid.ItemsSource = PeopleDT; 
  this.Content = dataGrid; 
} 
private DataTemplate GetTemplate(int index) 
{ 
  DataTemplate cellTemplate = new DataTemplate(() => 
{ 
   return new DateTimeTextBox(index); 
}); 
return cellTemplate; 
} 
 
public class DateTimeTextBox : ViewCell 
{ 
     StackLayout stack; 
     Label label; 
     TapGestureRecognizer tap; 
   
public DateTimeTextBox(int no) 
{ 
   stack = new StackLayout() { Orientation = StackOrientation.Vertical }; 
   tap = new TapGestureRecognizer(); 
   tap.Tapped += Tap_Tapped; 
   label = new Label(); 
   label.SetBinding(Label.TextProperty, new Binding($"[{no}]")); 
  label.GestureRecognizers.Add(tap); 
  stack.Children.Add(label); 
  View = label; 
} 
 

In below sample link, we have also attached a simple sample with GridTemplate based on requirement for your reference.  

Please let us know if you need any further assistance. 

Regards, 
Suja 



Loader.
Up arrow icon