利用DataGridView类编写的学生信息登记系统

步骤一

数据库以及表的创建

利用DataGridView类编写的学生信息登记系统_第1张图片

*对于数据库张字段的数据类型的使用,方便数据在输入的时候不会出错,进而经常会使用varchar类型

*对于涉及到的数据库连接对单独放到可配置文件当中

即app文件

利用DataGridView类编写的学生信息登记系统_第2张图片

 

步骤二

利用winform窗体的进行相关控件的使用

*主要控件的使用DateGridView控件

DateGridView控件的相关的属性:row 、cells、

DateGridView控件的相关的事件:cellsClick

DateGridView控件的相关的类:DateGridViewRow

利用DataGridView类编写的学生信息登记系统_第3张图片

步骤三

在vs中进行编码的编写

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace xiangmu
{
    public partial class 数据查询 : Form
    {
        public 数据查询()
        {
            InitializeComponent();
        }

  private void Date()
        {

              #region   对数据库中的数据进行查询
            //创建可配置文件,进行存放数据库的连接
            string sql = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
            //应用SqlConnection类建立连接的对象
            SqlConnection conn = new SqlConnection(sql);
            //打开数据库
            conn.Open();
            //应用SqlCommand类建立命令对象
            //三种格式创建命令对象
            SqlCommand comm = conn.CreateCommand();
            comm.CommandText = "select *from information where name=@name or Mobile =@Mobile ";
            /*SqlCommand comm=new SqlCommand();
            comm.Connection = conn;
            comm.CommandText = "select *from information where name=@name or Mobile =@Mobile ";*/
            /*SqlCommand comm=new SqlCommand("select *from information where name=@name or Mobile =@Mobile",conn);*/
            comm.Parameters.AddWithValue("@name", textBox1.Text.ToString());
            comm.Parameters.AddWithValue("@Mobile", textBox1.Text.ToString());
            //应用数据适配器创建对象,执行数据库语句,获得相关的数据
            SqlDataAdapter adapter = new SqlDataAdapter(comm);
            //应用数据表类,创建对象,进行数据库的数据的存放
            DataTable data = new DataTable();
            //将从数据库获得信息放置在数据表类中的一个对象当中,实现从数据库中显示在vs中
            adapter.Fill(data);
            this.dataGridView1.DataSource = data;
            //数据库连接的关闭
            conn.Close();
            //命令的释放
            comm.Dispose();
            //连接的释放
            #endregion

}

     private void button1_Click(object sender, EventArgs e)
        {
            //对数据查询函数的调用
            Date();
        }
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            #region 从 DataGridView控件中获得相关的信息
            if (this.dataGridView1.SelectedRows.Count > 0)
            {
                //应用 DataGridViewRow 类创建他的对象,
                DataGridViewRow row = this.dataGridView1.SelectedRows[0];
 
                //从 DataGridView控件中获得相关的信息
                this.textBox2.Text = row.Cells[0].Value.ToString();
                this.textBox6.Text = row.Cells[1].Value.ToString();
                string gender = row.Cells[2].Value.ToString();
                if (gender == "男")
                {
                    radioButton1.Checked = true;
                }
                if (gender == "女")
                {
                    radioButton2.Checked = true;
                }
                this.textBox3.Text = row.Cells[3].Value.ToString();
                this.comboBox1.Text = "";
                this.comboBox1.Text = row.Cells[4].Value.ToString();
                this.comboBox2.Text = "";
                this.comboBox2.Text = row.Cells[5].Value.ToString();
                this.textBox4.Text = row.Cells[6].Value.ToString();
                this.textBox5.Text = row.Cells[7].Value.ToString();
                #endregion
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            #region   对数据中的数据进行更改
            string sql = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
            SqlConnection conn = new SqlConnection(sql);
            conn.Open();
            SqlCommand comm = conn.CreateCommand();
            string id = textBox1.Text;
            //对数据进行更改
            comm.CommandText = "update information set  name=@name, sex=@sex ,Mobile=@Mobile,fasten=@fasten ,award=@award ,scord=@scord ,address=@address whereId=@id ";
            comm.Parameters.AddWithValue("@id", textBox6.Text.ToString());
            comm.Parameters.AddWithValue("@name", textBox2.Text.ToString());
            if (radioButton1.Checked == true)
            {
                comm.Parameters.AddWithValue("@sex", radioButton1.Text.ToString());
            }
            if (radioButton2.Checked == true)
            {
                comm.Parameters.AddWithValue("@sex", radioButton2.Text.ToString());
            }
            comm.Parameters.AddWithValue("@Mobile", textBox3.Text.ToString());
            comm.Parameters.AddWithValue("@fasten", comboBox1.Text.ToString());
            comm.Parameters.AddWithValue("@award", comboBox2.Text.ToString());
            comm.Parameters.AddWithValue("@scord ", textBox4.Text.ToString());
            comm.Parameters.AddWithValue("@address", textBox5.Text.ToString());
            comm.ExecuteNonQuery();
            MessageBox.Show("更改成功");
            Date();
            #endregion
        }

        private void button3_Click(object sender, EventArgs e)
        {
            #region 对数据库中的数据进行删除
            DataGridViewRow row = this.dataGridView1.SelectedRows[0];
            string sql = "Data Source=(local);Initial Catalog=denglu;Integrated Security=True";
            SqlConnection conn = new SqlConnection(sql);
            conn.Open();
            SqlCommand comm = conn.CreateCommand();
            string id = textBox1.Text;
            comm.CommandText = "delete from  information where Id=@Id";

            comm.Parameters.AddWithValue("@Id", row.Cells[1].Value.ToString());
            comm.ExecuteNonQuery();
            comm.Dispose();
            conn.Close();
            conn.Dispose();
            MessageBox.Show("删除成功");
            textBox2.Text = "";
            textBox6.Text = "";
            textBox3.Text = "";
            comboBox1.Text = "";
            comboBox2.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            //获取当前选中的行,返回值为DataGridViewRow
            this.dataGridView1.Rows.Remove(row);
            #endregion
        }
    }
}

       在很多时候坐在电脑面前编写代码,对一个小程序的思考后,按照自己的思路将效果实现,这里的思考,有时我们会把问题思考的变得稍微难一些。其结果,我们可以看到几行代码就可以让我们看到很好的有趣的程序的诞生,所以,我们对于编程,用简单的代码编写最完美的程序。

 

 

 

 

 

 

你可能感兴趣的:(.NET,c#,csdn,winform)