简体   繁体   中英

Get data from datagrid C# Entity Framework WPF

I've a datagrid in WPF, I autogenerate the columns and rows from an sql data entity.

I've tried several different ways but unable to get a specific column or row data,

for eg: When the user selects a row, I'd like to be able to get a single cell's data and use it in my LINQ query.

Please let me know if you have any questions.

Thanks

You have to cast the selected row as an item of the same type as your data class.

Example:

Drop a data grid on your form. call it 'grdMyData', drop a button on your form call it 'btnGetData'

Create a class in your application to hold your data, in my case:

    public class channel
    {
      public int id { get; set; }
      public string channelName { get; set; }
    }

Create your self a list of test data (Note this could just as easy come from an entity model, I'm just using this for illustration.)

    public List<channel> testData = new List<channel>
    {
      new channel {id = 1, channelName = "BBC1"},
      new channel {id = 2, channelName = "BBC2"},
      new channel {id = 3, channelName = "ITV1"},
      new channel {id = 4, channelName = "Channel 4"},
      new channel {id = 5, channelName = "Channel 5"}
    };

In your button click handler, bind your data to your grid...

    grdMyData.AutoGenerateColumns = true;
    grdMyData.ItemsSource = testData;

In your grid 'SelectionChanged' event add the following:

    if (grdMyData.SelectedIndex != -1)
    {
      channel selecteditem = (channel) grdMyData.Items[grdMyData.SelectedIndex];
      MessageBox.Show(selecteditem.channelName);
    }

You use 'SelectedIndex' to find the row that was clicked, and use that as an index into the Items array. You then cast that item as an object of your class type, and access the properties as normal.

The -1 check is needed so that it doesn't fire when drawing / filling the grid, if no actual rows are clicked the index will always be -1.

UPDATE

It seems that the OP is a little confused as to the way objects are used here, so based on the comments I'm going to attempt to clear things up a little:

Where I use "channel" above that's what I've named my object that holds the data I'm populating the grid with, for other purposes this will be an object that is specific to that purpose, EG: if your showing a list of users you may have a "user" object like so:

    public class user
    {
      public string firstName { get; set; }
      public string lastName { get; set; }
      public int age { get; set; }
    }

If you where then to get a row of data from your data grid, rather than:

      channel selecteditem = (channel) grdMyData.Items[grdMyData.SelectedIndex];

you would use:

      user selecteditem = (user) grdMyData.Items[grdMyData.SelectedIndex];

populating the grid in the first place would be similar, but instead of a list of channels, created statically as my example shows (Which was just for demo purposes) you might get that from a database:

    var myUsers = from p in mydb.userstable
                  select p;

    grdMyData.ItemSource = myUssers;

The important thing to remember is that what ever you put into the datagrid has a direct one-to-one mapping to the row you pull out when using the selected data.

In reality, rather than pull directly from your database as I've just done, you may be more likely to loop over that collection and put custom objects into your own object collection, because if your creating everything automatically then you've chance not to know the layout of the object, and may even have other items in there that you don't want to display.

since you mentioned you have only selected items in your grid then I can assume that your filtering only those out from your database retrieval, so it's probably better if you create a class to model just those items, then populate a list of those classes, and give that list to the datagrid.

Then use your own custom class in place of channel/user/whatever in the examples above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM