//点击gridview控件自带的编辑按钮时执行的事件
protected void gvNewsList_RowEditing(object sender, GridViewEditEventArgs e)
{
gvNewsList.EditIndex = e.NewEditIndex;
BindData();
}
//在编辑状态下,点击取消按钮时执行的事件
protected void gvNewsList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvNewsList.EditIndex = -1;
BindData();
}
//在编辑状态下,点击更新按钮时执行的事件
protected void gvNewsList_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//这里是关键,在绑定数据的时候要指定主键名称,比如字段id,在这里可以获得这条记录的id
//而这个id就是我们要更改的数据的id
string ids = gvNewsList.DataKeys[e.RowIndex].Value.ToString();
//如果有多个DataKeyNames可以通过下面这个方法得到指定的一个
//string ids = gvNewsList.DataKeys[e.RowIndex][0].ToString();
SystemMemberLevelModel systemMemberLevelModel = systemMemberLevelBll.GetModel(Convert.ToInt64(ids));
if (string.IsNullOrEmpty(((TextBox)gvNewsList.Rows[e.RowIndex].Cells[0].Controls[0]).Text))
{
Common.MessageBox.Show(this, "请输入级别名称!");
return;
}
else
{
systemMemberLevelModel.name = ((TextBox)gvNewsList.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
}
//是否允许新增会员
systemMemberLevelModel.isAllowAddMember = ((CheckBox)gvNewsList.Rows[e.RowIndex].Cells[5].Controls[0]).Checked;
//是否缺省级别
systemMemberLevelModel.isDefault = ((CheckBox)gvNewsList.Rows[e.RowIndex].Cells[6].Controls[0]).Checked;
//是否新闻发布免审核
systemMemberLevelModel.isFreeAuditNews = ((CheckBox)gvNewsList.Rows[e.RowIndex].Cells[7].Controls[0]).Checked;
// 是否求购发布免审核
systemMemberLevelModel.isFreeAuditProvPur = ((CheckBox)gvNewsList.Rows[e.RowIndex].Cells[4].Controls[0]).Checked;
//供应发布数量
if (!string.IsNullOrEmpty(((TextBox)gvNewsList.Rows[e.RowIndex].Cells[2].Controls[0]).Text))
{
systemMemberLevelModel.pubQuantityProv =Convert.ToInt64(((TextBox)gvNewsList.Rows[e.RowIndex].Cells[2].Controls[0]).Text);
}
//求购发布数量
if (!string.IsNullOrEmpty(((TextBox)gvNewsList.Rows[e.RowIndex].Cells[3].Controls[0]).Text))
{
systemMemberLevelModel.pubQuantityPur = Convert.ToInt64(((TextBox)gvNewsList.Rows[e.RowIndex].Cells[3].Controls[0]).Text);
}
//序号
if (!string.IsNullOrEmpty(((TextBox)gvNewsList.Rows[e.RowIndex].Cells[1].Controls[0]).Text))
{
systemMemberLevelModel.sort = Convert.ToInt32(((TextBox)gvNewsList.Rows[e.RowIndex].Cells[1].Controls[0]).Text);
}
try
{
systemMemberLevelBll.Update(systemMemberLevelModel);
gvNewsList.EditIndex = -1;
BindData();
}
catch (System.Data.DataException x)
{
throw new System.Data.DataException(x.ToString());
}
}
//点击控件自带的删除按钮时执行的事件
protected void gvNewsList_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//这里同样是通过这个属性来获取id
string ids = gvNewsList.DataKeys[e.RowIndex].Value.ToString();
try
{
systemMemberLevelBll.Delete(Convert.ToInt64(ids));
BindData();
}
catch (System.Data.DataException x)
{
throw new System.Data.DataException(x.ToString());
}
}
//GridView数据绑定
protected void BindData()
{
IList<SystemMemberLevelModel> list = systemMemberLevelBll.GetAllList();
this.gvNewsList.DataSource = list;
gvNewsList.DataKeyNames = new string[] { "id" };//主键
gvNewsList.DataBind();
}
原文地址:http://www.cnblogs.com/lyl6796910/p/3746636.html