您的位置:首页 > 其它

gridview 实现在GridView内部进行单行编辑

2008-04-07 15:02 399 查看
对于经常变更的字段我们利用这个效果进行数据更新。有点类似excel的效果。只是一个简单的应用,根据需要可以扩展。
HTML;


<html xmlns="http://www.w3.org/1999/xhtml">


<head runat="server">


<title>无标题页</title>


</head>


<body>


<form id="form1" runat="server">


<div>


<asp:GridView ID="gvshow" runat="server" AutoGenerateColumns ="False"


CellPadding="4" ForeColor="#333333" GridLines="None" HorizontalAlign="Center"


Width="548px"


onrowediting="gvshow_RowEditing" onrowcommand="gvshow_RowCommand"


onrowcreated="gvshow_RowCreated" >


<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />


<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />


<Columns >


<asp:BoundField DataField ="gwid" HeaderText ="GWID" />


<asp:TemplateField HeaderText="MachType">


<ItemTemplate >


<asp:TextBox ID="txtmachType" runat ="server" Text='<%#Eval("machtype") %>' Enabled ="false" ></asp:TextBox>


</ItemTemplate>


</asp:TemplateField>


<asp:TemplateField HeaderText ="ISOK" >


<ItemTemplate >


<asp:TextBox ID="txtisok" runat ="server" Text='<%#Eval("isok") %>' Enabled ="false"></asp:TextBox>


</ItemTemplate>


</asp:TemplateField>


<asp:TemplateField >


<ItemTemplate >


<asp:LinkButton ID="btnEdit" runat="server" Text="Edit" CommandName ="Edit" ></asp:LinkButton>


</ItemTemplate>


</asp:TemplateField>


<asp:TemplateField >


<ItemTemplate >


<asp:Button ID="btnSave" runat="server" Text=" Save " CommandName ="Save" />


</ItemTemplate>


</asp:TemplateField>


</Columns>


<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />


<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />


<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />


<AlternatingRowStyle BackColor="White" />


</asp:GridView>


</div>


</form>


</body>


</html>

CS:


using System;


using System.Collections;


using System.Configuration;


using System.Data;


using System.Linq;


using System.Web;


using System.Web.Security;


using System.Web.UI;


using System.Web.UI.HtmlControls;


using System.Web.UI.WebControls;


using System.Web.UI.WebControls.WebParts;


using System.Xml.Linq;


using System.Data.SqlClient;




public partial class ModifyText : System.Web.UI.Page




...{


protected void Page_Load(object sender, EventArgs e)




...{


if (!IsPostBack)




...{


SetBind();


}


}


public void SetBind()




...{


string connstr = ConfigurationManager.ConnectionStrings["ConnStr"].ToString();


using (SqlConnection conn = new SqlConnection(connstr))




...{


conn.Open();


string sql = "select * from allinfor";


SqlDataAdapter ada = new SqlDataAdapter(sql, conn);


DataSet ds = new DataSet();


ada.Fill(ds);


gvshow.DataSource = ds.Tables[0];


gvshow.DataBind();


}


}




protected void gvshow_RowEditing(object sender, GridViewEditEventArgs e)




...{


int i = e.NewEditIndex;




(gvshow.Rows[i].FindControl("txtmachType") as TextBox).Enabled = true; //激活要编辑的TextBox




}


protected void gvshow_RowCommand(object sender, GridViewCommandEventArgs e)




...{


if (e.CommandName == "Save") //保存更新数据




...{


int index = Convert.ToInt32(e.CommandArgument);


string sNewMachtype = (gvshow.Rows[index].FindControl("txtmachType") as TextBox).Text;


string sGwid = gvshow.Rows[index].Cells[0].Text.ToString();


string sql = "update allinfor set machtype='"+sNewMachtype+"' where gwid='"+sGwid+"'";




string connstr = ConfigurationManager.ConnectionStrings["ConnStr"].ToString();


using (SqlConnection conn = new SqlConnection(connstr))




...{


conn.Open();


SqlCommand cmd = new SqlCommand(sql,conn);


cmd.ExecuteNonQuery();


SetBind();


}


}


}


protected void gvshow_RowCreated(object sender, GridViewRowEventArgs e)




...{


//为btnsave 赋予rowindex


if (e.Row.RowType == DataControlRowType.DataRow)




...{


Button btn = (Button)e.Row.Cells[4].FindControl("btnSave");


btn.CommandArgument = e.Row.RowIndex.ToString();


}


}


}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: