码迷,mamicode.com
首页 > 其他好文 > 详细

GridView 编辑修改

时间:2014-05-26 13:28:19      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:c   class   blog   code   tar   ext   


bubuko.com,布布扣//点击gridview控件自带的编辑按钮时执行的事件   
bubuko.com,布布扣
 protected void gvNewsList_RowEditing(object sender, GridViewEditEventArgs e)
bubuko.com,布布扣    
{
bubuko.com,布布扣        gvNewsList.EditIndex 
= e.NewEditIndex;
bubuko.com,布布扣        BindData();
bubuko.com,布布扣    }

bubuko.com,布布扣
//在编辑状态下,点击取消按钮时执行的事件
bubuko.com,布布扣
    protected void gvNewsList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
bubuko.com,布布扣    
{
bubuko.com,布布扣        gvNewsList.EditIndex 
= -1;
bubuko.com,布布扣        BindData();
bubuko.com,布布扣    }

bubuko.com,布布扣
//在编辑状态下,点击更新按钮时执行的事件
bubuko.com,布布扣
 protected void gvNewsList_RowUpdating(object sender, GridViewUpdateEventArgs e)
bubuko.com,布布扣    
{
bubuko.com,布布扣        
//这里是关键,在绑定数据的时候要指定主键名称,比如字段id,在这里可以获得这条记录的id
bubuko.com,布布扣        
//而这个id就是我们要更改的数据的id

bubuko.com,布布扣        string ids = gvNewsList.DataKeys[e.RowIndex].Value.ToString();

      //如果有多个DataKeyNames可以通过下面这个方法得到指定的一个 

 

   //string ids = gvNewsList.DataKeys[e.RowIndex][0].ToString();

bubuko.com,布布扣
bubuko.com,布布扣        SystemMemberLevelModel systemMemberLevelModel 
= systemMemberLevelBll.GetModel(Convert.ToInt64(ids));
bubuko.com,布布扣        
if (string.IsNullOrEmpty(((TextBox)gvNewsList.Rows[e.RowIndex].Cells[0].Controls[0]).Text))
bubuko.com,布布扣        
{
bubuko.com,布布扣            Common.MessageBox.Show(
this"请输入级别名称!");
bubuko.com,布布扣            
return;
bubuko.com,布布扣        }

bubuko.com,布布扣        
else
bubuko.com,布布扣        
{
bubuko.com,布布扣            systemMemberLevelModel.name 
= ((TextBox)gvNewsList.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
bubuko.com,布布扣        }

bubuko.com,布布扣
bubuko.com,布布扣        
//是否允许新增会员
bubuko.com,布布扣
        systemMemberLevelModel.isAllowAddMember = ((CheckBox)gvNewsList.Rows[e.RowIndex].Cells[5].Controls[0]).Checked;
bubuko.com,布布扣        
//是否缺省级别
bubuko.com,布布扣
        systemMemberLevelModel.isDefault = ((CheckBox)gvNewsList.Rows[e.RowIndex].Cells[6].Controls[0]).Checked;
bubuko.com,布布扣        
//是否新闻发布免审核
bubuko.com,布布扣
        systemMemberLevelModel.isFreeAuditNews = ((CheckBox)gvNewsList.Rows[e.RowIndex].Cells[7].Controls[0]).Checked;
bubuko.com,布布扣        
//   是否求购发布免审核
bubuko.com,布布扣
        systemMemberLevelModel.isFreeAuditProvPur = ((CheckBox)gvNewsList.Rows[e.RowIndex].Cells[4].Controls[0]).Checked;
bubuko.com,布布扣        
//供应发布数量
bubuko.com,布布扣
        if (!string.IsNullOrEmpty(((TextBox)gvNewsList.Rows[e.RowIndex].Cells[2].Controls[0]).Text))
bubuko.com,布布扣        
{
bubuko.com,布布扣            systemMemberLevelModel.pubQuantityProv 
=Convert.ToInt64(((TextBox)gvNewsList.Rows[e.RowIndex].Cells[2].Controls[0]).Text);
bubuko.com,布布扣        }

bubuko.com,布布扣        
//求购发布数量
bubuko.com,布布扣
        if (!string.IsNullOrEmpty(((TextBox)gvNewsList.Rows[e.RowIndex].Cells[3].Controls[0]).Text))
bubuko.com,布布扣        
{
bubuko.com,布布扣            systemMemberLevelModel.pubQuantityPur 
= Convert.ToInt64(((TextBox)gvNewsList.Rows[e.RowIndex].Cells[3].Controls[0]).Text);
bubuko.com,布布扣        }

bubuko.com,布布扣        
//序号
bubuko.com,布布扣
        if (!string.IsNullOrEmpty(((TextBox)gvNewsList.Rows[e.RowIndex].Cells[1].Controls[0]).Text))
bubuko.com,布布扣        
{
bubuko.com,布布扣            systemMemberLevelModel.sort 
= Convert.ToInt32(((TextBox)gvNewsList.Rows[e.RowIndex].Cells[1].Controls[0]).Text); 
bubuko.com,布布扣        }

bubuko.com,布布扣        
try
bubuko.com,布布扣        
{
bubuko.com,布布扣            systemMemberLevelBll.Update(systemMemberLevelModel);
bubuko.com,布布扣            gvNewsList.EditIndex 
= -1;
bubuko.com,布布扣            BindData();
bubuko.com,布布扣
bubuko.com,布布扣        }

bubuko.com,布布扣        
catch (System.Data.DataException x)
bubuko.com,布布扣        
{
bubuko.com,布布扣            
throw new System.Data.DataException(x.ToString());
bubuko.com,布布扣        }

bubuko.com,布布扣
bubuko.com,布布扣    }

bubuko.com,布布扣
bubuko.com,布布扣
//点击控件自带的删除按钮时执行的事件
bubuko.com,布布扣
    protected void gvNewsList_RowDeleting(object sender, GridViewDeleteEventArgs e)
bubuko.com,布布扣    
{
bubuko.com,布布扣        
//这里同样是通过这个属性来获取id
bubuko.com,布布扣
        string ids = gvNewsList.DataKeys[e.RowIndex].Value.ToString();
bubuko.com,布布扣        
try
bubuko.com,布布扣        
{
bubuko.com,布布扣            systemMemberLevelBll.Delete(Convert.ToInt64(ids));
bubuko.com,布布扣            BindData();
bubuko.com,布布扣
bubuko.com,布布扣        }

bubuko.com,布布扣        
catch (System.Data.DataException x)
bubuko.com,布布扣        
{
bubuko.com,布布扣            
throw new System.Data.DataException(x.ToString());
bubuko.com,布布扣        }

bubuko.com,布布扣      
bubuko.com,布布扣    }

bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣
//GridView数据绑定 
bubuko.com,布布扣
   protected void BindData()
bubuko.com,布布扣    
{
bubuko.com,布布扣        IList
<SystemMemberLevelModel> list = systemMemberLevelBll.GetAllList();
bubuko.com,布布扣        
this.gvNewsList.DataSource = list;
bubuko.com,布布扣        gvNewsList.DataKeyNames 
= new string[] "id" };//主键
bubuko.com,布布扣
        gvNewsList.DataBind();
bubuko.com,布布扣    }

bubuko.com,布布扣

GridView 编辑修改,布布扣,bubuko.com

GridView 编辑修改

标签:c   class   blog   code   tar   ext   

原文地址:http://www.cnblogs.com/lyl6796910/p/3746636.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!