C#修改DataGridview里的数据

版权声明:转载原创文章请以超链接形式请注明原文章出处,尊重作者,尊重原创!


恰饭广告




创建表:(或者设置列名属性,标识-是,增量-1,种子-166122001)

--创建表
create table T_StuInfo
(
StuNO int identity(166122001,1),
StuName varchar(20),
StuSex varchar(2)
)
--插入测试数据
insert into T_StuInfo(StuName,StuSex) values('admin','男')
insert into T_StuInfo(StuName,StuSex) values('daobin','女')
--从166122000开始
dbcc checkident('T_StuInfo',RESEED,166122000) 

表结构:

数据库表

实现效果图:

实现效果

Form1.cs主要代码:

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;
using System.Data.SqlClient;
using System.Configuration;
namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlConnection conn;
        SqlDataAdapter sda;
        SqlCommand cmd;
        private void loadData()
        {
            sda = new SqlDataAdapter("select * from T_StuInfo", conn);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
        }
        private void bindData()
        {
            string stuNO = textBox1.Text.Trim();
            string stuName = textBox2.Text.Trim();
            string stuSex = textBox3.Text.Trim();
            cmd = conn.CreateCommand();
            cmd.Parameters.AddWithValue("@StuNO", stuNO);
            cmd.Parameters.AddWithValue("@StuName", stuName);
            cmd.Parameters.AddWithValue("@StuSex", stuSex);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            string testDB = ConfigurationManager.ConnectionStrings["testDB"].ConnectionString;
            conn = new SqlConnection(testDB);
            loadData();
            string[] htext = {"学号","姓名","性别" };
            for (int i=0;i<htext.Length;i++)
            {
                dataGridView1.Columns[i].HeaderText=htext[i];
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string stuNO = textBox1.Text.Trim();
            string stuName = textBox2.Text.Trim();
            string stuSex = textBox3.Text.Trim();
            try
            {
                conn.Open();
                cmd = conn.CreateCommand();
                cmd.CommandText = "select * from T_StuInfo where StuNO = @StuNO";
                cmd.Parameters.AddWithValue("@StuNO", stuNO);
                cmd.Parameters.AddWithValue("@StuName", stuName);
                cmd.Parameters.AddWithValue("@StuSex", stuSex);
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    //判断读取到的数据库中的学号与输入的学号是否相同
                    if (reader["StuNO"].ToString() == stuNO)
                    {
                        MessageBox.Show("学号已存在");
                        this.textBox1.Focus();
                        this.textBox1.SelectAll();
                    }
                    //关闭读取器
                    reader.Close();
                }
                else
                {
                    //关闭读取器
                    reader.Close();
                    cmd.CommandText = "insert into T_StuInfo (StuName,StuSex) values(@stuName, @stuSex)";
                    //获取执行sql语句后受影响的行数
                    int rowCount = cmd.ExecuteNonQuery();
                    if (rowCount == 1) //Update、Insert和Delete返回1,其他返回-1
                    {
                        MessageBox.Show("学生【" + this.textBox2.Text + "】录入成功!");
                        this.textBox1.Text = "";
                        this.textBox2.Text = "";
                        this.textBox3.Text = "";
                        reader.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                conn.Close();
            }
            loadData();
        }
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int index = dataGridView1.CurrentRow.Index;
            textBox1.Text = dataGridView1.Rows[index].Cells["stuNO"].Value.ToString();
            textBox2.Text = dataGridView1.Rows[index].Cells["stuName"].Value.ToString();
            textBox3.Text = dataGridView1.Rows[index].Cells["stuSex"].Value.ToString();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("是否修改学生【" + this.textBox2.Text + "】", "修改", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (dr == DialogResult.Yes)
            {
                try
                {
                    conn.Open();
                    bindData();
                    cmd.CommandText = "update T_StuInfo set StuName=@stuName,StuSex=@stuSex where StuNO=@StuNO";
                    int rowCount = cmd.ExecuteNonQuery();
                    if (rowCount > 0)
                    {
                        MessageBox.Show("学生【" + this.textBox2.Text + "】修改成功!");
                        textBox1.Text = "";
                        textBox2.Text = "";
                        textBox3.Text = "";
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    conn.Close();
                }
            }
            else
            {
                return;
            }
            loadData();
        }
        private void button3_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("是否删除学生【" + this.textBox2.Text + "】", "删除", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (dr == DialogResult.Yes)
            {
                try
                {
                    conn.Open();
                    bindData();
                    cmd.CommandText = "delete from T_StuInfo where StuNO=@StuNO";
                    int rowCount = cmd.ExecuteNonQuery();
                    if (rowCount == 1)
                    {
                        MessageBox.Show("学生【" + this.textBox2.Text + "】删除成功!");
                        textBox1.Text = "";
                        textBox2.Text = "";
                        textBox3.Text = "";
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    conn.Close();
                }
            }
            else
            {
                return;
            }
            loadData();
        }
    }
}

原文链接:https://www.idaobin.com/archives/1064.html

让我恰个饭吧.ヘ( ̄ω ̄ヘ)

支付宝 ——————- 微信
图片加载中图片加载中



恰饭广告

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

× 8 = 40