C#可视化 汽车违章处理系统(具体做法及全部代码)

题目:

 

 

 效果图:

 

 数据库:

做法:

点击查询按钮查询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的这三个属性

    1. AutoSizeColumsMode = Fill 设置每列单元格宽度平铺

    1. RowHeadersVisible = False 取消列表最左侧列显示

    1. 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
            {

            }
           


        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

W少年没有乌托邦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值