ASP:GridView没法进行编辑操作

wuha555 2011-11-28 08:43:58
我初学,实在是麻烦各位前辈了。
前台
  
<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>

后台

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)
{

}
}
}
...全文
79 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuha555 2011-11-28
  • 打赏
  • 举报
回复
程序能在浏览器中看,不过就是不能编辑啊??希望能给予指导

25,980

社区成员

发帖
与我相关
我的任务
社区描述
高性能WEB开发
社区管理员
  • 高性能WEB开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧