We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

how to capture Cell Value from the Datagrid

how to capture the value of the cell

 

hi , I am new in using the SyncFusion SDK , currently I am using the trial one.

 

My program is like this.

 

Reading a files from a local folder in my Windows phone 8.1 device , and view it in a SFdatagrid while demand.

While choosing the RadioButton the view will popup and be filled with the data from the chosen test file.

 

Check the code I am using below

 

 

        private async void RCustFile_Checked(object sender, RoutedEventArgs e)

        {

 

            this.theGrid.Visibility = Visibility.Visible;

 

            theGrid.Columns.Add(new GridTextColumn() { HeaderText = "Cust#", MappingName = "[" + (0) + "]" });

            theGrid.Columns.Add(new GridTextColumn() { HeaderText = "Cust Name", MappingName = "[" + (1) + "]" });

 

            List<List<string>> rows = new List<List<string>>();

            

            // Get the local folder.

            StorageFolder local = KnownFolders.DocumentsLibrary;

 

            if (local != null)

            {

                // Get the file.

                var file = await local.OpenStreamForReadAsync("CUSTD01.DOT");

 

                //read all the lines

                string delimiter = ",";

                StreamReader streamReader = new StreamReader(file);

                string allData = streamReader.ReadToEnd();

                string[] frows = allData.Split("\r".ToCharArray());

 

                // data

                foreach (string y in frows)

                {

                    var t = new List<string>();

                    string[] items = y.Split(delimiter.ToCharArray());

 

                    for (int i = 0; i < 2; i++)//how many columns in the grid

                    {

                        t.Add(items[i]);

                    }

 

                    rows.Add(t);

                }

 

                theGrid.ItemsSource = rows;

            }

 

        }

 

 

 

From the code above I am trying to get all the information I need to carry on with my program

Which is the value of the cells i am chosen by tapping on the cell OR to get the data of the line row.

 

I tried a lot but nothing working.  Please help.


11 Replies

JS Jayapradha S Syncfusion Team April 9, 2015 03:39 PM UTC

Hi Nedal,

Thank you for using Syncfusion products.

We have analyzed your requirement and you can get the row data and value of each column in datagrid loaded event through RowGenerator as shown in the below code snippet.


Code Snippet:

void sfGrid_Loaded(object sender, RoutedEventArgs e)

{

var rowGenerator = this.sfGrid.GetRowGenerator();

var items = rowGenerator.Items;

var rowData = (items[1].RowData);

var row = (rowData as Employees);
}


To access the GetRowGenerator(), you must include the below namespace:
using Syncfusion.UI.Xaml.Grid.Helpers;

In your shared code snippet, you have set the itemssource for grid from a file in Local folder. But before that you have load the grid and set the visibility is visible. Could you please share the details have you load the grid in radio button checked event, until then have you set the visibility of the grid is collapsed?
We could not get the exact information from your code snippet and so we have suggested for getting the datarow and cellvalue from grid in grid loaded event.
Kindly let us know if this solution helps you.

Regards,
Jayapradha


SR Sivakumar R Syncfusion Team April 9, 2015 06:10 PM UTC

Hi Nedal,

Please ignore the previous update. Please find the update below,

You can get the cell information while tabbing the cell by handling SfDataGrid.CurrentCellActivating event as in the below code snippet,

this.sfgrid.CurrentCellActivating += sfgrid_CurrentCellActivating;

void sfgrid_CurrentCellActivating(object sender, CurrentCellActivatingEventArgs args)

{

if (args.ActivationTrigger == ActivationTrigger.Mouse || args.ActivationTrigger == ActivationTrigger.Touch)

{

var record = sfgrid.GetRecordAtRowIndex(args.CurrentRowColumnIndex.RowIndex);

if (record == null)

return;

var data = record as SalesByYear;

var colindex = sfgrid.ResolveToGridVisibleColumnIndex(args.CurrentRowColumnIndex.ColumnIndex);

if (colindex >= 0)

{

var column = sfgrid.Columns[colindex];

}

//You can set args.Cancel as true if you don't want selection in Grid

args.Cancel = true;

}

}

Note: we have set SelectionMode as one. If you don’t want selection, then you can cancel the selection in CurrentCellActivating event by setting args.Cancel = true as.

Sample:

http://www.syncfusion.com/downloads/support/directtrac/137596/WPSample-1865118023.zip

Please let us know if you need any further assistance on this.

Thanks,

Sivakumar




NQ Nedal Qunibi April 12, 2015 09:04 AM UTC

dear Sivakumar,

I have tried to use the code you listed in your answer this is what I am getting as result.

Private void theGrid_CurrentCellActivating(object sender, CurrentCellActivatingEventArgs args)

{

if (args.ActivationTrigger == ActivationTrigger.Mouse || args.ActivationTrigger == ActivationTrigger.Touch)

{

var record = theGrid.GetRecordAtRowIndex(args.CurrentRowColumnIndex.RowIndex);

if (record == null)

return;

//var data = record as SalesByYear;

var colindex = theGrid.ResolveToGridVisibleColumnIndex(args.CurrentRowColumnIndex.ColumnIndex);

if (colindex >= 0)

{

var column = theGrid.Columns[colindex];

ItemText.Text = column.ToString();

}

//You can set args.Cancel as true if you don't want selection in Grid

//args.Cancel = true;

}

}


the result is  "Syncfusion.UI.Xaml.Grid.GridtTextColumn"


and I have checked the sample you gave , it's seems it's for WP platform not WP8

unless it doesn't make any difference.


do I have to activate anything to avoid this result




NQ Nedal Qunibi April 12, 2015 09:58 AM UTC

Dear Jayapradha,

yes I am using  (Visibility ="Collapsed" ) in the Xaml code, and I am loading the data into the grid thru the Radio button after setting the grid into Visible.

I tried your code, but the app is closing while loading.


JS Jayapradha S Syncfusion Team April 13, 2015 11:40 AM UTC

Hi Nedal,

Thank you for your update.

You can get the cellvalue from datagrid using GetRecordAtRowIndex in SelectionChanging Event as shown in the following code example :

Code Snippet:

this.sfgrid.SelectionChanging += sfGrid_SelectionChanging;

  void sfGrid_SelectionChanging(object sender, GridSelectionChangingEventArgs e)

        {

            var record = sfgrid.GetRecordAtRowIndex(sfgrid.SelectionController.CurrentCellManager.CurrentRowColumnIndex.RowIndex);

            if (record == null)

                return;

            var data = record as SalesByYear; //you can get the cell value from data

            var Name = data.Name; //for example get the Name property from data variable at the specified index     

        }

We have prepared a sample in WP8 platform and please find the sample from the following location:

Sample Link: PhoneApp3.zip

Kindly let us know if you require any further assistance.

Regards,

Jayapradha



JS Jayapradha S Syncfusion Team April 13, 2015 12:56 PM UTC

Hi Nedal,

 

Please ignore our previous update.

 

You can get the cellvalue from SelectionChanging Event using GetPropertyAccessProvider() as shown in the below code snippet.

 

Code Snippet:

  void sfGrid_SelectionChanging(object sender, GridSelectionChangingEventArgs e)

        {

            var record = sfgrid.GetRecordAtRowIndex(sfgrid.SelectionController.CurrentCellManager.CurrentRowColumnIndex.RowIndex);

            if (record == null)

                return;

            var colIndex = sfgrid.ResolveToGridVisibleColumnIndex(sfgrid.SelectionController.CurrentCellManager.CurrentRowColumnIndex.ColumnIndex);

            if (colIndex >= 0)

            {

                var column = sfgrid.Columns[colIndex];

                var cellValue = sfgrid.View.GetPropertyAccessProvider().GetValue(record, column.MappingName);

            }

        }

 

Please find the modified sample from the below location,

 

Sample Link:  PhoneApp3.zip

 

Please refer the below KB document to get the cell value:  http://www.syncfusion.com/kb/3011/how-to-get-information-from-the-selected-cells-when-using-cell-selection

 

Kindly let us know if you require any further assistance on this.

 

Regards,
Jayapradha



NQ Nedal Qunibi April 13, 2015 03:10 PM UTC

Dear Jayapradha,

I have tested your code , but it's seems it is giving null data

I have send you my test apps , hope I am not pushing too much , but this step is (getting the Cell value from the grid) is my main thing in my Project, everything is depend on it.

thanks for your effort man.



Attachment: TestApp_7cd64035.rar


JS Jayapradha S Syncfusion Team April 15, 2015 02:55 AM UTC


Hi Nedal,

Thank you for your update.

We have analyzed your requirement with sample provided by you. In that sample, the selected record is type of List<string> and so you can get the cellvalue based on the selected index as shown in the below code snippet,

Code Snippet:

void datagrid_SelectionChanging(object sender, GridSelectionChangingEventArgs e)

{

var record = datagrid.GetRecordAtRowIndex(datagrid.SelectionController.CurrentCellManager.CurrentRowColumnIndex.RowIndex);


if (record == null)

return;


var recordItems = record as List<string>;



var colIndex = datagrid.ResolveToGridVisibleColumnIndex(datagrid.SelectionController.CurrentCellManager.CurrentRowColumnIndex.ColumnIndex);



if (colIndex >= 0)

{

var cellValue = recordItems[colIndex];

Console.WriteLine(cellValue);

//var column = datagrid.Columns[colIndex];

//var cellValue = datagrid.View.GetPropertyAccessProvider().GetValue(record, column.MappingName);

}


}



Please let us know if this solution helps you.

Regards,
Jayapradha


JS Jayapradha S Syncfusion Team April 15, 2015 02:57 AM UTC



Hi Nedal,

Thank you for your update.

We have analyzed your requirement with sample provided by you. In that sample, the selected record is type of List<string> and so you can get the cellvalue based on the selected index as shown in the below code snippet,

Code Snippet:

void datagrid_SelectionChanging(object sender, GridSelectionChangingEventArgs e)

{

var record = datagrid.GetRecordAtRowIndex(datagrid.SelectionController.CurrentCellManager.CurrentRowColumnIndex.RowIndex);


if (record == null)

return;


var recordItems = record as List<string>;



var colIndex = datagrid.ResolveToGridVisibleColumnIndex(datagrid.SelectionController.CurrentCellManager.CurrentRowColumnIndex.ColumnIndex);



if (colIndex >= 0)

{

var cellValue = recordItems[colIndex];

Console.WriteLine(cellValue);

//var column = datagrid.Columns[colIndex];

//var cellValue = datagrid.View.GetPropertyAccessProvider().GetValue(record, column.MappingName);

}


}



Please let us know if this solution helps you.

Regards,
Jayapradha



NQ Nedal Qunibi April 15, 2015 06:16 AM UTC

dear Jayapradha,

thanks a lot man , this is just working fine and give the objective result.



JS Jayapradha S Syncfusion Team April 15, 2015 06:22 AM UTC

Hi Nedal,

 

Thank you for your update.

 

Please let us know if you have any other queries.


Regards,

Jayapradha


Loader.
Live Chat Icon For mobile
Up arrow icon