想在dataGridView 中进行编辑/输入。 然后把编辑的内容通过button按钮 存入数据库
请各位大哥帮下我
提供全代码的更好谢了
请各位大哥帮下我
提供全代码的更好谢了
解决方案 »
- c# 能否让一个form 或两个form 一直在顶层窗口不能被切换 一直到被关闭为止。
- 各位大神,高分求在PC上代码实现“创建“wifi热点的方法
- 如何使用xpath查找某个节点?
- 窗体背景如何通过一个路径指向一个图片文件。Form2.BackgroundImage = "d:\xpp.bmp"这个语句错在哪里?(因不准备使用资源文件来处理)
- ??连接数据库的问题???
- 如何实现类似输入法的软键盘?
- 现在用WPF的多吗,一般用来做什么
- API函数问题
- 如何把删除RowFilter过滤掉的记录
- serialPort打开串口时提示系统资源不足
- winform用户登陆问题
- c#WindowsApplication打包发布后差MDAC的问题
设置ReadOnly属性为false即可修改,至于更新数据库,需要通过datagridview所绑定的数据源,取用DataAdapter来更新数据库。
DataGridView常见用法和FAQ汇总
http://www.cnblogs.com/anderslly/archive/2006/12/07/dgvsummary.html
dataadapter.update(datatable);datatable.acceptchanges();就可以写入数据库了
/// 在单击 GridView 控件内某一行的 Delete 按钮(其 CommandName 属性设置为"Delete"的按钮)时发生,但在 GridView 控件从数据源删除记录之前。此事件通常用于取消删除操作。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sqlStr = "delete from Employee where ID=" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value) + "";
Common.ExecuteSql(sqlStr);
bind();
}
/// <summary>
/// 在单击 GridView 控件内某一行的 Update 按钮(其 CommandName 属性设置为"Update"的按钮)时发生,但在 GridView 控件更新记录之前。此事件通常用于取消更新操作。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string ID = GridView1.DataKeys[e.RowIndex].Value.ToString();
string Emp_ID = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim();
string Emp_RealName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
string Emp_Sex = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
string Emp_Address = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
string sqlStr = "update Employee set EmpID='" + Emp_ID + "',EmpRealName='" + Emp_RealName + "',EmpSex='" + Emp_Sex + "',EmpAddress='" + Emp_Address + "' where ID=" + ID + "";
Common.ExecuteSql(sqlStr);
GridView1.EditIndex = -1;
bind();
}
/// <summary>
/// 在单击 GridView 控件内某一行的 Edit 按钮(其 CommandName 属性设置为“Edit”的按钮)时发生,但在 GridView 控件进入编辑模式之前。此事件通常用于取消编辑操作。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
/// <summary>
/// 在单击 GridView 控件内某一行的 Cancel 按钮(其 CommandName 属性设置为“Cancel”的按钮)时发生,但在 GridView 控件退出编辑模式之前。此事件通常用于停止取消操作。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
/// <summary>
/// 在 GridView 控件中的某个行被绑定到一个数据记录时发生。此事件通常用于在某个行被绑定到数据时修改该行的内容。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
foreach (TableCell tc in e.Row.Cells)
{
tc.Attributes["style"] = "border-color:Black";
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
string OrderID = e.Row.Cells[1].Text;
string _jsEdit = "showModalDialog('GridviewEditUpdateDelete.aspx?id=" + OrderID + "',null,'dialogWidth=650px;dialogHeight=500px;help:no;status:no')";
e.Row.Cells[7].Attributes.Add("onclick", _jsEdit);
}
}
/// <summary>
/// 数据绑定
/// </summary>
public void bind()
{
string sqlStr = "select * from Employee";
DataSet myds = Common.dataSet(sqlStr);
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "ID" };
GridView1.DataBind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
int id =Convert.ToInt32(GridView1.DataKeys[GridView1.SelectedIndex].Value.ToString());
//Response.Write(id);
//你的插入数据库新表的代码
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
switch (e.CommandName)
{
case "EditOrder":
bind();
break;
}
}
在datagridview 中多建一个visible = false的 列 都设为false
当更改的时候把它置为true循环取
这样就找 行为true 就更新那条记录