前言:在GRIDVIEW的使用中,我们经常会碰到这样的需求:GRIDVIEW虽然自带了“编辑”的功能,但是有时候我们不愿意在girdview直接进行修改 ,这时就得自己通过RowCommand事件来做这个编辑的功能

例子如下:这个是我项目中的教室的占用情况表,我要修改一天内几个时间段的使用情况,因为有特殊的需求,那些信息我是用模板列里面用label控件绑定的,修改时前几列是不需要动的,综合考虑,我没有用gridview自带的编辑功能,我通过一个按钮(放在隐藏列里,我通过双击或者右健菜单激发该事件)

这是双击第一条记录后出现的编辑界面:

那么这些数据已经在GRIDVIEW读出来了,编辑时我们就没有必要再从数据库中去读数据,直接将GRIDVIEW里的数据给拿过来绑定上就是,那么如何实现呢,后台代码如下:

protected void SmartGridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    
{
        
if (e.CommandName == "EEdit")
        
{
            
string roomid = e.CommandArgument.ToString().Trim();
            GridViewRow row 
= ((Button)e.CommandSource).Parent.Parent as GridViewRow;//获得双击的那一行,这是关键部分
            string zc = ((Label)row.Cells[2].FindControl("Label2")).Text.Trim();
            
string xq = ((Label)row.Cells[3].FindControl("Label3")).Text.Trim();
            lblRoomID.Text 
= roomid;
            lblWeek.Text 
= zc;
            lblDay.Text 
= xq;
            ddl1.SelectedValue 
= ((Label)row.Cells[4].FindControl("Label4")).Text.Trim();
            ddl2.SelectedValue 
= ((Label)row.Cells[5].FindControl("Label5")).Text.Trim();
            ddl3.SelectedValue 
= ((Label)row.Cells[6].FindControl("Label6")).Text.Trim();
            ddl4.SelectedValue 
= ((Label)row.Cells[7].FindControl("lblIs1")).Text.Trim();
            ddl5.SelectedValue 
= ((Label)row.Cells[8].FindControl("Label7")).Text.Trim();
          
            Panel1.Visible 
= false;
            Panel2.Visible 
= true;

        }

    }

 

arrow
arrow
    全站熱搜

    alen1985 發表在 痞客邦 留言(0) 人氣()