ASP:GridView没法进行编辑操作
时间:2011-11-28
来源:互联网
我初学,实在是麻烦各位前辈了。
前台
C# code
后台
C# code
前台
C# code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CellPadding="4" ForeColor="#003366" GridLines="Both" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit"> <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> <Columns> <asp:BoundField DataField="name" HeaderText="用户ID" /> <asp:BoundField DataField="subject" HeaderText="用户姓名" /> <asp:BoundField DataField="createtime" HeaderText="性别" /> <asp:CommandField HeaderText="选择" ShowSelectButton="True" /> <asp:CommandField HeaderText="编辑" ShowEditButton="True" /> <asp:CommandField HeaderText="删除" ShowDeleteButton="True" /> </Columns> <RowStyle ForeColor="#000066" /> <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" /> <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> </asp:GridView>
后台
C# code
namespace test1 { public partial class WebForm1 : System.Web.UI.Page { MySqlConnection myConnection; MySqlCommand myCommand; string connectString = "userid=root;pwd=shouneng;host=localhost;database=test1"; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bind(); } } public void bind() { myCommand = new MySqlCommand("select * from teacher"); myConnection=new MySqlConnection(connectString); MySqlDataAdapter myAdapter = new MySqlDataAdapter(myCommand.CommandText, myConnection); DataSet myDataSet = new DataSet(); myAdapter.Fill(myDataSet); this.GridView1.DataSource = myDataSet; this.GridView1.DataKeyNames =new String[]{ "name"}; this.GridView1.DataBind(); myConnection.Close(); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { this.GridView1.EditIndex = -1; bind(); } protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { string command = "delete from teacher where id="+GridView1.DataKeys[e.RowIndex].Value.ToString(); myCommand = new MySqlCommand(command); myConnection = new MySqlConnection(connectString); myCommand.ExecuteNonQuery(); myConnection.Close(); bind(); } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; bind(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { string command = "update teacher set name=" + GridView1.Rows[e.RowIndex].Cells[0].ToString().Trim() + "subject" + GridView1.Rows[e.RowIndex].Cells[1].ToString().Trim() + "createtime" + GridView1.Rows[e.RowIndex].Cells[2].ToString().Trim(); } public override void VerifyRenderingInServerForm(Control control) { } } }
作者: wuha555 发布时间: 2011-11-28
程序能在浏览器中看,不过就是不能编辑啊??希望能给予指导
作者: wuha555 发布时间: 2011-11-28
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28