GridView绑定MySQL实现增删查改

klsd222 2010-06-21 09:39:27
如题目。我已经实现连接了,能把数据读到GridView。还有增删改怎么实现啊。
本来GridView内置有这些功能的,但是是SQLServer,跟MYSQL不一样。所以不能直接用!

还有就是怎样才能选中GridView的一项啊。要怎样设置?我现在不能鼠标单击某一行某一行就被选中?

那位可以帮帮我啊!!?

我用的是C#
...全文
433 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
tianya0370 2010-06-21
  • 打赏
  • 举报
回复
这些的也太麻烦了
直接用sqlcommandbuilder不就行了
generhappy 2010-06-21
  • 打赏
  • 举报
回复
参考下:

protected void GridView1_SelectedIndexChanged(object sender, GridViewPageEventArgs e)
{
//设置当前页的索引值
GridView1.PageIndex = e.NewPageIndex;
Databind(strsql);
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)//删除
{
string strsql1 = "delete from data_mold where id_code=" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value) + "";
SqlCommand sqlcom = new SqlCommand(strsql1, con);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = sqlcom;
con.Open();
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds;
con.Close();
GridView1.EditIndex = -1;
Databind(strsql);
//Response.Write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
//Response.Write("<Script>alert('删除成功!')</Script>");
//Response.Redirect(Request.Url.ToString());
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)//编辑
{
GridView1.EditIndex = e.NewEditIndex;
Databind(strsql);
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)//更新
{
string strsql1 = "update data_mold set customer_name='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim() + "',company_name='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',product_name='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',mold_name='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "',fees='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim() + "',collection_date='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim() + "',collection_way='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString().Trim() + "',notes='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text.ToString().Trim() + "',first_shipping_date='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[8].Controls[0])).Text.ToString().Trim() + "',fees_kind='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[10].Controls[0])).Text.ToString().Trim() + "' where id_code='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
SqlCommand sqlcom = new SqlCommand(strsql1, con);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = sqlcom;
con.Open();
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds;
con.Close();
GridView1.EditIndex = -1;
Databind(strsql);

SqlConnection sqlcon = new SqlConnection("server=localhost;database=moldsystem;uid=sa;pwd=sa");
SqlCommand sqlcom1 = new SqlCommand();
sqlcom1.CommandType = CommandType.StoredProcedure;
sqlcom1.CommandText = "aaa";
SqlDataAdapter adp = new SqlDataAdapter(sqlcom1.CommandText.ToString(), sqlcon);
sqlcon.Open();
DataSet dst = new DataSet();
adp.Fill(dst, "data_mold");
this.GridView1.DataSource = dst;
sqlcon.Close();
GridView1.EditIndex = -1;
Databind(strsql);

}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)//取消
{
GridView1.EditIndex = -1;
Databind(strsql);
}

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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