题目:
效果图:
数据库:
做法:
点击查询按钮查询state为0的数据
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text=="")
{
MessageBox.Show("请输入数据!");
return;
}
string sql = string.Format("select * from tb_violation where carNo='{0}'and state=0", textBox1.Text);
DataTable s = DBHelper.ds(sql).Tables[0];
if (s.Rows.Count>0)
{
this.dataGridView1.DataSource = s;
}
else
{
MessageBox.Show("该车无违章!");
}
}
双击一行后显示是否处理,选择是则改变数据库state的值,否就取消,然后自动刷新查询state为0的数据
private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (MessageBox.Show("是否删除", "提示", MessageBoxButtons.OKCancel)==DialogResult.OK)
{
string s = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
string sql = string.Format("update tb_violation set state=1 where id='{0}'", s);
DBHelper.noqe(sql);
//删除完刷新
string ss= string.Format("select * from tb_violation where carNo='{0}'and state=0", textBox1.Text);
DataTable d = DBHelper.ds(ss).Tables[0];
this.dataGridView1.DataSource =d;
}
else
{
}
}
datagirdview设置
首先设置datagridview的这三个属性
-
- AutoSizeColumsMode = Fill 设置每列单元格宽度平铺
-
- RowHeadersVisible = False 取消列表最左侧列显示
-
- SelectionMode = FullRowSelect 设置单元格选中模式为整行选中
全部代码:
DBHelper类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace 汽车违章信息处理
{
internal class DBHelper
{
public static string connstr = "server=.;database=CarDBA;uid=sa;pwd=123456";
public static SqlConnection conn = null;
public static void into()
{
if (conn == null)
{
conn = new SqlConnection(connstr);
}
conn.Close();
conn.Open();
}
public static bool noqe(string sql)
{
into();
SqlCommand cmd = new SqlCommand(sql, conn);
int ret = cmd.ExecuteNonQuery();
conn.Close();
if (ret > 0)
{
return true;
}
else
{
return false;
}
}
public static DataSet ds(string sql)
{
into();
DataSet d = new DataSet();
SqlDataAdapter t = new SqlDataAdapter(sql, conn);
t.Fill(d);
conn.Close();
return d;
}
}
}
From1主窗体代码
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 汽车违章信息处理
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text=="")
{
MessageBox.Show("请输入数据!");
return;
}
string sql = string.Format("select * from tb_violation where carNo='{0}'and state=0", textBox1.Text);
DataTable s = DBHelper.ds(sql).Tables[0];
if (s.Rows.Count>0)
{
this.dataGridView1.DataSource = s;
}
else
{
MessageBox.Show("该车无违章!");
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.dataGridView1.AutoGenerateColumns= false;
}
private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (MessageBox.Show("是否删除", "提示", MessageBoxButtons.OKCancel)==DialogResult.OK)
{
string s = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
string sql = string.Format("update tb_violation set state=1 where id='{0}'", s);
DBHelper.noqe(sql);
//删除完刷新
string ss= string.Format("select * from tb_violation where carNo='{0}'and state=0", textBox1.Text);
DataTable d = DBHelper.ds(ss).Tables[0];
this.dataGridView1.DataSource =d;
}
else
{
}
}
}
}