数据网格。我不能刷新字段

时间:2015-02-26 10:15:27

标签: c# wpf datagrid refresh

我无法刷新数据网格中的字段。

这是我的数据网格

 <DataGrid 
    ItemsSource="{Binding Dati_Viaggio}" SelectedItem="{Binding SelectDati_Viaggio}" 
       Style="{DynamicResource ST_DataGrid}" 
        CellStyle="{DynamicResource St_DataGridCellStyle}" SelectionMode="Single"  Name="Dg_Dati" 
        IsReadOnly="True" RowDetailsVisibilityMode="VisibleWhenSelected" >

这是我要刷新的数据网格中的字段

 <DataGridTextColumn x:Name="col_NumOrd" Binding="{Binding Path=NumOrd}"  Header="Num. Ord."  Width="150" />

这就是财产。

public ObservableCollection<Model_Ricerca_Dati_Viaggio> Dati_Viaggio 
   { get; set; }

   private Model_Ricerca_Dati_Viaggio _SelectDati_Viaggio;
   public Model_Ricerca_Dati_Viaggio SelectDati_Viaggio {
get { return _SelectDati_Viaggio; }
set {
    _SelectDati_Viaggio = value;
    OnPropertyChanged("SelectDati_Viaggio");}

为什么我编写此代码时datagrid不会刷新字段?

SelectDati_Viaggio.NumOrd= "abcabc";
OnPropertyChanged("SelectDati_Viaggio");

谢谢

2 个答案:

答案 0 :(得分:0)

我想在这里:

<DataGridTextColumn x:Name="col_NumOrd" Binding="{Binding Path=NumOrd}"/>

你绑定到NumOrd但是在这里:

SelectDati_Viaggio.NumOrd= "abcabc";
OnPropertyChanged("SelectDati_Viaggio");

通知SelectDati_Viaggio,您需要通知NumOrd

SelectDati_Viaggio.NumOrd= "abcabc";
OnPropertyChanged("NumOrd");

比您的控件知道该属性已更新并刷新

修改 修改您的SelectDati_Viaggio课程

private string numOrd;
public string NumOrd 
{ 
   get { return numOrd; } 
   set { numOrd = value; OnPropertyChanged("NumOrd"); }
}
希望有所帮助!

答案 1 :(得分:0)

尝试通过重新声明和重新查询集合来刷新绑定并初始化为datagrid,只需在搜索解决方案几天后尝试这样做。如果你明白了。

server = mongoClient.GetServer();
        database = server.GetDatabase("facultyDataAndSchedule");
        collection = database.GetCollection<facultyData>("faculty");
        var query = collection.FindAllAs<facultyData>().SetFields(Fields.Include("facultyID", "term", "acadYear", "age", "program", "lastName", "firstName", "middleName", "dateOfBirth", "rank", "yearsOfTeachingS", "yearsOfTeachingO", "status", "services"));
        // List<facultyData> resultList = query.ToList<facultyData>();
       resultBinding = new ObservableCollection<facultyData>(query);
       facultyDataGrid.ItemsSource = resultBinding;
        try
        {

            try
            {
                var entity = new facultyData
                {
                    facultyID = facultyID_Textbox.Text.ToString(),
                    term = termComboBox.SelectedItem.ToString(),
                    age = int.Parse(age_TextBox.Text),
                    acadYear = "2014-2015",
                    firstName = firstName_TextBox.Text.ToString(),
                    lastName = lastName_TextBox.Text.ToString(),
                    middleName = middleName_TextBox.Text.ToString(),
                    dateOfBirth = dateOfBirth_TextBox.Text.ToString(),
                    program = "progra",
                    rank = "gegs",
                    services = "gegsg",
                    status = "geh",
                    yearsOfTeachingO = 1,
                    yearsOfTeachingS = 1
                };
                collection.Insert(entity);



            }
            catch (FormatException ex)
            {
                Console.WriteLine(ex);
            }
        }
        catch (MongoConnectionException ex)
        {
            Console.WriteLine(ex);
        }

        var query1 = collection.FindAllAs<facultyData>().SetFields(Fields.Include("facultyID", "term", "acadYear", "age", "program", "lastName", "firstName", "middleName", "dateOfBirth", "rank", "yearsOfTeachingS", "yearsOfTeachingO", "status", "services"));
        // List<facultyData> resultList = query.ToList<facultyData>();
        resultBinding = new ObservableCollection<facultyData>(query1);
        facultyDataGrid.ItemsSource = resultBinding;