10

I am trying to obtain DataGridRow from my DataGrid based on index. I am using following code:

public DataGridRow GetGridRow(int index)
{
    DataGridRow row = (DataGridRow)DG_Statement.ItemContainerGenerator.ContainerFromIndex(index);
    if (row == null)
    {
        // May be virtualized, bring into view and try again.
        DG_Statement.UpdateLayout();
        DG_Statement.ScrollIntoView(DG_Statement.Items[index]);
        row = (DataGridRow)DG_Statement.ItemContainerGenerator.ContainerFromIndex(index);
    }
    return row;
}

Ref Link - Get row in datagrid

But unfortunately its returning a null object of DataGridRow. If I check the Items[] property of my grid I could see 13 items.

Need suggestion on how to obtain the Grid Row as I want to change color of top 2 and bottom 2 rows of my data grid.

Any help is appreciated. Thanks!!

Adding Screenshot of DataGrid Items

enter image description here

Important Update

If I call GetGridRow() from the SelectedIndexChanged Event of the Grid it works flawlessly.

On the other hand, if I call it after I construct the object of the page on which my grid is displayed it returns row object as NULL.

Community
  • 1
  • 1
Nilesh Barai
  • 1,312
  • 8
  • 22
  • 48
  • Where does this method live? Code behind or in a view model? – Mike Schwartz Jan 28 '14 at 18:06
  • This was already asked [here](http://stackoverflow.com/a/8464756/2458971) on Stack overflow. – AR5HAM Jan 28 '14 at 18:11
  • I have provided this link in the reference and using the code suggested in that post. But I am getting null object of Row that's where I am stuck. – Nilesh Barai Jan 28 '14 at 18:12
  • Sounds like at that point its not been initialized. – Mike Schwartz Jan 28 '14 at 19:10
  • @MikeSchwartz: can you please suggest any workaround for achieving this? – Nilesh Barai Jan 28 '14 at 19:12
  • The issue is that when the grid is loaded there is not yet a selectedindex. I'm not totally sure of all your logic. Are you changing the first 2 and last 2 rows color based on the selected index? – Mike Schwartz Jan 28 '14 at 19:43
  • You can get the count of the DataGrid in the Loaded event. – Mike Schwartz Jan 28 '14 at 19:50
  • well the datagrid count is 13 as I have posted the screenshot for the same. I cannot use selected index property for my cause as strictly I want to access 1st 2 and last 2 rows of the Grid. Also in my grid the number of rows will always be >= 4. So no question of datatable associated with the grid is empty!! – Nilesh Barai Jan 29 '14 at 04:07

2 Answers2

9

So if its in the code behind. You can just get the selected index of the DataGrid. I've named the datagrid dataGrid as an example.

var rowIndex = dataGrid.SelectedIndex;

var row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(selectedIndex);
Mike Schwartz
  • 2,182
  • 1
  • 17
  • 26
0

Check to make sure the index you're passing in is actually within bounds.

Leeland Clay
  • 112
  • 1
  • 11
  • thats exactly I am doing in the code that I have posted above. I am passing index = 0. Total items in the grid are 13 so its within bounds. Also If row == null then UpdateLayout() is called first and later to that ContainerFromIndex() is called. I am using a DataTable view as my itemsource. Will it cause any problem? – Nilesh Barai Jan 28 '14 at 18:09
  • Yes, I realized you were calling it again right after I clicked Add, so I edited it to try and remove that reference before anyone noticed my blonde moment :). – Leeland Clay Jan 28 '14 at 18:19
  • Where does the call to the GetGridRow happen? You may be calling it before the grid actually finishes loading all of the data. Because this happens on a thread, it may be getting populated after the call, but before you check the grid.Items count. – Leeland Clay Jan 28 '14 at 18:28
  • Added a screenshot for DataGrid Items count. – Nilesh Barai Jan 28 '14 at 18:39
  • Try calling: var row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(dataGrid.Items[index]); and see what that gives you – Leeland Clay Jan 28 '14 at 18:47
  • Also, is there a reason for highlighting just the first and last two rows? There may be another way to do what you're attempting to do. – Leeland Clay Jan 28 '14 at 18:59
  • well I am merging 3 data tables into 1 and displaying the entire thing on the datagrid. 1st table is 1st 2 rows and 3rd table is last 2 rows. These are special cases and I want those to be highlighted. :) – Nilesh Barai Jan 28 '14 at 19:02
  • tried the line of code which you have asked in the previous comment but no luck!! :( I think CLR is messing up somewhere in the background thread where it sets the ItemSource for the Grid :( – Nilesh Barai Jan 28 '14 at 19:10
  • If that's what you're doing, I'd probably do grouping instead. That way you can apply a style to the group and not have to fight with the ItemsGenerator. Also, another option is if you have a column in the table that can differentiate the 3 datatables, you can setup a style trigger to apply the background to just those rows. Would either of those help? – Leeland Clay Jan 28 '14 at 20:13
  • Well the second option you stated looks promising. Can you please provide some reference code which I can use to apply style trigger? I think we will have to edit the data template but not completely sure!! – Nilesh Barai Jan 29 '14 at 04:02
  • 1
    @NileshBarai The example I used when I needed help was at http://stackoverflow.com/questions/5549617/change-datagrid-cell-colour-based-on-values – Leeland Clay Jan 29 '14 at 15:59
  • Thanks a lot for giving good pointer!! I referred this link http://stackoverflow.com/questions/7381726/datagrid-row-background-based-on-cell-value and my issue got solved :) – Nilesh Barai Jan 29 '14 at 19:34