Home > Silverlight > Silverlight DataGrid Issues

Silverlight DataGrid Issues

I was eager to see how the DataGrid has changed, as the ControlTemplates have changed for the better.

I am not sure if I am doing something wrong but, here are the things I found

Here is the source
1. Here is a screenshot of the DataGrid when we run the application

scroll up and down a few times,Here is what it looks like after

see the difference, ContactTitle column increased its width and Companyname is pushed out, Similar thing happens With Horizontal Scrolling also

2. Sometimes we want to highlight the Rows when a condition is met. I thought LoadingRow event is a good place to check my condition and change the background of the row

so I added the following code

Customer c = e.Row.DataContext as Customer;

if (c.City == “London”)           
   e.Row.Background = new SolidColorBrush(Colors.Yellow);  

it works and we can see the Rows highlighted

Scroll up and down few times and we get

How come just scrolling up and down gives me this, I see LoadingRow event is being raised

3. I thought, may be try  changing the size when the condition is met, so added following code

FrameworkElement ele = grid1.Columns[2].GetCellContent(e.Row);
(ele as TextBlock).FontSize = 20;            

After I added this code, I cannot see all the records

Here is what I have before changing size, the Id’s start with W’s at the end

After changing size, I cannot see these records, I get this, the only way to see the rest of them is to use down keys

Please leave a comment if you have any Idea on what is going on, Thanks

 

Update: looks like a bug and will be fixed by RTM

To get around these issues we have to specify fixed with for the columns, reset the color if the condition is not met.

Categories: Silverlight
  1. gary
    June 9, 2008 at 3:49 pm | #1

    Try turning off virtualization. Inasmuch as the problems seem to happen after scrolling it seems likely that there are problems with virtualizing the rows. Just a thought.

    -Gary

  2. lee
    June 9, 2008 at 4:22 pm | #2

    I know how to turn it off in WPF, how to do it in silverlight?- Thanks, lee

  3. gary
    June 9, 2008 at 10:45 pm | #3

    Dang. Good question. I assumed that there would be a way to turn it off, but I can’t find anything either.

    I think you’ve got some interesting bugs here. I still suspect virtualization. You should probably post this on the Silverlight forum.

    -Gary

  4. lee
    June 10, 2008 at 12:26 am | #4

    Some one from microsoft said it is bug and will be fixed by RTM in the forum

  5. June 15, 2008 at 9:16 pm | #5

    Hi Lee,

    Issue #1 Seems like an intentional behavior.
    Issue #3 does seems like a Scrolling bug.

    Issue #2 is caused by the virtualzation modal in the DataGrid. You can use the “DataGrid.UnloadingRow” event to clear all custom display settings you’ve set on the row after it is no longer being shown.

  6. lee
    June 16, 2008 at 7:04 am | #6

    justin,
    1. you are right, staying away from Autogenerate columns(which wont be ideal in production application anyways) should fix the first one.

    2.Virtualization or not, I think developer should not be responsible for clearing custom settings, so I would say this is also a bug along with 3.

    Thanks

  7. yif
    August 7, 2008 at 7:57 pm | #7

    #1 The column growing is not a bug. By default, columns are set to Auto, and they will auto grow based on the content.

    #2 This is not a bug either. Because of container recycling, the row gets reused for other items so the user needs to clean up any customizations from the Loading event in the UnLoading event. However, the recommended way to do this is to DataBind the customizations instead. In this example the Background could be bound to a calculated property of the item that returns a yellow brush if the City is London.

    #3 That’s a Beta2 bug where the scrollbar wasn’t updated in that case. If you navigate with the keyboard, you should be able to navigate to all of the records

  8. August 19, 2008 at 2:06 pm | #8

    Hey,

    I’ve been having all sorts of issues with the DataGrid so glad to find some others that are too. My issue is that I’ve get a datagrid bound to an observable collection, I add a new item to it and then try to ScrollIntoView(myNewItem). This causes some very bizarre rendering issues – with the black column text being rendered all over the top left corner of the control.

    I need to work around this right now because it’s released tomorrow and this is the only issue left.

    Lee

  9. lee
    August 20, 2008 at 12:07 am | #9

    Can you send a sample project to reproduce the issue?

  10. Ananya
    September 8, 2008 at 1:45 pm | #10

    How to get rid of the extra column in extreme right in datagrid?

  11. lee
    September 8, 2008 at 2:07 pm | #11

    there is no direct way, AFAIK, there might be a way through code. I did not try it myself. will post if I find a way

    Thanks

  12. lee
    September 8, 2008 at 2:30 pm | #12

    you could try to increase the last column width in code

  13. sladapter
    September 18, 2008 at 4:15 pm | #13

    For bug #2, I think yif’s sugesstion make sense. Use DataBinding to do this instead of using loaded event.

    But I don’t think you need to add another Brush field to your data object, just bind the Background to the Data field that you want to check. Use a IValueConverter to return you a SolidColorBrush based on the data for that row. This is much cleaner way then using loaded event.

    With databinding, after you changed data for that row, the color is automatically changed accordingly. With loaded event, you do not get this effect either.

  14. lee
    September 18, 2008 at 4:48 pm | #14

    I agree converter would work, if there is a DataTrigger that would work the best instead. Changing the objects to add properties(for binding) might not be possible in all the cases

  15. Ananya
    October 5, 2008 at 9:54 am | #15

    Hi,

    I have following code inside my datagrid

    Now if someone want to edit the employeename, they can edit by double clicking. I want to change color of column after edit.
    How can I achieve this. I guess I need to handle this inside dgEmployees_CommittingEdit, but how?

  16. Ananya
    October 5, 2008 at 9:57 am | #16

    here is my XAML code :

  17. lee
    October 5, 2008 at 11:27 am | #17

    Ananya, I cannot see the xaml you have

  18. Ananya
    October 5, 2008 at 8:29 pm | #18

    Don’t know why my XAML is not appearing.
    I have used DataGridTemplateColumn.CellTemplate to display data and DataGridTemplateColumn.CellEditingTemplate for editing.
    It allows to edit on double click. I need to change background color of column after edit is complete on that column.

  19. lee
    October 5, 2008 at 11:09 pm | #19

    Ananya,
    Here is an Idea, you could do something along those lines
    http://leeontech.wordpress.com/2008/10/05/changing-foreground-of-cells-that-are-modified-in-datagrid/

  20. October 29, 2008 at 7:26 pm | #20

    Quite sad… but it seems that DataGrid + A good number of elements + Scroll Bar is not a very good combination…

    Dirty hacks that I have found..

    –> Insert elements at the top of the list / DataGrid.
    –> Do not trust on the Add + DataGrid + Scroll… rebind the DataGrid (quite ugly hack).
    –> If you want to rebind a grid and have problemas with the scroll bar… remove the DataGrid and create a copy dynamically (will try to recreate the popups dynamically better).

    I have several threads opened commeting this issues, waiting for Ms or a good chap to help.

    http://silverlight.net/forums/p/43791/120477.aspx#120477

    http://silverlight.net/forums/p/43792/120473.aspx#120473

  21. lee
    October 29, 2008 at 7:32 pm | #21

    Braulio,
    I agree with you, completely

  22. KimJungSuk
    February 12, 2009 at 5:37 am | #22

    You’re Korean?
    Very interesting posts by you’re.
    I made the tree DataGrid have a problems Scroll Up and Down.
    Did you solve the scrolling problem?
    If the sample you have to tell me.
    Thank you.
    감사합니다 .^^

    • lee
      February 12, 2009 at 10:34 am | #23

      Hi Kim,
      I am not korean. I am not sure what you mean by tree datagrid. can you send me a sample project. I can take a look

      • Ashok
        August 20, 2009 at 6:31 pm | #24

        Lee,

        Are you from India?

        Ashok

  23. evox
    June 29, 2009 at 3:34 pm | #25

    Hi, does someone know how can I color only one cell if it cell has ‘-’?

    • lee
      June 29, 2009 at 3:38 pm | #26

      you can specify a converter and based on the value change the color of the cell

  1. June 10, 2008 at 9:29 pm | #1