该题显示的列是五个列,但是需要通过id来进行删除所以我们需要添加id列但是需要设置为不可见,取消打勾,这样子能够按照id进行删除操作,但是不显示
新增活动窗口关闭时立即刷新表中的内容
private void button1_Click(object sender, EventArgs e) { Form2 f = new Form2(); f.ShowDialog(); //这三行代码意思为查找数据库中所有数据并显示 string sql = "select * from Activities "; DataSet ds = DBHelper.ds(sql); this.dataGridView1.DataSource = ds.Tables[0]; }
需要用datetimepicker.value.toshortdatestring()方法把时间转为字符串然后才能进行过数据库添加操作
private void button1_Click(object sender, EventArgs e)
{
string a = dateTimePicker1.Value.ToShortDateString();
string b = dateTimePicker2.Value.ToShortDateString();
if (textBox1.Text==""||textBox4.Text==""||textBox5.Text==""||a.Length<1||b.Length<1)
{
MessageBox.Show("所有信息均不为空,请重新填写!","失败提示",MessageBoxButtons.OK,MessageBoxIcon.Information);//分别对应提示信息,左上角提示,选项,图标
return;
}
string sql = string.Format("insert Activities values('{0}','{1}','{2}','{3}','{4}')", textBox1.Text, textBox4.Text, textBox5.Text, a, b);
if (DBHelper.noquery(sql))
{
//分别对应提示信息,左上角提示,选项,图标
MessageBox.Show("添加成功","成功提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
需要先要拿到隐藏列id的单元格值,再用该隐藏格的值进行删除
string str = this.dataGridView1.CurrentRow.Cells["id"].Value.ToString();//获取列名为id的单元格名字,调用方法进行删除
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
string str = this.dataGridView1.CurrentRow.Cells["id"].Value.ToString();//获取列名为id的单元格名字,调用方法进行删除
string sql = string.Format("delete Activities where id='{0}'", str);
DBHelper.noquery(sql);
}
catch (Exception eee )
{
MessageBox.Show(eee.ToString());
}
string sqlstr = "select * from Activities ";
DataSet ds = DBHelper.ds(sqlstr);
this.dataGridView1.DataSource = ds.Tables[0];
}
首先设置datagridview的这三个属性
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace a
{
class DBHelper
{
public static string connstr = "server=.;database=Club;uid=sa;pwd=123456";
public static SqlConnection conn = null;
public static void init() {
if (conn==null)
{
conn = new SqlConnection(connstr);
}
conn.Close();
conn.Open();
}
public static bool noquery(string sql) {
init();
SqlCommand cod = new SqlCommand(sql,conn);
int ret= cod.ExecuteNonQuery();
conn.Close();
if (ret>0)
{
return true;
}
else
{
return false;
}
}
public static DataSet ds(string sql) {
init();
DataSet ds=new DataSet();
SqlDataAdapter t = new SqlDataAdapter(sql,conn);
t.Fill(ds);
conn.Close();
return ds;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace a
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.dataGridView1.AutoGenerateColumns=false;//自动取消列
string sql = "select * from Activities ";
DataSet ds= DBHelper.ds(sql);
this.dataGridView1.DataSource = ds.Tables[0];
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.ShowDialog();
//这三行代码意思为查找数据库中所有数据并显示
string sql = "select * from Activities ";
DataSet ds = DBHelper.ds(sql);
this.dataGridView1.DataSource = ds.Tables[0];
}
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
string str = this.dataGridView1.CurrentRow.Cells["id"].Value.ToString();//获取列名为id的单元格名字,调用方法进行删除
string sql = string.Format("delete Activities where id='{0}'", str);
DBHelper.noquery(sql);
}
catch (Exception eee )
{
MessageBox.Show(eee.ToString());
}
string sqlstr = "select * from Activities ";
DataSet ds = DBHelper.ds(sqlstr);
this.dataGridView1.DataSource = ds.Tables[0];
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace a
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
string a = dateTimePicker1.Value.ToShortDateString();
string b = dateTimePicker2.Value.ToShortDateString();
if (textBox1.Text==""||textBox4.Text==""||textBox5.Text==""||a.Length<1||b.Length<1)
{
MessageBox.Show("所有信息均不为空,请重新填写!","失败提示",MessageBoxButtons.OK,MessageBoxIcon.Information);//分别对应提示信息,左上角提示,选项,图标
return;
}
string sql = string.Format("insert Activities values('{0}','{1}','{2}','{3}','{4}')", textBox1.Text, textBox4.Text, textBox5.Text, a, b);
if (DBHelper.noquery(sql))
{
//分别对应提示信息,左上角提示,选项,图标
MessageBox.Show("添加成功","成功提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
private void Form2_Load(object sender, EventArgs e)
{
}
}
}