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

[Solved] Reading Cell Values From Selected Row

9 Answers 333 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.
Michael Semanson
Top achievements
Rank 1
Michael Semanson asked on 03 Mar 2010, 09:01 PM
I have a Silverlight RadGridView control populated with data that I need to be able to access the value of a cell when a row is selected.

I'm using the "SelectionChanged" event handler to notify the application that a row has been selected, but I have no idea how to get the value at columnX in the selected row.

How can I get the value I need?

Any help would be greatly appreciated.

Thanks.

9 Answers, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 03 Mar 2010, 09:17 PM
Hello Michael Semanson,

I have prepared a sample project that shows how to do this. Here is how:

using System.Windows.Controls;
using Telerik.Windows.Controls;
using Telerik.Windows.Data;
using System.Diagnostics;
 
namespace TicketID_286812_GettingSelectedRow
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
 
            this.clubsGrid.ItemsSource = Club.GetClubs();
        }
 
        private void RadGridView_SelectionChanged(object sender, SelectionChangeEventArgs e)
        {
            if (e.AddedItems != null && e.AddedItems.Count > 0)
            {
                var selectedClub = e.AddedItems[0] as Club;
                if (selectedClub != null)
                {
                    Debug.WriteLine(selectedClub.Name);
                    Debug.WriteLine(selectedClub.StadiumCapacity);
                    Debug.WriteLine(selectedClub.Established);
                }
            }
        }
    }
}

Please, take a look at the sample project attached and let us know if there are some problems or difficulties.

Best wishes,
Ross
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
Michael Semanson
Top achievements
Rank 1
answered on 04 Mar 2010, 01:46 PM
Ross, thank you for your response.

Your sample code works fine but when I try to implement a similar solution I am unable to get it to work.

Here is my SelectionChanged function:
        private void radGridView_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e) 
        { 
            // Find selected item 
            if (e.AddedItems != null && e.AddedItems.Count > 0) 
            { 
                var selectedRow = e.AddedItems[0] as ASNDetails; 
                if (selectedRow != null) 
                { 
                    var proxy = new CrossDockSvc.CACrossDockClient(); 
 
                    proxy.GetASNDetailsByMasterIDCompleted += new EventHandler<RadGridViewTest.CrossDockSvc.GetASNDetailsByMasterIDCompletedEventArgs>(proxy_GetASNDetailsByMasterIDCompleted); 
 
                    proxy.GetASNMasterSmallAsync(selectedRow.AsnInDetailID); 
 
                } 
            } 
        } 
 
    } 
 

After the line:
var selectedRow = e.AddedItems[0] as ASNDetails, the selectedRow is null although the selected row count is equal to "1" at this point.

What am I missing?


0
Rossen Hristov
Telerik team
answered on 04 Mar 2010, 01:58 PM
Hi Michael Semanson,

If e.AddedItems[0] is not ASNDetails then selectedRow will be null. What does the debugger show? What is e.AddedItems[0]?

Greetings,
Ross
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
Michael Semanson
Top achievements
Rank 1
answered on 04 Mar 2010, 02:20 PM
I was afraid of that.

e.AddedItems[0] is RadGridViewTest.CrossDockSvc.AsnInMasterSmall, which is record from a dataset accessed through a web service.

I've been trying to reference AsnInMasterSmall from the web project but have been unable to figure out how to reference the namespace from the web project.  What is the correct way to access this class in the Silverlight project?

Michael
0
Rossen Hristov
Telerik team
answered on 04 Mar 2010, 02:29 PM
Hello Michael Semanson,

My colleague's blog post may shed some light on this topic.

Also, since this is a huge discussion I would suggest that you check out these resources:

Web Services and DataSets
Using Typed DataSets from Web Services

I hope this helps.

Greetings,
Ross
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
Michael Semanson
Top achievements
Rank 1
answered on 04 Mar 2010, 02:49 PM
Thanks again for your response, but I'm kind of new to this and I'm not sure if any of this applies to what I'm trying to do.

I've already extracted the data from the web service and populated the RadGridView with the data set.

Now all I want to do is retrieve the text value for cell[4] from the selected row after the user clicks on the row.

Isn't there a way just a simple way to retrieve the text value from a specific cell within a selected row (i.e. selectedRow.Column[4].Value.ToString())?

Michael
0
Rossen Hristov
Telerik team
answered on 04 Mar 2010, 03:07 PM
Hello Michael Semanson,

Here is how to work with UI rows and cells instead if data items:

void clubsGrid_SelectionChanged(object sender, SelectionChangeEventArgs e)
{
    // Find selected item
    if (e.AddedItems != null && e.AddedItems.Count > 0)
    {
        var dataItem = e.AddedItems[0];
        if (dataItem != null)
        {
            var selectedUIRow = this.clubsGrid.ItemContainerGenerator.ContainerFromItem(dataItem) as Telerik.Windows.Controls.GridView.GridViewRow;
            if (selectedUIRow != null)
            {
                var secondCell = selectedUIRow.Cells[1] as Telerik.Windows.Controls.GridView.GridViewCell;
                if (secondCell != null)
                {
                    // Get the value from the UI without knowing the type.
                    Debug.WriteLine(secondCell.Value.ToString());
                }
            }
        }
    }
}

Have in mind thought that it is always better to use data items if possible and not rely on UI elements such as rows and cells.

But I guess that in your specific case it will be easier for you to just go with the UI elements. I hope this helps.

Greetings,
Ross
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
Michael Semanson
Top achievements
Rank 1
answered on 04 Mar 2010, 03:29 PM
Thank you!  This is what I was looking for.

I'm not sure if this is the best approach for what I'm trying to do or not, but because this is a proof of concept and I have limited time it seemed to make sense just to grab the text from the selected row.  The end result for me is that I need to pass a value from the selected row to another stored procedure. 

Is there a better and straight forward way to approach this?
0
Accepted
Rossen Hristov
Telerik team
answered on 04 Mar 2010, 04:06 PM
Hi Michael Semanson,

Since you don't know the type of the object (my first sample), then I guess this is your only option.

If you somehow manage to "see" the type of your object, then my first suggestion is better.

But to do that you will have to read the resources I have sent you, since this is really not part of our domain.

Thank you for the understanding.

All the best,
Ross
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
Michael Semanson
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Michael Semanson
Top achievements
Rank 1
Share this question
or