0
我有4列一个DataGridView:
UserID, FirstName, LastName, Email
UPDATE语句改变错误记录当我双击某一行,另一种形式与4个文本框里面那些行的值,每个文本框有弹起单元格值
当我单击提交按钮时,该行将更新为文本框的值。
我与
....where UserID = @UserID
一起工作。但是,如果列单元格 我正在更改ISUserId
,错误的行或无行将被修改。
问题
我如何可以编辑任何列值的行,它改变了右排没有增量列?
代码
public partial class EditUser : Form
{
public DataGridViewRow dgvRow;
public EditUser()
{
InitializeComponent();
}
private void EditUser_Load(object sender, EventArgs e)
{
lbl5.Text = dgvRow.Cells[0].Value.ToString();
txtUserID.Text = dgvRow.Cells[0].Value.ToString();
txtFName.Text = dgvRow.Cells[1].Value.ToString();
txtLName.Text = dgvRow.Cells[2].Value.ToString();
txtEmail.Text = dgvRow.Cells[3].Value.ToString();
label1.Visible = false;
txtUserID.Visible = false;
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
this.Visible = false;
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection sc = new SqlConnection();
SqlCommand com = new SqlCommand();
sc.ConnectionString = ("");
sc.Open();
com.Connection = sc;
com.CommandText = ("update JoshUserTable SET UserID = @UserID, [email protected],[email protected],[email protected] where UserID = @UserID");
com.Parameters.AddWithValue("@UserID", txtUserID.Text);
com.Parameters.AddWithValue("@FirstName", txtFName.Text);
com.Parameters.AddWithValue("@LastName", txtLName.Text);
com.Parameters.AddWithValue("@Email", txtEmail.Text);
com.ExecuteNonQuery();
sc.Close();
}
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
label1.Visible = true;
txtUserID.Visible = true;
SqlConnection sc = new SqlConnection();
SqlCommand con = new SqlCommand();
sc.ConnectionString = ("");
sc.Open();
con.Connection = sc;
con.CommandText = ("update JoshUserTable SET UserID = @UserID, [email protected],[email protected],[email protected] where FirstName = @FirstName");
con.Parameters.AddWithValue("@UserID", txtUserID.Text);
con.Parameters.AddWithValue("@FirstName", txtFName.Text);
con.Parameters.AddWithValue("@LastName", txtLName.Text);
con.Parameters.AddWithValue("@Email", txtEmail.Text);
con.ExecuteNonQuery();
sc.Close();
}
}
感谢您的输入,我去一个稍微不同的路线。随着userIDtext框,我做了一个显示用户ID的标签。当editform弹出时,UserIDtextbox是不可见的。在UserID标签旁边,我有一个链接标签“更改”,如果点击该标签,则会显示UserID文本框,以更改名称。如果链接未被选中,则无意更改名称。在我的linkcheck事件中,我更改了where子句以匹配firstname和lastname,因为即使用户名或电子邮件可能,他们的名字也不会改变。然而,排并没有改变......见上面 – 2015-03-03 15:07:35
哇,我花了一秒才弄清楚什么是没有意义的。我正在让我的linklabel做比它应有的更多的方式。 – 2015-03-03 16:19:33
确定了你 - 无论如何,我真的认为你应该**检查原始值并设置(a)新值(s)**。当调用'executeNonQuery()'时,你将得到更新记录的数量。如果这个记录*不相等1 *出错了! – 2015-03-03 19:44:39