Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi,

I am using EntityFrameWork while working with Wpf application , In that iam using the DataGrid, In that i am trying to display the Numbers for each Row to that DataGrid... For that i have used one converter and RowHeaderStyle Code... But it showing some convertion error... the Detailed Code is Like Below..
XML
<DataGrid.RowHeaderStyle>
                  <Style TargetType="{x:Type DataGridRowHeader}">
                      <Setter Property="Content" Value="{Binding Converter={StaticResource RowIndexConverter }}"/>
                  </Style>
              </DataGrid.RowHeaderStyle>
          </DataGrid>

The Converter Code is
C#
public object Convert(object value, Type targetType, object parameters, System.Globalization.CultureInfo culture)
        {
            try
            {

                System.Data.DataRowView drv = (System.Data.DataRowView)value;
                DataGrid dg = (DataGrid)Application.Current.MainWindow.FindName("GridEmployeeList");
                CollectionView cv = (CollectionView)dg.Items;

                int rowIndex = cv.IndexOf(drv) + 1;
                return rowIndex.ToString();

            }
            catch (Exception e)
            {

                throw new NotImplementedException(e.Message);
            }
        }


While Working with Normal DataBase connection here...In Convertion function, the Type of the varible 'value' is
System.Data.DataRowView...so it didn't showing any error..it showing row count perfectly...

But when i working with EntityFrameWork and Binding Values to DataGrid ,,,In Convertion function, the Type of the varible 'value' is
showing.."MyOrg.EmployeeDetails" ..i.e it is of type Edm class, So it getting error like"Unable to cast object of type 'MyOrgDA.EmployeeDetail' to type 'System.Data.DataRowView'.""...

please provide the Solution or alternative solution to OverCome this behaviour While Working with EntityFrameWork..

The Same Code working Fine for normal Code without using EntityFrameWork...



Thank you...
Posted
Updated 28-May-12 23:47pm
v3
Comments
db7uk 29-May-12 8:55am    
The binding to the datagrid will (depending on the datasource property value) have the bound object in it (i.e. MyOrg.EmployeeDetails). The reason this is showing up in the converter is because you bound the content of the header to your entity.

What are you trying to achieve? do you want a label that has the number of rows in the grid?

If so can you not bind to the count of your collection instead of using a converter?
V G S Naidu A 29-May-12 23:53pm    
Actually i want to display row number before the in header,,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900