dataGridView 是显示数据库表的控件,
//获取第i列字段内容:
data[i] = this.dataGridView1.CurrentRow.Cells[i].Value.ToString();
//传入相应sql语句的参数,即可实现数据库的操作:
private static string DB_CON_STR = "server=192.168.1.105;uid=root;pwd=123;database=expt;Character Set=utf8";
MySqlConnection con = new MySqlConnection(DB_CON_STR);
public void add(string sql)
{
MySqlCommand command=null;
try
{
command = new MySqlCommand(sql, con);
con.Open();
int i = command.ExecuteNonQuery();
}
catch (MySqlException se)
{
Console.WriteLine("Database operation errors : " + se.StackTrace);
con.Close();
command = null;
}
}
//在dataGridView控件内添加可添加事件的控件:点击dataGridView属性中的Columns
//添加DataGridView控件的本体事件:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if(dataGridView1.Columns[e.ColumnIndex].Name =="change")
{
string[] data = new string[12];
for (int i = 0; i < 12; i++)
{
data[i] = this.dataGridView1.CurrentRow.Cells[i].Value.ToString();
}
#region 添加
//string sql = "insert into t_doman values('" + data[2] + "','"
// + data[3] + "','"
// + data[4] + "','"
// + data[5] + "','"
// + data[6] + "','"
// + data[7] + "','"
// + data[8] + "','"
// + data[9] + "','"
// + data[10] + "','"
// + data[11] + "');";
#endregion
string sql = "update t_doman set 项目名称 ='" + data[3] + "',"
+ "高高 ='" + data[4] + "',"
+ "高中上 ='" + data[5] + "',"
+ "高中下 ='" + data[6] + "',"
+ "正常上 ='" + data[7] + "',"
+ "正常下 ='" + data[8] + "',"
+ "中低上 ='" + data[9] + "',"
+ "中低下 ='" + data[10] + "',"
+ "低低 ='" + data[11]
+ "'where 单元名称 ='" +
this.dataGridView1.CurrentRow.Cells[2].Value.ToString() + "';";
DataBase mysql = new DataBase();
mysql.add(sql);
MessageBox.Show("修改成功!");
this.YUData_Load(sender, e);
}
if (dataGridView1.Columns[e.ColumnIndex].Name == "remove")
{
YN yn = new YN();
yn.Show();
this.YUData_Load(sender, e);
}
}
}
//效果图: