GridView的增删改操作

 

1 RowDataBound 事件

在创建gridView控件时,必须先为GridView的每一行创建一个GridViewRow对象,创建每一行时,将引发一个RowCreated事件;当行创建完毕,每一行GridViewRow就要绑定数据源中的数据,当绑定完成后,将引发RowDataBound事件。如果说我们可以利用RowCreated事件来控制每一行绑定的控件,那么我们同样可以利用RowDataBound事件来控制每一行绑定的数据。

<pre class="csharp" name="code">rotected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    //移动到此行就变色
                    e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#EBEBEB'");
                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");

                    //删除前的确认一下
                    if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                    {
                        ((LinkButton)e.Row.Cells[3].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:\"" + GridView1.DataKeys[e.Row.RowIndex].Values[0].ToString() + "\"吗?')");
                    }
                }             
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 

详细参考 http://blog.sina.com.cn/s/blog_690892850100uvkq.html

2 Edit事件

 


3 针对删操作,可以使用绑定主键的方法

   前台操作:在GridView 属性绑定可以使用

          DataKeyNames="FileName,ExtName“

 

 

   后台操作:

           string  filename = GridView1.DataKeys[e.RowIndex].Values[0].ToString() ;
           string extname = GridView1.DataKeys[e.RowIndex].Values[1].ToString();

 

关键点:对于DataKeyNames绑定的理解。

 

3 更新数据

 前台:

    <Columns>
            <asp:TemplateField HeaderText="#"> 
                    <ItemTemplate> 
                        <font color="#FF3300"><%# Container.DataItemIndex + 1%> </font>
                    </ItemTemplate> 
             </asp:TemplateField> 
            <asp:CommandField HeaderText="选择" ShowSelectButton="True" />
            <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
            <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />

         <asp:BoundField DataField="Customer" HeaderText="Customer" 
                    SortExpression="Customer" />
                <asp:TemplateField HeaderText="MCID" SortExpression="MCID">//模板类型
                    <EditItemTemplate>
                        <asp:TextBox ID="TXT_MCID" runat="server" Text='<%# Bind("MCID") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("MCID") %>'></asp:Label>
                    </ItemTemplate>
            </asp:TemplateField>
                <asp:BoundField DataField="LotID" HeaderText="LotID" 
                    SortExpression="LotID" />                            //普通类型DataControlRowType.DataRow
                <asp:BoundField DataField="CustLotID" HeaderText="CustLotID" 
                    SortExpression="CustLotID" />
                <asp:BoundField DataField="DeviceID" HeaderText="DeviceID" 
                    SortExpression="DeviceID" />
                <asp:BoundField DataField="FileName" HeaderText="FileName" //只读类型
                    SortExpression="FileName" ReadOnly="True" />
                <asp:BoundField DataField="ExtName" HeaderText="ExtName" 
                    SortExpression="ExtName" ReadOnly="True" /> 
                <asp:BoundField DataField="IsValid" HeaderText="IsValid" 
                    SortExpression="IsValid" />
                <asp:BoundField DataField="IsMainFile" HeaderText="IsMainFile" 
                    SortExpression="IsMainFile" />
                <asp:BoundField DataField="IsExists" HeaderText="IsExists" 
                    SortExpression="IsExists" />
                <asp:BoundField DataField="CurrentPath" HeaderText="CurrentPath" 
                    SortExpression="CurrentPath" />
                <asp:BoundField DataField="ActionTime" HeaderText="ActionTime" 
                    SortExpression="ActionTime" />
                     
        </Columns>

后台操作:

 

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try {
                //普通
                string customer = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();
                //模板类型
                string mcid = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].FindControl("TXT_MCID")).Text; 
                string lotid = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString();
                string custlotid = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text.ToString();
                string deviceid = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[8].Controls[0])).Text.ToString();

               //read only 只读类型
                string filename = GridView1.Rows[e.RowIndex].Cells[9].Text.ToString();
                string extension =  GridView1.Rows[e.RowIndex].Cells[10].Text.ToString();

                // s,string sDeviceID,string sLotID,string sCustLotID,ref string error
                string error = "";
             
                if (!myDB.UpdateFileData(customer, filename, extension, mcid, deviceid, lotid, custlotid, ref  error))
                {
                    MyDAL.MyUtility.ShowMsg_Normal(this.Page, "alert('Fail');");
                }
                else
                {
                    MyDAL.MyUtility.ShowMsg_Normal(this.Page, "alert('OK');");
                }
                GridView1.EditIndex = -1;
                bind();
              
            }
            catch(Exception ex)
            { throw ex; }
          
        }

 

 

 

 

 





  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值