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

Get Data by Index

5 Answers 46 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jeff
Top achievements
Rank 1
Jeff asked on 14 Jan 2014, 01:08 AM
I want to make it so when I select a row in the GridView, i can get the string inside the first column (or 20th). 

I am currently able to get ALL the information in the whole row with dataGrid1.SelectedItem.ToString(); and I could just chop that information up by a series of "Substring" operations, but there has to be a better way!

Getting this cell data by index (an integer 0-19) would be okay.

Getting the cell data by column header name would be okay as well.

Is this possible?

5 Answers, 1 is accepted

Sort by
0
Jeff
Top achievements
Rank 1
answered on 14 Jan 2014, 01:28 AM
   var query =
            from person in dataEntities.cs_people
            orderby person.personId
            select new { person.personId, person.Name, person.Address, person.Age };

            dataGrid1.ItemsSource = query.ToList();


That is how I bound data to my dataGrid1...

So, each row is equal to something like { personId = 456218, Name = Jennifer, Address = 1213 Fake St., Age = 21 }
and the RadGridView populates the visual aspect of this perfectly.

I tried accessing personId field in a selectedItem with: (dataGrid1.SelectedItem).personId, but that does not work.
0
Hristo
Telerik team
answered on 14 Jan 2014, 09:11 AM
Hello Jeff,

In order to get a property of the selected item, you would have to specify the type of the selected item.
For example the following expression based on your code var person = (dataGrid1.SelectedItem as Person (the name the class holding person data) ).personId should fix your issue.

You can read more about item selection in our onlinde documentation located here.

Regards,
Hristo
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Jeff
Top achievements
Rank 1
answered on 14 Jan 2014, 04:42 PM
Thanks for the reply, Hiristo!

If I created a class like:

public class Person {
public string personId;
public string Name;
public string Address;
public string Age;
}
 
How would I get my database information into the RadGridView as a "Person"? Right now, I have the information going in as an "IEnumerable", then casting it as a list for dataGrid1.Itemsource.

I tried:
var query =
     from person in dataEntities.cs_people
     orderby person.personId
     select new Person { personId = person.personId, Name = person.Name, Address = person.Address, Age = person.Age };
 
     dataGrid1.ItemsSource = query.ToList();

However, it does not appear that ItemSource can take a list like that.

Many thanks in advance!
0
Jeff
Top achievements
Rank 1
answered on 14 Jan 2014, 04:51 PM
Hmm...

When I call "var person = (dataGrid1.SelectedItem as Person).personId"
it gives a Null Reference Exception.

I know it's not null because I am using the selectedItem elsewhere.
0
Hristo
Telerik team
answered on 17 Jan 2014, 03:18 PM
Hi Jeff,

In your person class you should create public properties to store the information. You can find more information on how to set the properties  in the following msdn article: link.

For a sample Person class please see the following code snippet:
public class Person
{
    public string PersonId {get;set;}
    public string Name {get;set;}
    public string Address {get;set;};
    public string Age {get;set;}
 
    public Person(string PersonId, string Name, string Address, string Age)
    {
        this.PersonId = personId;
        this.Name = Name;
        this.Address = Address;
        this.Age = Age;
    }
}

One possible reason for the Null Reference Exception is that your class doesn't have a constructor which accepts parameters.

You can also find more information on how to load data into RadGridView from different sources on the following link: link.

Regards,
Hristo
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
GridView
Asked by
Jeff
Top achievements
Rank 1
Answers by
Jeff
Top achievements
Rank 1
Hristo
Telerik team
Share this question
or