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();
}
}
}
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();
}
}
}
相关文章推荐
- GridView实现双击进行编辑,更新
- gridView单行编辑功能的实现
- GridView实现双击进行编辑,更新
- GridView实现双击进行编辑,更新
- GridView实现双击进行编辑,更新
- GridView实现双击进行编辑,更新
- GridView实现双击进行编辑,更新2
- asp.net根据条件动态生成GridView,并动态绑定列,且可对其进行编辑的实现
- asp.net根据条件动态生成GridView,并动态绑定列,且可对其进行编辑的实现
- asp.net根据条件动态生成GridView,并动态绑定列,且可对其进行编辑的实现
- GridView如何实现双击行进行编辑,更新
- Gridview 选择某一行进行编辑,或选择某一行删除 获取参数
- andorid gridview 单行横向显示 横向滚动实现
- 使用Repeater实现类似GridView编辑功能
- gridView(二)实现手动数据源的编辑、删除
- 基于Qt的截图工具,实现截图后进行编辑
- android 单行gridview横向滑动的实现
- GridView实现编辑,选择删除,分页功能
- GridView 实现自定义分页、排序、查询、添加、编辑、多选删除