Hi All,
We have a question about data binding in DataGrid with LINQ data context which is using SQLConnection class. As we need a dynamic UI to allow user select tables to modify data, hence we cannot use strong typed datatabe in data context class. Here's the coding,
public partial class ControlTable : UserControl
{
private MyDBContext dbcontext = new MyDBContext(new SQLConnection....);
public ControlTable()
{
InitializeComponent();
comboBoxTable.ItemsSource = dbcontext.Mapping.GetTables();
comboBoxTable.DisplayMemberPath = "TableName";
}
private void comboBoxTable_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Type tableType = ((MetaTable)comboBoxTable.SelectedItem).RowType.Type;
dataGrid.ItemsSource = dbcontext.GetTable(tableType);
}
private void SaveBtn_Click(object sender, RoutedEventArgs e)
{
using (TransactionScope ts = new TransactionScope())
{
try
{
dbcontext.SubmitChanges();
ts.Complete();
}
catch (Exception)
{
}
}
}
private void CancelBtn_Click(object sender, RoutedEventArgs e)
{
Type tableType = ((MetaTable)comboBoxTable.SelectedItem).RowType.Type;
dbcontext.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, dbcontext.GetTable(tableType));
dataGrid.ItemsSource = dbcontext.GetTable(tableType);
}
}
It is fine for load data update date, however the issue is when click cancel button, the refresh function can reload data from database, however grid cannot refresh even use the following code which is actuall same as load data one.
dataGrid.ItemsSource = dbcontext.GetTable(tableType);
And if we change the code by using strong type table class, it is fine e.g.
dataGrid.ItemsSource = dbcontext.Table1.ToList();
We have no idea why it doesn't work, can anyone help. Many Thanks.
Regards,
Edwin.