I have a DataGrid
where its ItemsSource
bound to a DataTable
. When I Update the DataTable
, the data from the ViewModel
gets updated but the UI won’t. I already use INotifyPropertyChanged
for my ViewModel
.
Here is my functions
private DataTable hariKerjaTable;
public DataTable HariKerjaTable
{
get => hariKerjaTable;
set
{
hariKerjaTable = value;
OnPropertyChanged(nameof(HariKerjaTable));
}
}
private DataTable kalenderKerjaTable;
public DataTable KalenderKerjaTable
{
get => kalenderKerjaTable;
set
{
kalenderKerjaTable = value;
OnPropertyChanged(nameof(KalenderKerjaTable));
}
}
private async void GetWorkingCalendar()
{
HariKerjaTable.Clear();
KalenderKerjaTable.Columns.Clear();
KalenderKerjaTable.Rows.Clear();
var data = new
{
month = Month,
year = Year,
};
string get = await BaseAPI.PostMethod("calendar/select", JsonConvert.SerializeObject(data));
if (get is not null)
{
JObject jsonObject = JObject.Parse(get);
JToken dataToken = jsonObject["data"];
HariKerjaTable = dataToken.ToObject<DataTable>();
}
else
{
MessageBox.Show("Something wrong when retrieving", "Null", MessageBoxButton.OK, MessageBoxImage.Error);
}
Application.Current.Dispatcher.Invoke(() =>
{
foreach (DataRow row in HariKerjaTable.Rows)
{
DateTime date = DateTime.Parse(row["date_calendar"].ToString());
Debug.WriteLine(date.ToString("dddndd"));
if (!KalenderKerjaTable.Columns.Contains(date.ToString("dddndd")))
{
KalenderKerjaTable.Columns.Add(date.ToString("dddndd"));
}
}
OnPropertyChanged(nameof(KalenderKerjaTable));
});
Debug.WriteLine("+++++++++++++++++++++++++++++++");
foreach (DataColumn column in KalenderKerjaTable.Columns)
{
Debug.WriteLine(column.ColumnName);
}
}
This is my XAML
<Grid Grid.Row="1" Margin="0,5,0,0">
<DataGrid ItemsSource="{Binding KalenderKerjaTable, Mode=TwoWay}" FrozenColumnCount="2" AutoGenerateColumns="True"/>
</Grid>
I tried to add Mode=TwoWay
and UpdateSourceTrigger=PropertyChanged
to the XAML, but it doesn’t change anything.
New contributor
Exto Logia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.