我有一个简单的客户注册表。我在数据网格视图旁边放置了一些文本框,以便我可以添加和更新用户。这工作正常,但仍然如此。
最近,我添加了一个用于客户端定义的新窗体,因此当双击一行时,会出现一个包含必填字段的表单,显示客户端信息并允许您更改它们。单击“确定”按钮将保存对SQL数据库的更改。
使用网格视图旁边的字段,当我更新用户信息时,网格视图会立即更新并显示数据,但在使用新表单更新数据时,即使我双击,网格视图中也不会显示更新再次排。当我关闭应用程序并再次运行它时,我将看到更新的数据。
我为主窗体添加了一个激活事件,以便从数据库中再次选择数据来更新网格视图,但没有成功。这是我用于侧面板更新的代码:
private void btnUpdate_Click(object sender, EventArgs e)
{
if (btnUpdate.Text == "Update")
{
txtUsername.Text = ClientsGridView.CurrentRow.Cells["Username"].Value.ToString();
txtPassword.Text = ClientsGridView.CurrentRow.Cells["Password"].Value.ToString();
txtUID.Text = ClientsGridView.CurrentRow.Cells["UID"].Value.ToString();
txtName.Text = ClientsGridView.CurrentRow.Cells["Name"].Value.ToString();
chkIsActive.Checked = (bool)ClientsGridView.CurrentRow.Cells["IsActive"].Value;
chkItemListAccess.Checked = (bool)ClientsGridView.CurrentRow.Cells["ItemListAccess"].Value;
chkMaterialSumAccess.Checked = (bool)ClientsGridView.CurrentRow.Cells["MaterialSumAccess"].Value;
chkPartListAccess.Checked = (bool)ClientsGridView.CurrentRow.Cells["PartListAccess"].Value;
chkPriceListAccess.Checked = (bool)ClientsGridView.CurrentRow.Cells["PriceListAccess"].Value;
btnUpdate.Text = "Apply Update";
btnSubmit.Enabled = false;
txtUsername.ReadOnly = true;
}
else if (btnUpdate.Text == "Apply Update")
{
if (ClientFormValidation())
{
int id = Convert.ToInt32(ClientsGridView.CurrentRow.Cells["ID"].Value.ToString());
var SelectedUser = from Client in ClientsContext.Clients
where Client.ID == id
select Client;
if (SelectedUser.Count() == 1)
{
Client UpdatingClient = SelectedUser.Single();
UpdatingClient.Password = txtPassword.Text.Trim();
UpdatingClient.UID = txtUID.Text.Trim();
UpdatingClient.Name = txtName.Text.Trim();
UpdatingClient.IsActive = chkIsActive.Checked;
UpdatingClient.ItemListAccess = chkItemListAccess.Checked;
UpdatingClient.MaterialSumAccess = chkMaterialSumAccess.Checked;
UpdatingClient.PartListAccess = chkPartListAccess.Checked;
UpdatingClient.PriceListAccess = chkPriceListAccess.Checked;
ClientsContext.SubmitChanges();
}
ShowAll();
ClearClientForm();
btnUpdate.Text = "Update";
btnSubmit.Enabled = true;
txtUsername.ReadOnly = false;
}
}
}
这适用于客户定义表单:
private void btnOK_Click(object sender, EventArgs e)
{
if(ISnewUser)
{
InsertNewClient();
}
else if(!ISnewUser)
{
UpdateClient();
}
this.Close();
}
private void UpdateClient()
{
if (ClientFormValidation())
{
var SelectedUser = from Client in ClientsContext.Clients
where Client.ID == id
select Client;
if (SelectedUser.Count() == 1)
{
Client UpdatingClient = SelectedUser.Single();
UpdatingClient.Password = txtPassword.Text.Trim();
UpdatingClient.UID = txtUID.Text.Trim();
UpdatingClient.Name = txtName.Text.Trim();
UpdatingClient.IsActive = chkUserActiveSatus.Checked;
UpdatingClient.ItemListAccess = chkItemListAccess.Checked;
UpdatingClient.MaterialSumAccess = chkMaterialSummeryAccess.Checked;
UpdatingClient.PartListAccess = chkPartListAccess.Checked;
UpdatingClient.PriceListAccess = chkPriceListAcess.Checked;
ClientsContext.SubmitChanges();
}
}
}
有谁能告诉我这段代码有什么问题?
答案 0 :(得分:0)
*var SelectedUser = from Client in ClientsContext.Clients
where Client.ID == id
select Client;
它是Iqueryable,应用.ToList()来获取内存,然后修补更改。
相反,我建议,使用此,
var SelectedUser = (from Client in ClientsContext.Clients
where Client.ID == id
select Client).FirstOrDefault();
if(SelectedUser!=null){
SelectedUser.Password = txtPassword.Text.Trim();
SelectedUser.UID = txtUID.Text.Trim();
SelectedUser.Name = txtName.Text.Trim();
SelectedUser.IsActive = chkUserActiveSatus.Checked;
SelectedUser.ItemListAccess = chkItemListAccess.Checked;
SelectedUser.MaterialSumAccess = chkMaterialSummeryAccess.Checked;
SelectedUser.PartListAccess = chkPartListAccess.Checked;
SelectedUser.PriceListAccess = chkPriceListAcess.Checked;
ClientsContext.SubmitChanges();
}
else{
//write your logic
}*
我只是将异常处理留给你。
答案 1 :(得分:0)
public frmClientDefinition(Client thisClient=null)
{
InitializeComponent();
if (thisClient!=null)
{
User = thisClient;
id = thisClient.ID;
ISnewUser = false;
txtName.Text = thisClient.Name;
txtUsername.Text = thisClient.Username;
txtPassword.Text = thisClient.Password;
txtDate.Text = thisClient.Date.ToString();
txtUID.Text = thisClient.UID;
chkUserActiveSatus.Checked = thisClient.IsActive;
chkItemListAccess.Checked = thisClient.ItemListAccess;
chkPartListAccess.Checked = thisClient.PartListAccess;
chkMaterialSummeryAccess.Checked = thisClient.MaterialSumAccess;
chkPriceListAcess.Checked = thisClient.PriceListAccess;
chkFullPriceListAccess.Checked = false;
chkOfficialRecieptAccess.Checked = false;
chkNonOfficialRecieptAccess.Checked = false;
chkAdvancedPriceControlsAccess.Checked = false;
chkFullPriceListSaveAccess.Checked = false;
chkOfficialRecieptSaveAccess.Checked = false;
chkNonOfficialRecieptSaveAccess.Checked = false;
txtUsername.ReadOnly = true;
}
txtDate.Text = DateTime.Now.ToString();
}
并更新此代码段:
private void UpdateClient()
{
if (ClientFormValidation())
{
User.Password = txtPassword.Text.Trim();
User.UID = txtUID.Text.Trim();
User.Name = txtName.Text.Trim();
User.IsActive = chkUserActiveSatus.Checked;
User.ItemListAccess = chkItemListAccess.Checked;
User.MaterialSumAccess = chkMaterialSummeryAccess.Checked;
User.PartListAccess = chkPartListAccess.Checked;
User.PriceListAccess = chkPriceListAcess.Checked;
ClientsContext.SubmitChanges();
}
}