2016-04-14 40 views
1

bağlayıcı sonra boş olmalıdırÖğeler koleksiyonu ben SQLite veritabanından veri kılavuzunu doldurmak veri kılavuzunu

var mA = new System.Data.SQLite.SQLiteDataAdapter("SELECT * FROM users ORDER BY Name", DataHolder.SQLiteConnection); 
var mT = new System.Data.DataTable(); 
if (dataGrid.Columns.Count > 0) 
{ 
    return; 
} 
mA.Fill(mT); 
if (mT.Rows.Count == 0) 
{ 
    mT.Rows.Add(new object[mT.Columns.Count]); 
} 
dataGrid.ItemsSource = mT.DefaultView; 

ama onun değeri bu kodu

<DataGrid x:Name="dataGrid" Margin="0,10,0,0" Loaded="dataGrid_Loaded" FontSize="14"> 
    <DataGridTextColumn Binding="{Binding Active}"> 
     <DataGridTextColumn.ElementStyle> 
     <Style TargetType="{x:Type TextBlock}"> 
      <Setter Property="Background" Value="{Binding Active, Converter={StaticResource All}}"/> 
     </Style> 
     </DataGridTextColumn.ElementStyle> 
    </DataGridTextColumn> 
</DataGrid> 
tarafından 0'a eşitse eğer hücre arka planını değiştirmek için DataGrid bağlamak sonra

public class All : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     string input = value as string;  

     if (System.Convert.ToByte(input) == 0) 
     { 
      return Brushes.Red; 
     } 
     else if (System.Convert.ToByte(input) > 0 && System.Convert.ToByte(input) < 5) 
     { 
      return Brushes.OrangeRed; 
     } 
     else 
     { 
      return DependencyProperty.UnsetValue; 
     } 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotSupportedException(); 
    } 

} 

ne zamanzamanında o hatayı alıyorum IValueConverter Öğeler koleksiyonu ItemsSource kullanmadan önce boş olması gerekir:

tip system.data.dll 'bir istisna

PresentationFramework.dll oluştu ama

bilgiye kullanıcı kodu işlenmedi söylüyorlar dolduruluyor.

dataGrid.ItemsSource = mT.DefaultView;

+0

kontrolü çok zor kodlanmış unsurları içerdiğinde bu hata oluşur öğeleri doldurmaya çalışacak bir hatayla edilir. combobox'a bazı comboboxitem eklemek ve daha sonra itemssource ayarlamak gibi, bu hatayı alacaksınız. – AnjumSKhan

cevap

4

de misin XAML DataGrid.Columns etiket eklemek unuttum. O DataGridTextColumn olmadan bir öğe olarak ele ve ItemsSource set beri size iki kaynaktan

<DataGrid ...> 
    <DataGrid.Columns> 
     <DataGridTextColumn>