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; }
}