C#中的DataGrid怎样执行查询\添加\修改,并保存到数据库?
C#中的DataGrid怎样执行查询\添加\修改,并保存到数据库?我是用的DataGrid控件自带的自动连接到数据库的.我想再加上个查询\添加\修改\删除的几个Butto...
C#中的DataGrid怎样执行查询\添加\修改,并保存到数据库?
我是用的DataGrid控件自带的自动连接到数据库的.
我想再加上个查询\添加\修改\删除的几个Button,实现上述功能,怎么办?
新建一排按钮要写的代码是什么? 展开
我是用的DataGrid控件自带的自动连接到数据库的.
我想再加上个查询\添加\修改\删除的几个Button,实现上述功能,怎么办?
新建一排按钮要写的代码是什么? 展开
6个回答
展开全部
你点击GRIDVIEW右上角的那个小三角,然后点编辑列,在弹出的窗口中添加一个叫做COMMANDFILE的列,下这个列下面有一个叫做“编辑、更新、去消”它是有来修改一列内容的。还有一个叫做“删除”的,这个不用我说是做什么的了吧。要想添加一列有点儿麻烦,这个要你自己写,不过你可新添加一个列,放上个BUTTON,然后让它打开一个DetailsView进行数据添加。也是一个不错的选择啊。
补存:你可把DetailsView放到一个新的页面中,那在添加数据的按钮就写连接这个页面的代码就行了,如果你想在一个页面内显示的话,你可以写上DetailsView显示的代码,DetailsView.Enable=true;当然这个控件在你打开网页是隐藏的。
补存:你可把DetailsView放到一个新的页面中,那在添加数据的按钮就写连接这个页面的代码就行了,如果你想在一个页面内显示的话,你可以写上DetailsView显示的代码,DetailsView.Enable=true;当然这个控件在你打开网页是隐藏的。
展开全部
先在DataGrid数据源里面看看有没有插入/更新/删除的语句,没有的话要加上,DataGrid.UpdateCommandName 这里
然后允许此控件的删除/更新操作就行了,或者新建一列按钮列,手动指定数据更新操作
然后允许此控件的删除/更新操作就行了,或者新建一列按钮列,手动指定数据更新操作
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
e
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在后台写代码和SQL语句.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace FineYYWeb.Competence
{
/// <summary>
/// DWBMset 的摘要说明。
/// </summary>
public class DWBMset : System.Web.UI.Page
{
public SqlConnection conn;
public SqlCommand comm;
public SqlDataAdapter dap;
public DataSet myDataSet;
protected System.Web.UI.HtmlControls.HtmlInputText user_Text;
protected System.Web.UI.HtmlControls.HtmlInputText number_Text;
protected System.Web.UI.HtmlControls.HtmlInputText audit_Text;
protected System.Web.UI.WebControls.Button submitButton;
protected System.Web.UI.HtmlControls.HtmlInputText name_Text;
protected System.Web.UI.HtmlControls.HtmlInputText auditNum_Text;
protected System.Web.UI.WebControls.DataGrid myDataGrid;
public string m_connString="user id=sa;password=5518008;initial catalog=jxhpt;data source=127.0.0.1;Connect Timeout=30";
string str_qxbm=null;
string loginName=null;
string str_UPName=null;
protected System.Web.UI.HtmlControls.HtmlInputText Password1;
protected System.Web.UI.HtmlControls.HtmlInputText DWZDBM_Text;
protected System.Web.UI.HtmlControls.HtmlInputText JGDM_Text;
string str_UPJH=null;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
//myHashtable(); //判断是否登陆,并传递用户的相关信息
Grid_Bind(); //myDataGrid数据源
}
}
public void myHashtable() //登陆_数据源
{
if(Session["ht_user_msg"]!=null)
{
Hashtable myHT = null;
myHT = (Hashtable)Session["ht_user_msg"];
loginName = myHT["DLNAME"].ToString(); //用户名
str_UPName = myHT["TRUENAME"].ToString();//用户_姓名
str_UPJH = myHT["DWZDBM"].ToString(); //用户_单位中队编码
str_qxbm=myHT["QXBM"].ToString();
if(str_qxbm!="002")
{ Response.Redirect("../Error.aspx?ID=2"); }
}
else
{ Response.Redirect("../Error.aspx?ID=1"); }
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.submitButton.Click += new System.EventHandler(this.submitButton_Click);
this.myDataGrid.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.myDataGrid_PageIndexChanged);
this.myDataGrid.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.myDataGrid_CancelCommand);
this.myDataGrid.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.myDataGrid_EditCommand);
this.myDataGrid.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.myDataGrid_UpdateCommand);
this.myDataGrid.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.myDataGrid_DeleteCommand);
this.myDataGrid.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.myDataGrid_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
public void Grid_Bind() //myDataGrid数据源
{
string sqlString="select * from SP_JJH_DICT_DWBM";
dap=new SqlDataAdapter(sqlString,m_connString);
myDataSet=new DataSet();
dap.Fill(myDataSet);
myDataGrid.DataSource=myDataSet.Tables[0].DefaultView;
myDataGrid.DataBind();
}
private void submitButton_Click(object sender, System.EventArgs e)
{
string user = user_Text.Value.Trim(); //辖区编码
string name = name_Text.Value.Trim(); //单位名称
string number = number_Text.Value.Trim(); //单位部门编码
string password = Password1.Value.Trim(); //Password1
string strJGDM = JGDM_Text.Value.Trim();
string strDWZDBM = DWZDBM_Text.Value.Trim(); //单位中队编码
string auditNum = auditNum_Text.Value.Trim();//中队名称
if(user=="" || name=="" || number=="" || password==""|| auditNum=="" ||strJGDM=="" ||strDWZDBM=="")
{
this.RegisterStartupScript("js","<script>alert('请输入完整的信息!');</script>");
return;
}
//查询 单位名称 是否存在
string sqlString = "select XQBM from SP_JJH_DICT_DWBM where JGDM='"+strJGDM+"'";
dap=new SqlDataAdapter(sqlString,m_connString);
myDataSet=new DataSet();
dap.Fill(myDataSet);
if (myDataSet.Tables[0].Rows.Count == 0) //用户不存在
{
sqlString = "insert into SP_JJH_DICT_DWBM (XQBM,DW,DWBMBM,BM,JGDM,DWZDBM,ZD) ";
sqlString += " values('"+user+"','"+name+"','"+number+"','"+password+"','"+strJGDM+"','"+strDWZDBM+"','"+auditNum+"')";
//dap=new SqlDataAdapter(sqlString,m_connString);
//dap.InsertCommand();
conn = new SqlConnection(m_connString);
comm = new SqlCommand(sqlString,conn);
if(conn.State.ToString()=="Closed" )conn.Open();
comm.ExecuteNonQuery();
comm.Dispose();
conn.Close();
conn.Dispose();
Grid_Bind(); //myDataGrid数据源
}
else
{
this.RegisterStartupScript("js","<script>alert('机关代码已存在!');</script>");
}
}
// private void myDataGrid_SelectedIndexChanged(object sender, System.EventArgs e)
// {
// user_Text.Value = myDataGrid.SelectedItem.Cells[3].Text.Trim();//.Replace(" ","");
// name_Text.Value = myDataGrid.SelectedItem.Cells[4].Text.Trim();//.Replace(" ","");
// number_Text.Value = myDataGrid.SelectedItem.Cells[5].Text.Trim();//.Replace(" ","");
// //loginNum_Text.Value = myDataGrid.SelectedItem.Cells[4].Text.Trim();
// Password1.Value = myDataGrid.SelectedItem.Cells[6].Text.Trim();
// //departNum_Text.SelectedValue = myDataGrid.SelectedItem.Cells[8].Text.Trim();
// audit_Text.Value = myDataGrid.SelectedItem.Cells[9].Text.Trim();
// auditNum_Text.Value = myDataGrid.SelectedItem.Cells[10].Text.Trim();
// }
private void myDataGrid_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
myDataGrid.CurrentPageIndex=e.NewPageIndex;
Grid_Bind();
}
//删除
private void myDataGrid_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string id=e.Item.Cells[2].Text;
string sql="delete from SP_JJH_DICT_DWBM where ID='"+id+"'";
SqlConnection co=new SqlConnection(m_connString);
SqlCommand com=new SqlCommand(sql,co);
co.Open();
com.ExecuteNonQuery();
com.Dispose();
co.Close();
co.Dispose();
this.Grid_Bind();
}
// 修改
private void myDataGrid_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
myDataGrid.EditItemIndex = e.Item.ItemIndex;
Grid_Bind();
}
//取消修改
private void myDataGrid_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
myDataGrid.EditItemIndex = -1;
Grid_Bind();
}
//更新
private void myDataGrid_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string id=myDataGrid.DataKeys[e.Item.ItemIndex].ToString();
TextBox tbXQBM=(TextBox)e.Item.Cells[3].Controls[0];
string strXQBM= tbXQBM.Text.Trim();
TextBox tbDW=(TextBox)e.Item.Cells[4].Controls[0];
string strDW = tbDW.Text.Trim();
TextBox tbDWBMBM=(TextBox)e.Item.Cells[5].Controls[0];
string strDWBMBM = tbDWBMBM.Text.Trim();
TextBox tbBM=(TextBox)e.Item.Cells[6].Controls[0];
string strBM = tbBM.Text.Trim();
TextBox tbJGDM=(TextBox)e.Item.Cells[7].Controls[0];
string strJGDM = tbJGDM.Text.Trim();
TextBox tbDWZDBM=(TextBox)e.Item.Cells[8].Controls[0];
string strDWZDBM = tbDWZDBM.Text.Trim();
TextBox tbZD=(TextBox)e.Item.Cells[9].Controls[0];
string strZD = tbZD.Text.Trim();
string MYsqlString = "update SP_JJH_DICT_DWBM set XQBM='"+strXQBM+"',DW='"+strDW+"',DWBMBM='"+strDWBMBM+"',BM='"+strBM+"',JGDM='"+strJGDM+"',DWZDBM='"+strDWZDBM+"',ZD='"+strZD+"' where ID='"+id+"'";
conn = new SqlConnection(m_connString);
comm = new SqlCommand(MYsqlString,conn);
if(conn.State.ToString()=="Closed" )conn.Open();
comm.ExecuteNonQuery();
comm.Dispose();
conn.Close();
conn.Dispose();
myDataGrid.EditItemIndex = -1;
Grid_Bind();
}
//设置编辑模式的 TextBox 的宽度
private void myDataGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.EditItem)
{
((TextBox)e.Item.Cells[3].Controls[0]).Width=90;
((TextBox)e.Item.Cells[4].Controls[0]).Width=170;
((TextBox)e.Item.Cells[5].Controls[0]).Width=90;
((TextBox)e.Item.Cells[6].Controls[0]).Width=90;
((TextBox)e.Item.Cells[7].Controls[0]).Width=90;
((TextBox)e.Item.Cells[8].Controls[0]).Width=90;
((TextBox)e.Item.Cells[9].Controls[0]).Width=90;
}
}
}
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace FineYYWeb.Competence
{
/// <summary>
/// DWBMset 的摘要说明。
/// </summary>
public class DWBMset : System.Web.UI.Page
{
public SqlConnection conn;
public SqlCommand comm;
public SqlDataAdapter dap;
public DataSet myDataSet;
protected System.Web.UI.HtmlControls.HtmlInputText user_Text;
protected System.Web.UI.HtmlControls.HtmlInputText number_Text;
protected System.Web.UI.HtmlControls.HtmlInputText audit_Text;
protected System.Web.UI.WebControls.Button submitButton;
protected System.Web.UI.HtmlControls.HtmlInputText name_Text;
protected System.Web.UI.HtmlControls.HtmlInputText auditNum_Text;
protected System.Web.UI.WebControls.DataGrid myDataGrid;
public string m_connString="user id=sa;password=5518008;initial catalog=jxhpt;data source=127.0.0.1;Connect Timeout=30";
string str_qxbm=null;
string loginName=null;
string str_UPName=null;
protected System.Web.UI.HtmlControls.HtmlInputText Password1;
protected System.Web.UI.HtmlControls.HtmlInputText DWZDBM_Text;
protected System.Web.UI.HtmlControls.HtmlInputText JGDM_Text;
string str_UPJH=null;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
//myHashtable(); //判断是否登陆,并传递用户的相关信息
Grid_Bind(); //myDataGrid数据源
}
}
public void myHashtable() //登陆_数据源
{
if(Session["ht_user_msg"]!=null)
{
Hashtable myHT = null;
myHT = (Hashtable)Session["ht_user_msg"];
loginName = myHT["DLNAME"].ToString(); //用户名
str_UPName = myHT["TRUENAME"].ToString();//用户_姓名
str_UPJH = myHT["DWZDBM"].ToString(); //用户_单位中队编码
str_qxbm=myHT["QXBM"].ToString();
if(str_qxbm!="002")
{ Response.Redirect("../Error.aspx?ID=2"); }
}
else
{ Response.Redirect("../Error.aspx?ID=1"); }
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.submitButton.Click += new System.EventHandler(this.submitButton_Click);
this.myDataGrid.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.myDataGrid_PageIndexChanged);
this.myDataGrid.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.myDataGrid_CancelCommand);
this.myDataGrid.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.myDataGrid_EditCommand);
this.myDataGrid.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.myDataGrid_UpdateCommand);
this.myDataGrid.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.myDataGrid_DeleteCommand);
this.myDataGrid.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.myDataGrid_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
public void Grid_Bind() //myDataGrid数据源
{
string sqlString="select * from SP_JJH_DICT_DWBM";
dap=new SqlDataAdapter(sqlString,m_connString);
myDataSet=new DataSet();
dap.Fill(myDataSet);
myDataGrid.DataSource=myDataSet.Tables[0].DefaultView;
myDataGrid.DataBind();
}
private void submitButton_Click(object sender, System.EventArgs e)
{
string user = user_Text.Value.Trim(); //辖区编码
string name = name_Text.Value.Trim(); //单位名称
string number = number_Text.Value.Trim(); //单位部门编码
string password = Password1.Value.Trim(); //Password1
string strJGDM = JGDM_Text.Value.Trim();
string strDWZDBM = DWZDBM_Text.Value.Trim(); //单位中队编码
string auditNum = auditNum_Text.Value.Trim();//中队名称
if(user=="" || name=="" || number=="" || password==""|| auditNum=="" ||strJGDM=="" ||strDWZDBM=="")
{
this.RegisterStartupScript("js","<script>alert('请输入完整的信息!');</script>");
return;
}
//查询 单位名称 是否存在
string sqlString = "select XQBM from SP_JJH_DICT_DWBM where JGDM='"+strJGDM+"'";
dap=new SqlDataAdapter(sqlString,m_connString);
myDataSet=new DataSet();
dap.Fill(myDataSet);
if (myDataSet.Tables[0].Rows.Count == 0) //用户不存在
{
sqlString = "insert into SP_JJH_DICT_DWBM (XQBM,DW,DWBMBM,BM,JGDM,DWZDBM,ZD) ";
sqlString += " values('"+user+"','"+name+"','"+number+"','"+password+"','"+strJGDM+"','"+strDWZDBM+"','"+auditNum+"')";
//dap=new SqlDataAdapter(sqlString,m_connString);
//dap.InsertCommand();
conn = new SqlConnection(m_connString);
comm = new SqlCommand(sqlString,conn);
if(conn.State.ToString()=="Closed" )conn.Open();
comm.ExecuteNonQuery();
comm.Dispose();
conn.Close();
conn.Dispose();
Grid_Bind(); //myDataGrid数据源
}
else
{
this.RegisterStartupScript("js","<script>alert('机关代码已存在!');</script>");
}
}
// private void myDataGrid_SelectedIndexChanged(object sender, System.EventArgs e)
// {
// user_Text.Value = myDataGrid.SelectedItem.Cells[3].Text.Trim();//.Replace(" ","");
// name_Text.Value = myDataGrid.SelectedItem.Cells[4].Text.Trim();//.Replace(" ","");
// number_Text.Value = myDataGrid.SelectedItem.Cells[5].Text.Trim();//.Replace(" ","");
// //loginNum_Text.Value = myDataGrid.SelectedItem.Cells[4].Text.Trim();
// Password1.Value = myDataGrid.SelectedItem.Cells[6].Text.Trim();
// //departNum_Text.SelectedValue = myDataGrid.SelectedItem.Cells[8].Text.Trim();
// audit_Text.Value = myDataGrid.SelectedItem.Cells[9].Text.Trim();
// auditNum_Text.Value = myDataGrid.SelectedItem.Cells[10].Text.Trim();
// }
private void myDataGrid_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
myDataGrid.CurrentPageIndex=e.NewPageIndex;
Grid_Bind();
}
//删除
private void myDataGrid_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string id=e.Item.Cells[2].Text;
string sql="delete from SP_JJH_DICT_DWBM where ID='"+id+"'";
SqlConnection co=new SqlConnection(m_connString);
SqlCommand com=new SqlCommand(sql,co);
co.Open();
com.ExecuteNonQuery();
com.Dispose();
co.Close();
co.Dispose();
this.Grid_Bind();
}
// 修改
private void myDataGrid_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
myDataGrid.EditItemIndex = e.Item.ItemIndex;
Grid_Bind();
}
//取消修改
private void myDataGrid_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
myDataGrid.EditItemIndex = -1;
Grid_Bind();
}
//更新
private void myDataGrid_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string id=myDataGrid.DataKeys[e.Item.ItemIndex].ToString();
TextBox tbXQBM=(TextBox)e.Item.Cells[3].Controls[0];
string strXQBM= tbXQBM.Text.Trim();
TextBox tbDW=(TextBox)e.Item.Cells[4].Controls[0];
string strDW = tbDW.Text.Trim();
TextBox tbDWBMBM=(TextBox)e.Item.Cells[5].Controls[0];
string strDWBMBM = tbDWBMBM.Text.Trim();
TextBox tbBM=(TextBox)e.Item.Cells[6].Controls[0];
string strBM = tbBM.Text.Trim();
TextBox tbJGDM=(TextBox)e.Item.Cells[7].Controls[0];
string strJGDM = tbJGDM.Text.Trim();
TextBox tbDWZDBM=(TextBox)e.Item.Cells[8].Controls[0];
string strDWZDBM = tbDWZDBM.Text.Trim();
TextBox tbZD=(TextBox)e.Item.Cells[9].Controls[0];
string strZD = tbZD.Text.Trim();
string MYsqlString = "update SP_JJH_DICT_DWBM set XQBM='"+strXQBM+"',DW='"+strDW+"',DWBMBM='"+strDWBMBM+"',BM='"+strBM+"',JGDM='"+strJGDM+"',DWZDBM='"+strDWZDBM+"',ZD='"+strZD+"' where ID='"+id+"'";
conn = new SqlConnection(m_connString);
comm = new SqlCommand(MYsqlString,conn);
if(conn.State.ToString()=="Closed" )conn.Open();
comm.ExecuteNonQuery();
comm.Dispose();
conn.Close();
conn.Dispose();
myDataGrid.EditItemIndex = -1;
Grid_Bind();
}
//设置编辑模式的 TextBox 的宽度
private void myDataGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.EditItem)
{
((TextBox)e.Item.Cells[3].Controls[0]).Width=90;
((TextBox)e.Item.Cells[4].Controls[0]).Width=170;
((TextBox)e.Item.Cells[5].Controls[0]).Width=90;
((TextBox)e.Item.Cells[6].Controls[0]).Width=90;
((TextBox)e.Item.Cells[7].Controls[0]).Width=90;
((TextBox)e.Item.Cells[8].Controls[0]).Width=90;
((TextBox)e.Item.Cells[9].Controls[0]).Width=90;
}
}
}
}
参考资料: http://www.cnblogs.com/xugang/archive/2007/12/20/1007318.html
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
以前做个一个 不过现在都忘了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询