Munim
Munim

Reputation: 2768

Silverlight : SIlverlight app crashes when I set itemssource for data grid

I am developing a silverlight application where you can set sales target for specific kind of product. I have a combobox which has some product types in it like Rice , Tea etc. Once you select a type, all products of that type gets loaded into a datagrid with following info: Product Name, Session(this is the time for which the target is going to be activated,selected using a datepicker before,and added to datagrid as string), Target Amount. I have defined the datagrid like below:

<sdk:DataGrid x:Name="productListGrid" Margin="8,117,8,8" ColumnHeaderStyle="{StaticResource DataGridColumnHeaderStyle}" CellStyle="{StaticResource DataGridCellStyle}" RowHeight="50" AutoGenerateColumns="False">
            <sdk:DataGrid.Columns>
                <sdk:DataGridTextColumn Binding="{Binding ProductName}" CanUserSort="True" CanUserReorder="True" CellStyle="{x:Null}" CanUserResize="True" ClipboardContentBinding="{x:Null}" DisplayIndex="-1" DragIndicatorStyle="{x:Null}" EditingElementStyle="{x:Null}" ElementStyle="{x:Null}" Foreground="{x:Null}" FontWeight="Normal" FontStyle="Normal" FontSize="NaN" HeaderStyle="{x:Null}" Header="Name" IsReadOnly="False" MaxWidth="Infinity" MinWidth="0" SortMemberPath="{x:Null}" Visibility="Visible" Width="Auto"/>
                <sdk:DataGridTextColumn Binding="{Binding Session}" CanUserSort="True" CanUserReorder="True" CellStyle="{x:Null}" CanUserResize="True" ClipboardContentBinding="{x:Null}" DisplayIndex="-1" DragIndicatorStyle="{x:Null}" EditingElementStyle="{x:Null}" ElementStyle="{x:Null}" Foreground="{x:Null}" FontWeight="Normal" FontStyle="Normal" FontSize="NaN" HeaderStyle="{x:Null}" Header="Session" IsReadOnly="False" MaxWidth="Infinity" MinWidth="0" SortMemberPath="{x:Null}" Visibility="Visible" Width="Auto"/>
                <sdk:DataGridTextColumn Binding="{Binding TargetQuantity, Mode=TwoWay}" CanUserSort="True" CanUserReorder="True" CellStyle="{x:Null}" CanUserResize="True" ClipboardContentBinding="{x:Null}" DisplayIndex="-1" DragIndicatorStyle="{x:Null}" EditingElementStyle="{x:Null}" ElementStyle="{x:Null}" Foreground="{x:Null}" FontWeight="Normal" FontStyle="Normal" FontSize="NaN" HeaderStyle="{x:Null}" Header="Target Quantity" IsReadOnly="False" MaxWidth="Infinity" MinWidth="0" SortMemberPath="{x:Null}" Visibility="Visible" Width="Auto"/>
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>

I have a class that represents each row:

public class DataGridRow
        {
            public string ProductName{get;set;}
            public string Session { get; set; }
            public string TargetQuantity { get; set; }

        }

And then using the following method I set itemssource of the datagrid:

private void initDataGrid(string product_type)
        {
            List<DataGridRow> rows = new List<DataGridRow>();
            if (start.Equals(NullDate) || end .Equals(NullDate))
            {
                MessageBox.Show("Please select start and end date first!");
            }
            else
            {
                var products_list = _context.Products.Where(entity => entity.ProductType.Equals(product_type));

                var product_name_list = from product in products_list select product.ProductName;

                foreach (string name in product_name_list)
                {
                    rows.Add(new DataGridRow()
                    {
                        ProductName = name,
                        Session = this.Months[start.Month-1] + "," + start.Year + "->" + this.Months[end.Month-1] + "," + end.Year,
                        TargetQuantity = "0.0"
                    });
                }
                try
                {
                    this.productListGrid.ItemsSource = rows;
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

I have checked the list rows by a for loop if it was filled properly, and saw that it was.

This method is invoked once I select a product type from combo box:

private void productCombo_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            string product_type=(string)e.AddedItems[0];
            this.initDataGrid(product_type);
        }

But once I run the app and select something from the combobox the whole screen becomes blank.Then I commented out the line this.productListGrid.ItemsSource = rows; from initDataGrid method and then I could select items from combo except the datagrid left blank this time(usual actually, the itemssource is not set) So it seems when I set the ItemsSource for DataGrid my silverlight app gets crashed. I have been struggling with it for few hours,searched Google & StackOF so many times,but no way... so I need help badly.

If any body can solve the problem please give a bit explanation too as I am new to silverlight and don't want to make the mistake again due to lack of knowledge.

Thanks in advance.

Upvotes: 0

Views: 692

Answers (1)

McAden
McAden

Reputation: 13972

Since you're not giving an exception we don't have much to go on. I would guess since you're posting it here that there isn't a specific line it's breaking on then it's probably a Xaml issue. For starters, try simplifying your datagrid's columns. You're setting a bunch of properties you have no business setting. I'm also noticing you're setting DisplayIndex to -1 on each of them which I suspect is the issue.

Try this:

<sdk:DataGrid x:Name="productListGrid" Margin="8,117,8,8" ColumnHeaderStyle="{StaticResource DataGridColumnHeaderStyle}" CellStyle="{StaticResource DataGridCellStyle}" RowHeight="50" AutoGenerateColumns="False">
    <sdk:DataGrid.Columns>
        <sdk:DataGridTextColumn Binding="{Binding ProductName}" CanUserSort="True" CanUserReorder="True" CanUserResize="True" Header="Name" IsReadOnly="False" Width="Auto"/>
        <sdk:DataGridTextColumn Binding="{Binding Session}" CanUserSort="True" CanUserReorder="True" CanUserResize="True" Header="Session" IsReadOnly="False" Width="Auto"/>
        <sdk:DataGridTextColumn Binding="{Binding TargetQuantity, Mode=TwoWay}" CanUserSort="True" CanUserReorder="True" CanUserResize="True" Header="Target Quantity" IsReadOnly="False" Width="Auto"/>
    </sdk:DataGrid.Columns>
</sdk:DataGrid>

Upvotes: 1

Related Questions