求助 关于datagridview删除行问题
各位大哥大姐,我是个C#菜鸟,请给为大哥大姐给小弟指点指点,小弟跪谢~~~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 用户
{
public partial class 用户管理 : Form
{
SqlDataAdapter thisAdapter;
DataSet thisDataSet = new DataSet();
DataSet mydataset = new DataSet();
public 用户管理(info parent)
{
InitializeComponent();
this.MdiParent = parent;
userload();
}
public void userload()
{
SqlConnection thisConnection = new SqlConnection(
"Server=(local);Integrated Security=True;" +
"Database=stu");
thisAdapter = new SqlDataAdapter("select f_username[用户名],f_password[密码],f_limit[权限],f_memo[备注] from t_user", thisConnection);
SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);
thisAdapter.Fill(thisDataSet, "t_user");
dataGridView1.DataSource = thisDataSet.Tables[0];
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult result;
result = MessageBox.Show("确定要添加?", "系统信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (result == DialogResult.OK)
{
try
{
SqlConnection thisConnection = new SqlConnection(
"Server=(local);Integrated Security=True;" +
"Database=stu");
thisConnection.Open();
SqlCommand thisCommand = thisConnection.CreateCommand();
thisCommand.CommandText = "insert into t_user(f_username,f_password,f_limit,f_memo) values('" +
textBox1.Text.ToString().Trim() + "','" + textBox2.Text.ToString().Trim() +
"','" + textBox3.Text.ToString().Trim() + "','" + richTextBox1.Text.ToString().Trim() + "')";
thisCommand.ExecuteNonQuery();
thisAdapter = new SqlDataAdapter("select f_username[用户名],f_password[密码],f_limit[权限],f_memo[备注] from t_user", thisConnection);
thisDataSet.Clear();
SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);
thisAdapter.Fill(thisDataSet, "t_user");
dataGridView1.DataSource = thisDataSet.Tables[0];
}
catch
{ MessageBox.Show("请检查输入格式是否正确?"); }
}
}
private void button3_Click(object sender, EventArgs e)
{
try
{
if (this.dataGridView1.SelectedRows.Count > 0)
{
DataRowView drv = dataGridView1.SelectedRows[0].DataBoundItem as DataRowView;
drv.Delete();
MessageBox.Show("删除数据集记录操作成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
//这样已经把dataGridView1里面选中的行删除了,却无法删除SQL里面的数据
}
}
catch (Exception ex)
{
ex.Message.ToString();
}
}
private void button5_Click(object sender, EventArgs e)
{
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
DialogResult result;
result = MessageBox.Show("确定要修改?", "系统信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (result == DialogResult.OK)
{
try
{
thisAdapter.Update(thisDataSet, "t_user");
MessageBox.Show("修改成功!");
}
catch
{
MessageBox.Show("修改失败!");
}
}
}
}
} --------------------编程问答-------------------- (1)按ID删除SQL中的记录
(2)删除DataGridView中的行 --------------------编程问答-------------------- 动不动就跪谢
无语 --------------------编程问答-------------------- if (this.dataGridView1.SelectedRows.Count > 0)
{
DataRowView drv = dataGridView1.SelectedRows[0].DataBoundItem as DataRowView;
drv.Delete();
MessageBox.Show("删除数据集记录操作成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
//这样已经把dataGridView1里面选中的行删除了,却无法删除SQL里面的数据
}
drv.Delete();
你这里不需要带一个主键ID进去删除的吗??? --------------------编程问答-------------------- 你只是删除了数据集中的数据,但是没有把删除数据后的数据集更新到数据库中,因此数据库中的数据仍然存在。 --------------------编程问答--------------------
是的,要update回去 --------------------编程问答-------------------- if (this.dataGridView1.SelectedRows.Count > 0)
{
DataRowView drv = dataGridView1.SelectedRows[0].DataBoundItem as DataRowView;
drv.Delete();
MessageBox.Show("删除数据集记录操作成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
//这样已经把dataGridView1里面选中的行删除了,却无法删除SQL里面的数据
}
drv.Delete();
你这里不需要带一个主键ID进去删除的吗??? --------------------编程问答-------------------- try
{
if (this.dataGridView1.SelectedRows.Count > 0)
{
DataRowView drv = dataGridView1.SelectedRows[0].DataBoundItem as DataRowView;
drv.Delete();
dataGridView1.DataSource = thisDataSet.Tables[0];
SqlConnection thisConnection = new SqlConnection(
"Server=(local);Integrated Security=True;" +
"Database=stu");
thisConnection.Open();
SqlCommand cmd = new SqlCommand("delete from t_user where f_username", thisConnection);
cmd.ExecuteNonQuery();
thisConnection.Close();
userload();
MessageBox.Show("删除数据集记录操作成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
ex.Message.ToString();
}
大哥不好意思,没有复制完整,,删除按钮的代码是这样的~~~ --------------------编程问答-------------------- 不错,正好用到,谢谢! --------------------编程问答-------------------- if (this.dataGridView1.SelectedRows.Count > 0)
{
DataRowView drv = dataGridView1.SelectedRows[0].DataBoundItem as DataRowView;
drv.Delete();
MessageBox.Show("删除数据集记录操作成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
//这样已经把dataGridView1里面选中的行删除了,却无法删除SQL里面的数据
}
drv.Delete();
你这里不需要带一个主键ID进去删除的吗??? --------------------编程问答-------------------- C#.net,Javascript,AJAX,SilverLight,HTML5,CSS3,WPF,WCF,Win Phone技术交流群118907238
补充:.NET技术 , C#