2

I have a DataTable and when I alter (add,modifiy..ect) it wont reflect on the DataGrid which is bound to the DataTable.

ItemsSource="{Binding TableData,Mode=TwoWay,IsAsync=True}" 

Thats the binding ^

Now when I set the RowError

TableData.Rows[x].RowError = ex.Message;

HasError gets set to true... but the DataGrid does not reflect this ( I have a style that marks a row red when there is an error)

Note: My changes are not being reflected on more then setting ErrorMessages, I've also tried adding rows in the ViewModel and those added rows are not relfected either.

About the DataTable: It has no set columns or anything, it relfects a selected Database table which the user picks.

user1145927
  • 133
  • 2
  • 13

2 Answers2

1

Binding to a DataTable won't work. You need to first convert your DataTable to an ObservableCollection<>. From here:

    public void ConvertDataTable( DataTable dt )
    {
        DataList  = new ObservableCollection<ProjectWorkHours>();
        //Scan and arrange data into ObservableCollection
        int UserID = 0;
        if ( dt.Rows.Count >0 )
        {
            UserID = int.Parse( dt.Rows[0]["UserID"].ToString() );
            //Distill project id list
            List<int> ProjectIDList = GetProjectIDList( dt );
            for ( int i = 0 ; i < ProjectIDList.Count; i ++ )
            {
                int ProjectID= ProjectIDList[i];
                //Get WorkRecord
                int[] MyWorkRecord = GetWorkRecord(dt, ProjectID);
                ProjectWorkHours newProjectWorkHours = new ProjectWorkHours(UserID,ProjectID,MyWorkRecord);
                DataList.Add( newProjectWorkHours);
            }           
        } 
    }

The link has a more full example of working with a database and using binding.

N_A
  • 19,799
  • 4
  • 52
  • 98
0

What Type is TableData?

If it is a List<> then changing the items in the list will NOT update the binding
If it is an ObservableCollection<> then changing the items in the collection will update the binding

MyKuLLSKI
  • 5,285
  • 3
  • 20
  • 39