WPF LINQ Query on DataGrid

Page 1 of 1

5 Replies - 8669 Views - Last Post: 28 June 2013 - 06:11 AM Rate Topic: -----

#1 optix212  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 30
  • View blog
  • Posts: 540
  • Joined: 10-October 09

WPF LINQ Query on DataGrid

Posted 27 June 2013 - 07:49 AM

I've been trying to figure this out for the last 30 minutes, and it seems as if I've looked through every single event, property, and method for DataGrid... My problem is that I can't loop through a datagrid's rows to get the value of a cell.. First off, I have to use dataGrid.Items instead of dataGrid.Rows.. This makes the code look a bit ambiguous to me, since it's not defining if I am accessing a Row or a Cell (I know I am accessing a row and not a cell, but if someone else were to read my code, would they?).. But I guess I'll have to live with that. The most annoying part is that once I found out how to access a row, I have no idea how to access a Cell in that row. In my old WinForms program I used DataGridView. What I'm trying to do is get the WPF DataGrid equivalent of this LINQ Query:

            var accountSearchQuery =
            from DataGridViewRow row in accountsDataGridView.Rows
            where (row.Cells[1].Value != null) && (row.Cells[column].Value.ToString() == searchValue)
            select row;



Is This A Good Question/Topic? 0
  • +

Replies To: WPF LINQ Query on DataGrid

#2 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: WPF LINQ Query on DataGrid

Posted 27 June 2013 - 08:28 AM

I'm sure your datagrid had to get its data from some place. Why not search your data source rather than the UI component that displays your data?
Was This Post Helpful? 0
  • +
  • -

#3 optix212  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 30
  • View blog
  • Posts: 540
  • Joined: 10-October 09

Re: WPF LINQ Query on DataGrid

Posted 27 June 2013 - 12:24 PM

Well, it's for a searchbar that allows the user to search for values within the datagrid.. If that value returns a match to what the user entered in the searchbar, then select the corresponding row on the datagrid.. That's why I wouldn't want to search the DataSource. Unless there is maybe a way to select a row on the DataGrid via the DataSource?
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: WPF LINQ Query on DataGrid

Posted 27 June 2013 - 12:57 PM

I'm not suggesting that you actually use the DataSource property, but you should have some source where that data is coming from.

DataGrids are for display; that's all they should do. Keep the data in memory in some data structure like a list and bind it to the grid. When you need to filter, filter the original source into a new list (or IEnumerable using LINQ), and bind the result of the filtering to the grid. When you want to clear the filter, re-bind the original list.

That way, you're not trying to use UI components for things they're not made to do.
Was This Post Helpful? 1
  • +
  • -

#5 optix212  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 30
  • View blog
  • Posts: 540
  • Joined: 10-October 09

Re: WPF LINQ Query on DataGrid

Posted 28 June 2013 - 05:20 AM

Okay. I think I get it. Whenever a user does a search, create a new <List> of the results, clear the DataGrid, and return only the list results onto the DG?

What if I don't want to clear the datagrid, though? For instance, if I just wanted to keep all records on the DG at the same time, and multiselect the matching ones? Either way, I see how this would work and it would provide a solution to my problem although it's not exactly what I wanted..

Furthermore, would this be an example of some of the code that Microsoft took out in order to make programmers stick to the MVVM standard? I still don't quite have a grasp on MVVM, any recommended articles to read to get started on WPF and MVVM?

Sorry so many questions. I feel like a total noob again since I switched to WPF.
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: WPF LINQ Query on DataGrid

Posted 28 June 2013 - 06:11 AM

Quote

would this be an example of some of the code that Microsoft took out in order to make programmers stick to the MVVM standard


No. I'm fairly sure you could do what you're trying to do; I just suggest against it. UI controls aren't meant to be treated as data structures (and that's true for WinForms as well).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1