想在dataGrid中实现对数据库的更新 想通过dataGrid控件对数据库实现 添加 删除 修改 操作请懂的朋友给予帮助 谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这种问题网上很好找的,例如这篇:http://www.chenjiliang.com/Article/View.aspx?ArticleID=1162&TypeID=5 //数据绑定private void BindGrid() { SqlConnection con = new SqlConnection(ConnectionString); con.Open(); string sql = "select [co_id]as[会员号],[co_name]as[公司名],[co_shortname]as[公司简称] from jq_fitment"; SqlCommand com = new SqlCommand(sql, con); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = com; DataSet ds = new DataSet(); da.Fill(ds, "jinqi"); con.Close(); GridView1.DataSource = ds.Tables[0].DefaultView; GridView1.DataBind(); }//数据更新 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { // GridView1.FooterRow.Enabled = false; string co_id = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString()); string co_name = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString()); string co_shortname = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString()); string co_passwd = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.ToString()); string co_location = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text.ToString()); string co_product_1 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text.ToString()); string co_product_2 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text.ToString()); string co_product_3 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[8].Controls[0]).Text.ToString()); string co_product_other = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[9].Controls[0]).Text.ToString()); string co_deputy = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[10].Controls[0]).Text.ToString()); string co_tel = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[11].Controls[0]).Text.ToString()); string co_tel_1 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[12].Controls[0]).Text.ToString()); string co_tel_2 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[13].Controls[0]).Text.ToString()); string co_fax = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[14].Controls[0]).Text.ToString()); string co_email_1 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[15].Controls[0]).Text.ToString()); string co_email_2 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[16].Controls[0]).Text.ToString()); string co_web = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[17].Controls[0]).Text.ToString()); string co_adress = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[18].Controls[0]).Text.ToString()); string co_info = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[19].Controls[0]).Text.ToString()); try { SqlConnection con = new SqlConnection(ConnectionString); con.Open(); string update = "update jq_fitment set co_name='" + co_name + "',co_shortname='" + co_shortname + "',co_passwd='" + co_passwd + "',co_location='" + co_location + "',co_product_1='" + co_product_1 + "',co_product_2='" + co_product_2 + "',co_product_3='" + co_product_3 + "',co_product_other='" + co_product_other + "',co_deputy='" + co_deputy + "',co_tel='" + co_tel + "',co_tel_1='" + co_tel_1 + "',co_tel_2='" + co_tel_2 + "',co_fax='" + co_fax + "',co_email_1='" + co_email_1 + "',co_email_2='" + co_email_2 + "',co_web='" + co_web + "',co_adress='" + co_adress + "',co_info='" + co_info + "'where co_id='" + co_id + "'"; SqlCommand com = new SqlCommand(update, con); int a = com.ExecuteNonQuery(); GridView1.EditIndex = -1; BindGrid(); } catch (Exception ex) { Response.Write("<script> alert ('" + ex.Message + "') </script>"); } }//取消 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { this.GridView1.EditIndex = -1; this.BindGrid(); }//删除protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { try { string a = this.GridView1.Rows[e.RowIndex].Cells[1].Text.ToString(); SqlConnection con = new SqlConnection(ConnectionString); con.Open(); string sql = "delete from jq_fitment where co_id='" + a + "'"; SqlCommand com = new SqlCommand(sql, con); com.ExecuteNonQuery(); this.BindGrid(); } catch (Exception ex) { Response.Write("<script> alert('" + ex.Message + "') </script>"); } }//编辑 protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; BindGrid(); } //数据绑定private void BindGrid() { SqlConnection con = new SqlConnection(ConnectionString); con.Open(); string sql = "select [co_id]as[会员号],[co_name]as[公司名],[co_shortname]as[公司简称] from jq_fitment"; SqlCommand com = new SqlCommand(sql, con); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = com; DataSet ds = new DataSet(); da.Fill(ds, "jinqi"); con.Close(); GridView1.DataSource = ds.Tables[0].DefaultView; GridView1.DataBind(); }//数据更新 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { // GridView1.FooterRow.Enabled = false; string co_id = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString()); string co_name = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString()); string co_shortname = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString()); string co_passwd = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.ToString()); string co_location = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text.ToString()); string co_product_1 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text.ToString()); string co_product_2 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text.ToString()); string co_product_3 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[8].Controls[0]).Text.ToString()); string co_product_other = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[9].Controls[0]).Text.ToString()); string co_deputy = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[10].Controls[0]).Text.ToString()); string co_tel = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[11].Controls[0]).Text.ToString()); string co_tel_1 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[12].Controls[0]).Text.ToString()); string co_tel_2 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[13].Controls[0]).Text.ToString()); string co_fax = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[14].Controls[0]).Text.ToString()); string co_email_1 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[15].Controls[0]).Text.ToString()); string co_email_2 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[16].Controls[0]).Text.ToString()); string co_web = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[17].Controls[0]).Text.ToString()); string co_adress = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[18].Controls[0]).Text.ToString()); string co_info = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[19].Controls[0]).Text.ToString()); try { SqlConnection con = new SqlConnection(ConnectionString); con.Open(); string update = "update jq_fitment set co_name='" + co_name + "',co_shortname='" + co_shortname + "',co_passwd='" + co_passwd + "',co_location='" + co_location + "',co_product_1='" + co_product_1 + "',co_product_2='" + co_product_2 + "',co_product_3='" + co_product_3 + "',co_product_other='" + co_product_other + "',co_deputy='" + co_deputy + "',co_tel='" + co_tel + "',co_tel_1='" + co_tel_1 + "',co_tel_2='" + co_tel_2 + "',co_fax='" + co_fax + "',co_email_1='" + co_email_1 + "',co_email_2='" + co_email_2 + "',co_web='" + co_web + "',co_adress='" + co_adress + "',co_info='" + co_info + "'where co_id='" + co_id + "'"; SqlCommand com = new SqlCommand(update, con); int a = com.ExecuteNonQuery(); GridView1.EditIndex = -1; BindGrid(); } catch (Exception ex) { Response.Write("<script> alert ('" + ex.Message + "') </script>"); } }//取消 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { this.GridView1.EditIndex = -1; this.BindGrid(); }//删除protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { try { string a = this.GridView1.Rows[e.RowIndex].Cells[1].Text.ToString(); SqlConnection con = new SqlConnection(ConnectionString); con.Open(); string sql = "delete from jq_fitment where co_id='" + a + "'"; SqlCommand com = new SqlCommand(sql, con); com.ExecuteNonQuery(); this.BindGrid(); } catch (Exception ex) { Response.Write("<script> alert('" + ex.Message + "') </script>"); } }//编辑 protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; BindGrid(); } 请教一个实际应用的问题 tabcontrol中的tabpage能换位置么? 一个运行时问题?类的实例化过程引起的? datagirdview的更新问题 非常急,这怎么实现。 我公司正在选购一套“企业级”的考试管理系统,请各位介绍几套好产品。 播放器 string 数组的定义方法在内存构造上的区别 Dev控件库中Grid控件中下拉框级联显示 自定义了一个DataGridView的可编辑值的ComboBox列,不能表示DisplayMember 简单加解密算法(大学课本中介绍过的不算) c#应用程序如何登陆计费网关?
http://www.chenjiliang.com/Article/View.aspx?ArticleID=1162&TypeID=5
private void BindGrid()
{
SqlConnection con = new SqlConnection(ConnectionString);
con.Open();
string sql = "select [co_id]as[会员号],[co_name]as[公司名],[co_shortname]as[公司简称]
from jq_fitment";
SqlCommand com = new SqlCommand(sql, con);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = com;
DataSet ds = new DataSet();
da.Fill(ds, "jinqi");
con.Close();
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
//数据更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// GridView1.FooterRow.Enabled = false;
string co_id = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString());
string co_name = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString());
string co_shortname = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString());
string co_passwd = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.ToString());
string co_location = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text.ToString());
string co_product_1 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text.ToString());
string co_product_2 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text.ToString());
string co_product_3 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[8].Controls[0]).Text.ToString());
string co_product_other = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[9].Controls[0]).Text.ToString());
string co_deputy = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[10].Controls[0]).Text.ToString());
string co_tel = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[11].Controls[0]).Text.ToString());
string co_tel_1 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[12].Controls[0]).Text.ToString());
string co_tel_2 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[13].Controls[0]).Text.ToString());
string co_fax = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[14].Controls[0]).Text.ToString());
string co_email_1 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[15].Controls[0]).Text.ToString());
string co_email_2 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[16].Controls[0]).Text.ToString());
string co_web = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[17].Controls[0]).Text.ToString());
string co_adress = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[18].Controls[0]).Text.ToString());
string co_info = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[19].Controls[0]).Text.ToString()); try
{
SqlConnection con = new SqlConnection(ConnectionString);
con.Open();
string update = "update jq_fitment set co_name='" + co_name + "',co_shortname='" + co_shortname + "',co_passwd='" + co_passwd + "',co_location='" + co_location + "',co_product_1='" + co_product_1 + "',co_product_2='" + co_product_2 + "',co_product_3='" + co_product_3 + "',co_product_other='" + co_product_other + "',co_deputy='" + co_deputy + "',co_tel='" + co_tel + "',co_tel_1='" + co_tel_1 + "',co_tel_2='" + co_tel_2 + "',co_fax='" + co_fax + "',co_email_1='" + co_email_1 + "',co_email_2='" + co_email_2 + "',co_web='" + co_web + "',co_adress='" + co_adress + "',co_info='" + co_info + "'where co_id='" + co_id + "'";
SqlCommand com = new SqlCommand(update, con);
int a = com.ExecuteNonQuery();
GridView1.EditIndex = -1;
BindGrid();
}
catch (Exception ex)
{
Response.Write("<script> alert ('" + ex.Message + "') </script>");
} }
//取消
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;
this.BindGrid();
}
//删除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
string a = this.GridView1.Rows[e.RowIndex].Cells[1].Text.ToString();
SqlConnection con = new SqlConnection(ConnectionString);
con.Open();
string sql = "delete from jq_fitment where co_id='" + a + "'";
SqlCommand com = new SqlCommand(sql, con);
com.ExecuteNonQuery();
this.BindGrid();
}
catch (Exception ex)
{
Response.Write("<script> alert('" + ex.Message + "') </script>");
}
}
//编辑
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGrid();
}
private void BindGrid()
{
SqlConnection con = new SqlConnection(ConnectionString);
con.Open();
string sql = "select [co_id]as[会员号],[co_name]as[公司名],[co_shortname]as[公司简称]
from jq_fitment";
SqlCommand com = new SqlCommand(sql, con);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = com;
DataSet ds = new DataSet();
da.Fill(ds, "jinqi");
con.Close();
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
//数据更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// GridView1.FooterRow.Enabled = false;
string co_id = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString());
string co_name = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString());
string co_shortname = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString());
string co_passwd = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.ToString());
string co_location = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text.ToString());
string co_product_1 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text.ToString());
string co_product_2 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text.ToString());
string co_product_3 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[8].Controls[0]).Text.ToString());
string co_product_other = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[9].Controls[0]).Text.ToString());
string co_deputy = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[10].Controls[0]).Text.ToString());
string co_tel = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[11].Controls[0]).Text.ToString());
string co_tel_1 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[12].Controls[0]).Text.ToString());
string co_tel_2 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[13].Controls[0]).Text.ToString());
string co_fax = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[14].Controls[0]).Text.ToString());
string co_email_1 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[15].Controls[0]).Text.ToString());
string co_email_2 = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[16].Controls[0]).Text.ToString());
string co_web = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[17].Controls[0]).Text.ToString());
string co_adress = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[18].Controls[0]).Text.ToString());
string co_info = Server.HtmlEncode(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[19].Controls[0]).Text.ToString()); try
{
SqlConnection con = new SqlConnection(ConnectionString);
con.Open();
string update = "update jq_fitment set co_name='" + co_name + "',co_shortname='" + co_shortname + "',co_passwd='" + co_passwd + "',co_location='" + co_location + "',co_product_1='" + co_product_1 + "',co_product_2='" + co_product_2 + "',co_product_3='" + co_product_3 + "',co_product_other='" + co_product_other + "',co_deputy='" + co_deputy + "',co_tel='" + co_tel + "',co_tel_1='" + co_tel_1 + "',co_tel_2='" + co_tel_2 + "',co_fax='" + co_fax + "',co_email_1='" + co_email_1 + "',co_email_2='" + co_email_2 + "',co_web='" + co_web + "',co_adress='" + co_adress + "',co_info='" + co_info + "'where co_id='" + co_id + "'";
SqlCommand com = new SqlCommand(update, con);
int a = com.ExecuteNonQuery();
GridView1.EditIndex = -1;
BindGrid();
}
catch (Exception ex)
{
Response.Write("<script> alert ('" + ex.Message + "') </script>");
} }
//取消
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;
this.BindGrid();
}
//删除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
string a = this.GridView1.Rows[e.RowIndex].Cells[1].Text.ToString();
SqlConnection con = new SqlConnection(ConnectionString);
con.Open();
string sql = "delete from jq_fitment where co_id='" + a + "'";
SqlCommand com = new SqlCommand(sql, con);
com.ExecuteNonQuery();
this.BindGrid();
}
catch (Exception ex)
{
Response.Write("<script> alert('" + ex.Message + "') </script>");
}
}
//编辑
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGrid();
}