以下是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()
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)