哈喽,你好啊,我是雷工!
前边学习了通过选择条件查询账号的功能:
本节继续学习练习C#,今天练习修改账号的功能实现。
以下为学习笔记。
01 实现功能
①:从查询到的账号中,选择某一账号,然后点击【修改账号】按钮,将选中的信息获取显示到,下方的修改面板中;
②:增加功能:每次点击【查询按钮】时关闭修改面板;
02 效果演示
2.1、【修改账号】按钮功能演示
2.2、【提交查询】按钮功能演示
03 实现步骤
3.1、数据访问层
在数据访问层的AccountServices.cs类中添加修改对象的方法;
实现思路:
①:定义修改数据的SQL语句;
②:封装要修改的参数;
③:提交保存;
添加的代码如下:
/// <summary>
/// 修改账号对象
/// </summary>
/// <param name="account"></param>
/// <returns></returns>
public int ModifyAccount(Account account)
{
//定义SQL语句
string sql = #34;update Account Set AccountName=@accountName,AccountContent=@accountContent,originality=@originality,TypeId=@typeId";
sql += " where AccountId=@accountId";
//封装参数
SqlParameter[] param = new SqlParameter[]
{
new SqlParameter("@AccountName",account.AccountName),
new SqlParameter("@AccountContent",account.AccountContent),
new SqlParameter("@originality",account.originality),
new SqlParameter("@TypeId",account.TypeId),
new SqlParameter("@AccountId",account.AccountId),
};
//提交保存
return SQLHelper.Update(sql, param);
}
3.2、业务逻辑层
业务逻辑层比较简单只是传递功能,
在业务逻辑层的AccountManager.cs类中添加如下代码:
public int ModifyAccount(Account account)
{
return accountServices.ModifyAccount(account);
}
3.3、UI层
在UI层的FrmIPManager.cs类中添加【修改账号】按钮的功能代码:
实现思路:
①:首先判断是否选中了查到数据的某一行;
②:获取选中行对应的账号ID;
③:根据获取的账号ID从缓存的集合中查询账号对象;
④:在修改面板显示选中的要修改的账号对象;
⑤:显示修改面板
其中根据账号ID从缓存中获取选中对象有3中方法:
方式1:通过循环获取当前对象(适合初学者,容易理解)
foreach(var item in this.queryList)
{
if(item.AccountId.Equals(accountId))
{
currentAccount = item;
break;
}
}
方式2:通过LINQ获取当前选中对象(实际项目中应用)
currentAccount = (from a in this.queryList where a.AccountId.Equals(accountId)select a).First();
方式3:通过LINQ获取当前选中对象(实际项目中应用)
currentAccount = this.queryList.Where(a => a.AccountId.Equals(accountId)).First();
【修改账号】按钮的完整事件代码为:
/// <summary>
/// 【修改账号】按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnEditIP_Click(object sender, EventArgs e)
{
//①:首先判断是否有选中某行信息
if(this.dgvAccountList.CurrentRow==null)
{
MessageBox.Show("得先选中一行呀!", "雷工提示");
return
}
//②:获取选中行对应的账号ID
int accountId = (int)this.dgvAccountList.CurrentRow.Cells["AccountId"].Value;
//③:根据获取到的账号ID从集合中查询账号对象
Account currentAccount = null;
//方式3:通过LINQ获取当前选中对象(实际项目中应用)
currentAccount = this.queryList.Where(a => a.AccountId.Equals(accountId)).First();
//④:显示要修改的账号对象
this.txtIPName_Modify.Text = currentAccount.AccountName;
this.txtOriginalArticles.Text = currentAccount.originality.ToString();
//this.txtPageView.Text = currentAccount.PageView.ToString();
this.txtIPIntro.Text = currentAccount.AccountContent;
this.lblIPID.Text = currentAccount.AccountId.ToString();//提交修改时需要使用
//将当前账号分类与下拉框选择做同步
this.cbbType_Modify.SelectedValue = currentAccount.TypeId;
//⑤:显示修改面板
this.panelModify.Visible = true;
}
3.4、功能优化
当点击【修改账号】按钮后,再次修改条件,点击查询,修改面板需要随之关闭。
在【提交查询】按钮事件中添加如下代码:
//每次查询,隐藏修改面板,提高用户体验
this.panelModify.Visible = false;
3.5、账号类型
修改面板的账号类型下拉框的填充与上方的类似,但不能直接复制,直接复制会导致修改面板的账号类型修改,上方的账号分类下拉框也跟着同步变化,需要稍微调整。
代码如下:
//绑定修改用的账号分类
this.cbbType_Modify.DataSource =new List<AccountType>( list);//将前面的集合重新复制再做数据源
this.cbbType_Modify.DisplayMember = "TypeName";//设置该属性为UI中看到的信息列设置;
this.cbbType_Modify.ValueMember = "TypeId";//保存到数据库使用的外键值;
04 后记
以上为【修改账号】按钮事件的实现练习笔记,
接下来开始【提交修改】按钮功能的实现,将修改的信息更新到SQLServer数据库中。
有感兴趣的可以点赞关注!
本文暂时没有评论,来添加一个吧(●'◡'●)