1

I have a GridView with multiple items (10 items bind from List). And (for simple example) lets I have a text field and button. In text field I'll type number from 0 to 9. When I click on a button I want to change background of item with index typed in text field. So when I type 0 i want first item to change background etc etc. I have a global list and I can get object from that list but I don't know how to change background of this item on gridview

Fixus
  • 4,631
  • 10
  • 38
  • 67

2 Answers2

1

You can have a DataTemplate for that different item (which changes only it's background). Then, using a TemplateSelector you can establish that you want to apply that template only to the item whose number match the number binded to the TextBox.

public class GridViewDataTemplateSelector : DataTemplateSelector
{
    public override DataTemplate
        SelectTemplate(object item, DependencyObject container)
    {
        FrameworkElement element = container as FrameworkElement;

        if (element != null && item != null && item is GridViewRow)
        {
            GridViewRow  rowitem = item as GridViewRow;

            // Here's where you compare with actual selected number (change 1 with the method call to obtain it.
            if (GridViewRow.RowIndex == 1) 
                return
                    element.FindResource("SpecialBackgroundRowTemplate") as DataTemplate;
            else 
                return
                    element.FindResource("NormalBackgroundRowTemplate") as DataTemplate;
        }

        return null;
    }
}

Here SpecialBackgroundRowTemplate and NormalBackgroundRowTemplate are the DataTemplates where you will set the normal background and the focused background for the given grid rows.

Erre Efe
  • 15,387
  • 10
  • 45
  • 77
  • Yep, you can. Once the user changes the value on the textbox, then the number binded will change and then the template selector will change the DataTemplate of the respective selected row and then it's background color will change. It's technical easy but will take you a time to code. – Erre Efe Sep 25 '12 at 17:18
  • Ok but how to invoke the template selector ? Can you show me some code ? – Fixus Sep 25 '12 at 20:27
  • My main problem is how to set style for selected item ? I implemented template selector but I can only get it working for whole Collection. I don`t know how to invoke it for one item after the View is generated – Fixus Sep 25 '12 at 20:33
  • As I wrote, I have already created my DataTemplateSelector but I don`t know how to invoke it after the click on the button. I can easy change background while rendering first time my view. But when the GridView is created I don`t know how to invoke the DataTemplateSelector after clicking on a button to change the background – Fixus Sep 26 '12 at 05:40
  • 1
    @Fixus Then you need to reapply the selector when the property change as in here: http://stackoverflow.com/questions/3543515/wpf-reapply-datatemplateselector-when-a-certain-value-changes – Erre Efe Sep 26 '12 at 12:44
  • thanks for help and I' sorry but I don't get it. Should be there some invoke from click event to some action that repaits/reloads the Grid ? – Fixus Sep 26 '12 at 14:11
  • @Fixus Nop. That's precisely what the DataTrigger on the linked example do. Once the property change it changes the DataTemplate also, when this DataTemplate changes it will change the background of your Row and will do the repaint for your. – Erre Efe Sep 26 '12 at 14:32
0

You can define DataGridViewCellStyle object and set it's properties, then call any cell from your datagridview -

DataGridViewCellStyle dgvs = new DataGridViewCellStyle();   
dgvs.BackColor = Color.Red;
yourDGV.Rows[0].Cells[0].Style = dgvs;
mihail
  • 2,173
  • 19
  • 31