C# winform 在datagridview中修改数据后,点击按钮把数据库中的数据更新,如何实现?

C# winform 在datagridview中修改数据后,点击按钮把数据库中的数据更新,如何实现?,第1张

/*以下是我在自己三层项目改过来的,先在设计模式下拉一dataGridView控件,名称叫dataGridView1,添加一属性text值为“修改” 属性name值为“ btnUpdate”的button控件,还有一个属性name值为btnCancel的取消控件。

以下是cs代码,把整个复制过去,改下空间名,和类名(这里指窗体名字),就行了

*/

using System

using System.Collections.Generic

using System.ComponentModel

using System.Data

using System.Drawing

using System.Text

using System.Windows.Forms

using System.Data.SqlClient

namespace LDZ.HMISAUX

{

public partial class frmUsersEdit : Form

{

public frmUsersEdit()

{

InitializeComponent()

}

/// <summary>

/// 构造一个方法,并绑定数据到dataGridView控件中

/// </summary>

///

private void GetUsers()

{

try

{

SqlConnection conn=new SqlConnection("server=localhostdatabase=testuid=abcpwd=abc")

conn.Open()

string sql=string.Format("Select all * from testTable")

SqldataAdapter SDA=new SqldataAdapter(sql,conn)

DataSet DS = ShowUsers.GetUsers()

SDA.Fill(DS,"testTable")

this.dataGridView1.DataSource = DS.Tables[0].DefaultView

}

catch (Exception ex)

{

MessageBox.Show(ex.Message)

}

}

private int Aid

private string ALoginId

private string AUserName

private void btnUpdate_Click(object sender, EventArgs e)

{

try

{

SqlConnection conn=new SqlConnection("server=localhostdatabase=testuid=abcpwd=abc")

conn.Open()

string sql=string.Format("update testTable set LoginId=' "+ALoginId+" ' ,UserName=' "+AUserName+" ' where id=' "+Aid+" ' ")

SqldataAdapter SDA=new SqldataAdapter(sql,conn)

DataSet DS = ShowUsers.GetUsers()

SDA.Fill(DS,"testTable")

this.dataGridView1.DataSource = DS.Tables[0].DefaultView

MessageBox.Show("修改记录成功")

}

catch (Exception ex)

{

MessageBox.Show(ex.Message)

}

}

private void btnCancel_Click(object sender, EventArgs e)

{

this.Close()

}

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)

{

try

{

if (dataGridView1.Rows.Count >0)

{

Aid = int.Parse(this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString())

ALoginId = this.dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString()

AUserName = this.dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString()

AUserpwd = this.dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString()

AEmail = this.dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString()

AUserGroup = int.Parse(this.dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString())

AUserType = int.Parse(this.dataGridView1.Rows[e.RowIndex].Cells[6].Value.ToString())

}

}

catch (Exception ex)

{

MessageBox.Show(ex.Message)

}

}

private void UpdateUsers_Load(object sender, EventArgs e)

{

GetUsers()

}

}

}

}

通过浏览器的reload方法即可在提交之后重新刷新页面

<script language=javascript>

function winclose() {

//此处填写要处理的逻辑代码

window.opener.location.reload()//刷新

}

</script>

<input type=“button” name=“close” value="提交" onclick=“winclose()”/>

1.选中gridview,然后右击,添加项 添加命令项中的添加,并且把外观中的buttontype设置成button.

2.在前台代码中添加OnRowEditing="GridView1_RowEditing"

<asp:GridView ID="GridView1" runat="server" Height="291px" Width="482px"

OnRowDeleting="GridView1_RowDeleting "

( 如 )OnRowEditing="GridView1_RowEditing"

OnRowCancelingEdit="GridView1_RowCancelingEdit"

3.在后台代码中

添加

//绑定

public void bind()

{

GridView1.DataKeyNames = new string[] { "编号", "说明", "子编号", "子编号说明" }//主键

mysql.ExecAdapyerBing3(GridView1, "hbgbbm3")//绑定gridview

}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

{

GridView1.EditIndex = e.NewEditIndex

//当前编辑行背景色高亮

this.GridView1.EditRowStyle.BackColor = System.Drawing.Color.FromName("#F7CE90")

bind()

}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

{

//根据自己的程序 需要修改

string txt1 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim()

string txt2 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim()

string UpdateStr = "update 后备干部编码 set 说明='" + txt1 + "',子编号说明=' " + txt2 + "' where 编号=" + this.GridView1.DataKeys[e.RowIndex].Value + " and 子编号=" + this.GridView1.DataKeys[e.RowIndex].Values[2].ToString()

try

{

mysql.ExecSqlCon(UpdateStr)//自己写一个方法

Response.Write("<script language='javascript'>alert('修改成功!')</script>")

}

catch (Exception exp)

{

Response.Write("<script language='javascript'>alert('" + exp.Message + "')</script>")

}

finally

{

this.GridView1.EditIndex = -1

bind()//自定义绑定

}

}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

{

GridView1.EditIndex = -1

bind()

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10090537.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存