在DataGridView中获取了数据库的数据可以在上面直接修改怎么改。
例如:DataGridView中填充好数据后有一行数据如下:
ID name
01 TOM
我要在TOM上点鼠标,然后修改为JOHN,然后保存到数据库。现在是能修改是能改的,但是怎么把修改保存到数据库,关键不知道怎么取得修改的值。
例如:DataGridView中填充好数据后有一行数据如下:
ID name
01 TOM
我要在TOM上点鼠标,然后修改为JOHN,然后保存到数据库。现在是能修改是能改的,但是怎么把修改保存到数据库,关键不知道怎么取得修改的值。
{
//将该行的值存入数据库
}
//加载的数据
private void HmdFrom_Load(object sender, EventArgs e)
{
button1.Enabled = false;
String sql = "select * from hmd where 1=1";
show(sql);
}
//写个方法绑定数据到dataGridView1
private void show(String sql)
{
try
{
conn = new DbConn();
ds.Clear();
Adapter = conn.DataGridViewBind(sql);
Adapter.Fill(ds);
DataTable dataTable = ds.Tables[0]; dataGridView1.DataSource = dataTable.DefaultView; //设置dataGridView2不同的样式
this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
this.dataGridView1.DefaultCellStyle.SelectionBackColor = Color.SkyBlue;
this.dataGridView1.AllowUserToAddRows = true;
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
}
//保存黑名单里的数据
private void button2_Click(object sender, EventArgs e)
{
button1.Enabled = true;
try
{
if (this.dataGridView1.SelectedRows[0].Cells[1].Value.ToString() != "" && this.dataGridView1.SelectedRows[0].Cells[1].Value != null)
{
try
{
SqlCommandBuilder commBuilder = new SqlCommandBuilder();
commBuilder.DataAdapter = Adapter;
Adapter.Update(ds.Tables[0]);
MessageBox.Show("黑名单信息保存成功!");
button4_Click(sender, e);
button1_Click(sender, e); }
catch (SqlException)
{
MessageBox.Show("id重复!");
}
}
else
{
MessageBox.Show("姓名必须填写");
}
}
catch (SqlException)
{
MessageBox.Show("添加错误!");
}
}
DataGridView dgv;
dgv.CellEndEdit += new DataGridViewCellEventHandler(dgv_CellEndEdit);
} void dgv_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
DataGridViewCell dgvc = (DataGridViewCell)sender;
MessageBox.Show(dgvc.Value.ToString());
//e.RowIndex;
//e.ColumnIndex;
}
Adapter = conn.DataGridViewBind(sql);//你的sql
Adapter.Fill(ds);
DataTable dataTable = ds.Tables[0]; dataGridView1.DataSource = dataTable.DefaultView;最后在Button方法写下,就会实现你的结果啊~~
SqlCommandBuilder commBuilder = new SqlCommandBuilder();
commBuilder.DataAdapter = Adapter;
Adapter.Update(ds.Tables[0]);
SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = new SqlCommand("select * from tb_stu order by stuID desc", conn); SqlCommandBuilder builder = new SqlCommandBuilder(adapter); conn.Open(); DataSet dataSet = new DataSet();
adapter.Fill(dataSet, "allUser"); this.dataGridView1.DataSource = dataSet.Tables["allUser"];先谢谢上面几位朋友的回答,可能我太笨了,有些不能理解你们的回答。我只做到这步,在Datagridview中EditMode属性改了,还有Readonly属性也改了。但是调试下,运行后在上面可以改,但是保存效果,怎么做都没用,数据库中依旧没保存到。
我的本意就是我们修改数据可以想SQL企业管理器里面一样,可以直接在上面点点就修改了。不晓得哪位前辈有过这样的经验呀。
怎么可能嘛,你把你的源代码都贴出来,我看哈~~绝对能改的
这个代码放在加载里面
private void JzmxbForm_Load(object sender, EventArgs e)
{
SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = new SqlCommand("select * from tb_stu order by stuID desc", conn); SqlCommandBuilder builder = new SqlCommandBuilder(adapter);这行去掉 conn.Open(); DataSet dataSet = new DataSet();这个放在全局上面定义
adapter.Fill(dataSet, "allUser"); this.dataGridView1.DataSource = dataSet.Tables["allUser"];
}
添加一个Button1按钮
private void button1_Click(object sender, EventArgs e)
{
SqlCommandBuilder commBuilder = new SqlCommandBuilder();
commBuilder.DataAdapter = Adapter;
Adapter.Update(dataSet.Tables[0]); }
试试看
public partial class HmdFrom : Form,
{
DbConn conn = null;
int strid; //记录黑名单信息行id
SqlDataAdapter Adapter; //初始化Adapter对象
DataSet ds = new DataSet(); //绑定协议单位信息DataSet
String mm = "";
public HmdFrom()
{
InitializeComponent();
} private void HmdFrom_Load(object sender, EventArgs e)
{
button1.Enabled = false;
String sql = "select * from hmd where 1=1";
show(sql);
} private void show(String sql)
{
try
{
conn = new DbConn();
ds.Clear();
Adapter = conn.DataGridViewBind(sql);
Adapter.Fill(ds);
DataTable dataTable = ds.Tables[0]; dataGridView1.DataSource = dataTable.DefaultView; //设置dataGridView2不同的样式
this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
this.dataGridView1.DefaultCellStyle.SelectionBackColor = Color.SkyBlue;
this.dataGridView1.AllowUserToAddRows = true;
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
strid = e.RowIndex;
}
//刷新datagridview1里的数据
private void button1_Click(object sender, EventArgs e)
{
if (dataGridView1.Rows.Count != 0)
{
show(mm);
}
}
//保存黑名单里的数据
private void button2_Click(object sender, EventArgs e)
{
button1.Enabled = true;
try
{
if (this.dataGridView1.SelectedRows[0].Cells[1].Value.ToString() != "" && this.dataGridView1.SelectedRows[0].Cells[1].Value != null)
{
try
{
SqlCommandBuilder commBuilder = new SqlCommandBuilder();
commBuilder.DataAdapter = Adapter;
Adapter.Update(ds.Tables[0]);
MessageBox.Show("黑名单信息保存成功!");
button4_Click(sender, e);
button1_Click(sender, e); }
catch (SqlException)
{
MessageBox.Show("id重复!");
}
}
else
{
MessageBox.Show("姓名必须填写");
}
}
catch (SqlException)
{
MessageBox.Show("添加错误!");
}
} private void button3_Click(object sender, EventArgs e)
{
if (dataGridView1.Rows[0].Cells[0].Value != null && dataGridView1.CurrentRow.Cells[0].Value.ToString() != "" && dataGridView1.CurrentRow.Cells[0].Value != null)
{
if (MessageBox.Show("确定删除吗?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
try
{
String Sql = "delete from hmd where id=" + dataGridView1.CurrentRow.Cells[0].Value;
dataGridView1.Rows.RemoveAt(strid);
int result = conn.ExecuteNonQuery(Sql);
if (result >= 1)
{
MessageBox.Show("删除成功!");
button4_Click(sender, e);
button1_Click(sender, e);
}
else
{
MessageBox.Show("删除失败!");
}
}
catch (Exception)
{
MessageBox.Show("请选中有效的行!");
}
}
}
} private void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
{
e.Row.Cells["zjlx"].Value = "身份证";
} private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
MessageBox.Show("请输入正确的数据格式!");
}
//查询,默认为空的查询条件是数据全部显示
private void button4_Click(object sender, EventArgs e)
{
getsql();
}
//查询内容的构造函数
public void getsql()
{
String strSql = "select * from hmd where 1=1"; String dm = textBox1.Text;
if (textBox1.Text == "" && textBox1.Text == null)
{
strSql += " and id=" + 1;
}
if (textBox1.Text != "" && textBox1.Text != null)
{
strSql += "and zjhm='" + dm + "'";
}
strSql += " order by id";
show(strSql);
mm = strSql;
}
}
下面这个是配置文件dbconn里的一个方法
public SqlDataAdapter DataGridViewBind(String sql)
{
try
{
//当连接处于打开状态时关闭,然后再打开,避免有时候数据不能及时更新
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
conn.Open();
//创建SqlDataAdapter对象
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
conn.Close();
return sda;
}
catch (Exception)
{
throw;
}
finally {
conn.Close();
}
}
哎,LZ多看点书
{
InitializeComponent();
CON = new SqlConnection(connectionString);
CON.Open();
DS = new DataSet();
SqlCommand COM = new SqlCommand("select * from AA", CON);
SDA = new SqlDataAdapter(COM);
BU = new SqlCommandBuilder(SDA);
SDA.Fill(DS, "AA");
dataGridView1.DataSource = DS.Tables[0];
} DataSet DS;
SqlDataAdapter SDA;
SqlCommandBuilder BU;
SqlConnection CON; string connectionString = "Password=888;Persist Security Info=True;User ID=sa;Initial Catalog=test;Data Source=10.100.101.42"; private void save_Click(object sender, EventArgs e)
{ int row = SDA.Update(DS, "AA");
MessageBox.Show(Convert.ToString(row));
}
这方法可以实现你的功能
public Form1()
{
InitializeComponent();
CON = new SqlConnection(connectionString);
CON.Open();
DS = new DataSet();
SqlCommand COM = new SqlCommand("select * from AA", CON);
SDA = new SqlDataAdapter(COM);
BU = new SqlCommandBuilder(SDA);
SDA.Fill(DS, "AA");
dataGridView1.DataSource = DS.Tables[0];
} DataSet DS;
SqlDataAdapter SDA;
SqlCommandBuilder BU;
SqlConnection CON; string connectionString = "Password=888;Persist Security Info=True;User ID=sa;Initial Catalog=test;Data Source=10.100.101.42";//这里是一个保存数据的按钮
private void save_Click(object sender, EventArgs e)
{ int row = SDA.Update(DS, "AA");
MessageBox.Show(Convert.ToString(row));
}
这方法我已经试过了,可以保存修改的数据;