4-3-2  DataGridview控件创建的案例教学

1.案例学习:DataGridview基本的数据操作

本实验目标是通过本案例您将学习了解到:
nDataGridView的数据绑定技术;
nDataGridView的更新和删除技术。
用户界面如图460所示:
4-60 DataGridview基本的数据操作应用程序界面图
u实验步骤(1):
VS.NET 2005中新建一个名为示例4的基于Windows的项目。将默认窗体重命名为form9.cs
u实验步骤(2):
从工具箱之中拖拽一个dataGridView控件到Form窗体,ColumnHeadersHeightSizeMode属性设置为“AutoSize”;另外还要向Form窗体下侧添加二个Button控件,text属性分别设置为“更改”、“删除”。
u实验步骤(3):
数据库的设计参见图461
4-61  数据库设计图
数据库为school,共有六个表,该应用程序中只使用了表student。具体字段设计情况请参见图479。数据表student中可以先存放一部分数据,便于后面处理。数据库环境是SQL Server 2005
u实验步骤(4):
用鼠标双击所有Button控件,进入.cs文件编辑状态准备进行开发。代码加下:
//==========动态程序设计部分================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsApplication1
{
    public partial class Form9 : Form
    {
        private SqlConnection mycon;
        private SqlDataAdapter myada;
        private SqlCommand mycomd;
        private SqlCommandBuilder mycbd;
        private DataSet myset;
 
        public Form9()
        {
            InitializeComponent();
            mycon = new SqlConnection("Data Source=LKJ\\SQLEXPRESS;Initial Catalog=school;Integrated Security=True");
            mycomd = new SqlCommand("select * from student",mycon);
            myada = new SqlDataAdapter();
            myada.SelectCommand = mycomd;
            mycbd = new SqlCommandBuilder(myada);
            myset = new DataSet();
            myada.TableMappings.Add("student","student");
            myada.TableMappings[0].ColumnMappings.Add("SNO", "学号");
            myada.TableMappings[0].ColumnMappings.Add("SNAME", "姓名");
            myada.TableMappings[0].ColumnMappings.Add("SEX", "性别");
            myada.TableMappings[0].ColumnMappings.Add("BIRTHDAY", "生日");
            myada.TableMappings[0].ColumnMappings.Add("CLASS", "班级");
           
        }
        /// <summary>
        /// 数据修改
        /// </summary>
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //将更改的数据更新到数据表里
                myada.Update(myset.Tables["student"].GetChanges());
                MessageBox.Show("数据库修改成功","成功信息");
                //DataTable接受更改,以便为下一次更改作准备
                myset.Tables["student"].AcceptChanges();
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        /// <summary>
        /// 初始化数据
        /// </summary>
        private void Form9_Load(object sender, EventArgs e)
        {
            try
            {
                myada.Fill(myset, "student");
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                mycon.Close();
            }
            dataGridView1.DataSource = myset.Tables["student"].DefaultView;
        }
        /// <summary>
        /// 数据删除
        /// </summary>
        private void button2_Click(object sender, EventArgs e)
        {
if (MessageBox.Show("确定要删除当前行数据?", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                try
                {
                    //DataTable中删除当前选中的行
                    myset.Tables[0].Rows[dataGridView1.CurrentRow.Index].Delete();
                    //将更改的数据更新到数据表里
                    myada.Update(myset.Tables[0].GetChanges());
                    MessageBox.Show("数据删除成功!");
                    //DataTable接受更改,以便为下一次更改作准备
                    myset.Tables[0].AcceptChanges();
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            else
            {
                //取消对DataTable的更改
                myset.Tables[0].RejectChanges();
            }
        }
    }
}