I have a datagrid in main form, which take datas from databse Firebird.
In another form i''am editing datas in database by wcf service,in which i describe a methods to create,update my datas.
But I cannot refresh datas in datagrid when i create a new record in second window..
Datas to datagrid i''am taking from database by using a class.
code of button edit,new and delete ..private void QueueOperations(int tag)
{
Queue q = new Queue();
MainWindow mn=new MainWindow();
ISDElectronicQueueWcfServiceLibrary.Queue queue = new ISDElectronicQueueWcfServiceLibrary.Queue();
switch (tag)
{
case 1:
{
if (ttbQueueName.Text == "")
{
MessageBox.Show("Заполните наименование очереди", this.Title);
ttbQueueName.Focus();
}
else
{
try
{
svc.CreateQueue(ttbQueueName.Text, ttbQueueDescription.Text);
ttbQueueDescription.Text = "";
ttbQueueName.Text = "";
q.dtgQueue.ItemsSource = svc.GetQueuefromClass(0, 0);
q.dtgQueue.Items.Refresh();
Close();
}
catch (Exception str)
{
MessageBox.Show("Такая очередь уже существует\n" + str.Message, this.Title);
}
}
}
break;
case 2:
{
if (ttbQueueName.Text.Length == 0)
{
MessageBox.Show("Нужно заполнить наименование", mn.Title);
ttbQueueName.Focus();
}
else
{
try
{
queue.id = queueid;
queue.name = ttbQueueName.Text;
queue.description = ttbQueueDescription.Text;
svc.SetQueue(queue);
ttbQueueName.Text = "";
ttbQueueDescription.Text = "";
q.dtgQueue.ItemsSource = svc.GetQueuefromClass(0, 0);
q.dtgQueue.Items.Refresh();
Close();
}
catch (Exception e)
{
MessageBox.Show(e.Message, mn.Title);
}
}
}
break;
case 3:
{
svc.ClearQueue(queueid);
ttbQueueName.Text = "";
ttbQueueDescription.Text = "";
q.dtgQueue.ItemsSource = svc.GetQueuefromClass(0, 0);
q.dtgQueue.Items.Refresh();
Close();
}
break;
}
ttbQueueName.Focus();
}
解决方案Use an ObservableCollection for the data (http://msdn.microsoft.com/en-us/library/ms668604.aspx[^]). The ObservableCollection handles the NotifyPropertyChanged when items are added or deleted.
Since only see part of the solution, so not sure about everything. You may need to provide an adapter to convert your Queue (? guess you are using a queue) to an ObservableCollection. You could also create a control that does what you want as far as Queue operations the inherits from ObservableCollection
try this..
dataGrid1.BeginEdit();
///add your code here/
dataGrid1.CommitEdit();//or use only this
then refresh datagrid