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.
Unfortunately, activation email could not send to your email. Please try again.

how to capture Cell Value from the Datagrid

Thread ID:

Created:

Updated:

Platform:

Replies:

118762 Apr 8,2015 07:57 AM Apr 15,2015 02:22 AM Windows Phone 11
loading
Tags: SfDataGrid
Nedal Qunibi
Asked On April 8, 2015 07:57 AM

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.


Jayapradha S [Syncfusion]
Replied On April 9, 2015 11:39 AM

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

Sivakumar R [Syncfusion]
Replied On April 9, 2015 02:10 PM

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



Nedal Qunibi
Replied On April 12, 2015 05:04 AM

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



Nedal Qunibi
Replied On April 12, 2015 05:58 AM

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.

Jayapradha S [Syncfusion]
Replied On April 13, 2015 07:40 AM

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


Jayapradha S [Syncfusion]
Replied On April 13, 2015 08:56 AM

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


Nedal Qunibi
Replied On April 13, 2015 11:10 AM

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

Jayapradha S [Syncfusion]
Replied On April 14, 2015 10:55 PM


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

Jayapradha S [Syncfusion]
Replied On April 14, 2015 10:57 PM



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


Nedal Qunibi
Replied On April 15, 2015 02:16 AM

dear Jayapradha,

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


Jayapradha S [Syncfusion]
Replied On April 15, 2015 02:22 AM

Hi Nedal,

 

Thank you for your update.

 

Please let us know if you have any other queries.


Regards,

Jayapradha


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;