Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Change the style of cell within datagrid RSS

17 replies

Last post Sep 23, 2011 12:30 PM by Andy Chen

(0)
  • siomi

    siomi

    Member

    2 Points

    3 Posts

    Change the style of cell within datagrid

    Oct 27, 2008 09:03 AM | LINK

    Hi, as mentioned in the title, is anyone know how to access and obtain the properties of any single cell within the datagrid and change its style base on certain conditions, says if the content value is greater than zero?

    I am very new to Silverlight, is there any documentation for me to know all the members of datagrid control?

     

    Thanks in advance!

  • WadeFlextronics

    WadeFlextronics

    Member

    377 Points

    82 Posts

    Re: Change the style of cell within datagrid

    Oct 27, 2008 10:56 AM | LINK

    Here is a starting point on the datagrid cell:

    http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridcell(VS.95).aspx

    Notice the different templates that tie back to the Visual State Manger.  Also look at this link:

    http://msdn.microsoft.com/en-us/library/cc278066(VS.95).aspx for datagrid styles and templates.

     

    (Please mark as Answered if this helps!)   [H]

    (Please mark as Answered if this helps!) Cool

    Thanks,
    Wade
  • siomi

    siomi

    Member

    2 Points

    3 Posts

    Re: Change the style of cell within datagrid

    Oct 27, 2008 11:26 AM | LINK

     Thanks for reply, WadeFlextronics, do you have any sample code to change the properties of datagrid cell?

    for example, change the foreground color of a datagrid cell which is located on column 2 and row 2 to blue color?


  • Amanda Wang - MSFT

    Amanda Wang...

    All-Star

    17236 Points

    1466 Posts

    Microsoft

    Re: Change the style of cell within datagrid

    Oct 29, 2008 06:15 AM | LINK

    Hi,

    You can try to refer the below code the  change the datagrid's column 2 and row 2's font color to blue.

     private void peopleDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
            {
                if (e.Row.GetIndex() == 2)
                {
                    this.peopleDataGrid.SelectedIndex = e.Row.GetIndex();
                    DataGridColumn column = this.peopleDataGrid.Columns[1];
                    FrameworkElement fe = column.GetCellContent(e.Row);
                    FrameworkElement result = GetParent(fe, typeof(DataGridCell));
                    if (result != null)
                    {
                        DataGridCell cell = (DataGridCell)result;

                        cell.Foreground = new SolidColorBrush(Colors.Blue);
                    }
                }
            }

            private FrameworkElement GetParent(FrameworkElement child, Type targetType)
            {
                object parent = child.Parent;
                if (parent != null)
                {
                    if (parent.GetType() == targetType)
                    {
                        return (FrameworkElement)parent;
                    }
                    else
                    {
                        return GetParent((FrameworkElement)parent, targetType);
                    }
                }
                return null;
            }

    Amanda Wang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • siomi

    siomi

    Member

    2 Points

    3 Posts

    Re: Change the style of cell within datagrid

    Oct 29, 2008 08:22 AM | LINK

    Amanda Wang - MSFT

    ....

     private void peopleDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
            {
                if (e.Row.GetIndex() == 2)
                {
                    this.peopleDataGrid.SelectedIndex = e.Row.GetIndex();
                    DataGridColumn column = this.peopleDataGrid.Columns[1];
                    FrameworkElement fe = column.GetCellContent(e.Row);
                    FrameworkElement result = GetParent(fe, typeof(DataGridCell));

    ....

     

    Thanks for your help, Amanda! Basically, can you recommend me in learning datagrid? i had been working so much but found it very much different compare to gridview in ASP.NET ...

    As for this case, the key of code is "e.Row", is it possible to obtain the row of datagrid like "peopleDataGrid.Row"? because i usually will not going to raise an event to obtain the "e.Row".

     

  • Amanda Wang - MSFT

    Amanda Wang...

    All-Star

    17236 Points

    1466 Posts

    Microsoft

    Re: Change the style of cell within datagrid

    Oct 29, 2008 11:55 AM | LINK

    Hi,

    siomi

    Basically, can you recommend me in learning datagrid? i had been working so much but found it very much different compare to gridview in ASP.NET ...

    Unfortunately, there is no much resource on learning the datagrid. I learn it just from the SDK and the internet. You can try to search on this forum, there are many excellent threads on the DataGrid control, which can be helpful. 

    siomi

    As for this case, the key of code is "e.Row", is it possible to obtain the row of datagrid like "peopleDataGrid.Row"? because i usually will not going to raise an event to obtain the "e.Row".

    In fact, there are many differences between the ASP.NET DataGrid Control and the SilverLight Control. The Silverlight DataGrid does not have the Rows property.

     If you want to retrieval the data of a row, you can try to refer the below code:

    DataGridColumn column= peopleDataGrid.Columns[0];

    FrameworkElement fe= peopleDataGrid.GetCellContent(people[1]);//people is a collection to which Datagrid is bound

    The above code will get you the Content of 1st column of 2nd row.

     

    Amanda Wang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • mr.saif

    mr.saif

    Member

    438 Points

    165 Posts

    Re: Change the style of cell within datagrid

    Oct 29, 2008 12:31 PM | LINK

    Amanda Wang - MSFT

    Hi,

    You can try to refer the below code the  change the datagrid's column 2 and row 2's font color to blue.

     private void peopleDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
            {
                if (e.Row.GetIndex() == 2)
                {
                    this.peopleDataGrid.SelectedIndex = e.Row.GetIndex();
                    DataGridColumn column = this.peopleDataGrid.Columns[1];
                    FrameworkElement fe = column.GetCellContent(e.Row);
                    FrameworkElement result = GetParent(fe, typeof(DataGridCell));
                    if (result != null)
                    {
                        DataGridCell cell = (DataGridCell)result;

                        cell.Foreground = new SolidColorBrush(Colors.Blue);
                    }
                }
            }

            private FrameworkElement GetParent(FrameworkElement child, Type targetType)
            {
                object parent = child.Parent;
                if (parent != null)
                {
                    if (parent.GetType() == targetType)
                    {
                        return (FrameworkElement)parent;
                    }
                    else
                    {
                        return GetParent((FrameworkElement)parent, targetType);
                    }
                }
                return null;
            }

     

    very nice [:)] it helped me too. thanks Amanda Wang

    Regards,
    Muhammad Saifullah
  • SilverlightOIS

    SilverlightOIS

    Member

    2 Points

    1 Post

    Re: Change the style of cell within datagrid

    Dec 03, 2008 04:12 PM | LINK

      Hello Amanda Wang!

      I have read your code and I have used it.It is very good!

      But,with your code,I can only change the Foreground of a Cell and I can't change the Background of a Cell.

      So,can you show me the way which I can use to change the Background of a Cell ?

      I wrote :  cell.Background = new SolidColorBrush(Colors.Red);

      The code line runs but the Background isn't changed !

     Thank you very much !

                      SilverlightOIS

  • hushaoming

    hushaoming

    Member

    4 Points

    9 Posts

    Re: Re: Change the style of cell within datagrid

    Jan 06, 2009 08:23 AM | LINK

    When the data  in cell is changed, I need change Foreground again, How can I do this?

    Ask question again in  http://silverlight.net/forums/p/63281/156483.aspx

     

  • Terian

    Terian

    Member

    4 Points

    2 Posts

    Re: Re: Change the style of cell within datagrid

    Feb 12, 2009 02:22 PM | LINK

     Hello,

    I am in a similar situation, whereby I need to style a datagrid cell based on it's contents, either altering the foreground/background based on the value of a 'Decimal' field, setting the justification for date fields and making the date display as a short date and setting colour and justification on text fields.

    The datagrid is bound to a data table and is dynamically created from various SQL calls (being a 'common' datagrid).

    The contents of e.row are a datarow.rowview and contain the row details as expected.

    The problem is, with the above code,  the following line always returns a null, as does any attempt to get the cellcontent

    FrameworkElement fe = column.GetCellContent(e.Row);

     If I apply  a 'restyle' via a function from a button click, it works once, but then crashes if I try to perform the restyle again. It also loses the styling if the datagrid is sortged by clicking on the columnheader and any attempt to re-apply the style fails. Another stange action is, once the grid has been dynamically created, if I then call the function to apply the style, the grid is dislayed *without* the modified style. The button on my form calls the exact same function and does nothing else, but this *does* apply the style. It almost seems to me that though the grid is built, because it has not been displayed, the style isn't "known".

     If there is a way code wise, or somehow via a style on the datagridcell which could react to the column datatype to call an approriate converter, I'd be very grateful for some pointers as countless hours searching the web has not revealed any answers.

    I am presuming this is actually do-able of course as unfortunately most of the example I find relating to converters and styling assume the datagrid has the columns hard-set in the XAML.  Given that a grid where the explicit column is affected by a converter retains it's styling when the grid is resorted on different columns, I am hoping it is possible.