ورود

View Full Version : سوال: مشکل اپدیت نشدن دیتاگرید



hercool
چهارشنبه 23 بهمن 1392, 20:52 عصر
سلام دوستان من مشکلی با اپدیت شدن دیتاگریدم دارم
من با الگوی mvvm کار می کنم
باید بگم وقتی من رکورد جدیدی را ثبت می کنم در دیتاگرید نمایش داده نمیشه و باید صفحه را دوباره فراخوانی کنم تا بتونه به دیتابیس وصل بشه و اطلاعات را دوباره بکشه
class CompanyViewModel :INotifyPropertyChanged
{
#region properties
private Int32 codeInsurance = 0;
public Int32 CodeInsurance
{
get
{ return codeInsurance; }

set
{
if (codeInsurance == value)
{ return; }

codeInsurance = value;
this.RaisePropertyChanged("CodeInsurance");
}
}

private string companyInsurance = string.Empty;
public string CompanyInsurance
{
get
{ return companyInsurance; }

set
{
if (companyInsurance == value)
{ return; }

companyInsurance = value;
this.RaisePropertyChanged("CompanyInsurance");
}
}


private bool state = true;
public bool State
{
get { return state; }
set
{
if (state == value) { return; }
state = value;
this.RaisePropertyChanged("State");
}
}

#endregion

newbitsqlite.App app = (Application.Current as App);

// گرفتن نام یا ای دی یک شرکت بیمه
public CompanyViewModel GetDoctor(Int32 codeInsurance)
{
var company = new CompanyViewModel();
using (var db = new SQLite.SQLiteConnection(app.DBPath))
{
var _Company = (db.Table<Company>().Where(d => d.CodeInsurance == codeInsurance)).Single();
company.CodeInsurance = _Company.CodeInsurance;
company.CompanyInsurance = _Company.CompanyInsurance;
company.state = _Company.state;
}
return company;
}
public CompanyViewModel GetDoctor(string companyInsurance)
{
var company = new CompanyViewModel();
using (var db = new SQLite.SQLiteConnection(app.DBPath))
{
var _Company = (db.Table<Company>().Where(d => d.CompanyInsurance == companyInsurance)).Single();
company.CompanyInsurance = _Company.CompanyInsurance;
company.CodeInsurance = _Company.CodeInsurance;
company.state = _Company.state;
}
return company;
}
public string SaveNewCompany(CompanyViewModel Company)
{
string result = string.Empty;
using (var db = new SQLite.SQLiteConnection(app.DBPath))
{
string change = string.Empty;
try
{
var existCompany = (db.Table<Company>().Where(c => c.CodeInsurance == Company.CodeInsurance)).SingleOrDefault();
if (existCompany != null)
{
existCompany.CodeInsurance = Company.CodeInsurance;
existCompany.CompanyInsurance = Company.CompanyInsurance;
existCompany.state = Company.State;
}
else
{
int success = db.Insert(new Company()
{
CompanyInsurance=Company.CompanyInsurance,
CodeInsurance=Company.CodeInsurance,
state=Company.State
});
}
result = "ثبت کمپانی با موفقیت انجام شد";
}
catch
{
result = "ثبت کمپانی با موفقیت انجام نشد";
}
}

return result;
}



این کد مربوط به اپدیت در کلاس بالا
public event PropertyChangedEventHandler PropertyChanged;

protected virtual void RaisePropertyChanged(string propertyName)
{
var handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
این کد xaml

<Grid:RadDataGrid x:Name="Copmanydatagrid" HorizontalAlignment="Left" Margin="10,200,0,0" Grid.Row="1" VerticalAlignment="Top" Height="275" Width="385"
ItemsSource="{Binding Mode=TwoWay,Source={StaticResource CompanyViewSource}}" SelectionChanged="Copmanydatagrid_SelectionChanged" SelectionMode="Multiple">
اینم کد مربوط به کلید ذخیره که م یخوام رکورد جدیدی اضافه کنم
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
CompanyViewModel newmodel = new CompanyViewModel();
newmodel.CodeInsurance = Convert.ToInt32(tx_CodeInsurance.Text);
newmodel.CompanyInsurance = tx_CompanyInsurance.Text.ToString();
newmodel.State = Convert.ToBoolean(tx_state.IsChecked);
CompanyViewModel savemodel = new CompanyViewModel();
savemodel.SaveNewCompany(newmodel);
Copmanydatagrid.ItemsSource = Companys;
}