2.案例学习:DataGridView基本的数据操作
本实验目标是通过本案例您将学习了解到:
n
DataGridView
的数据绑定技术;
n
数据上下顶底的移动技术;
n
DataGridView
的更新和删除技术;
n
window
控件的综合应用。
用户界面如图4-62所示:
图4-62 基本的数据操作应用程序界面图
u
实验步骤(1):
在
VS
.
NET
2005中新建一个名为示例5的基于
Windows
的项目。将默认窗体重命名为
form8
.
cs
。
u
实验步骤(2):
从工具箱之中拖拽一个
GroupBox
控件到
From
窗体,
Text
属性设置为“数据信息显示区”;向这个
GroupBox
控件拖拽一个
dataGridView
控件,
ColumnHeadersHeightSizeMode
属性设置为“
AutoSize
”,
Dock
属性设置为“
Fill
”,
SelectionMode
属性设置为“
FullRowSelect
”;还要向这个
GroupBox
控件拖拽七个
Button
控件,
text
属性分别为“
|<
”、“
<
”、“
>
”、“
>|
”、“更新
(U)
”、“删除
(D)
”、“退出
(E)
”;另外还要向
Form
窗体下侧添加一个
StatusStrip
控件,选中这个
StatusStrip
控件,在右侧属性栏找到
Items
属性,点击右侧的“
…
”按钮进入项目集合编辑器,在下拉列表框选中“
StatusLabel
”后点击旁边的“添加”按钮,成员栏出现一个“
ToolStripStatusLabel
”项,再一次在下拉列表框选中“
ToolStripProgressBar
”后点击旁边的“添加”按钮,成员栏出现一个“
ToolStripProgressBar
”项。
u
实验步骤(3):
数据库的设计参见图4-63:
图4-63 数据库设计图
数据库为
school1
,仅用了两个表,
mz
是表示民族的表,
student
是表示学生的表。具体字段设计情况请参见图4-63。数据库环境是
SQL
Server
2005。
u
实验步骤(4):
鼠标右击项目,在弹出的菜单中选择“添加”、“新建项”,在弹出的“添加新项”窗体中选择“代码文件”,名称改为“
DataBase
.cs”,点击“添加”按钮,完成添加。该类库文件前面已有论述,此处不再复述。
u
实验步骤(5):
用鼠标双击所有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 Form8 : Form
{
public Form8()
{
InitializeComponent();
}
DataBase mydatabase = new DataBase();
//
注意:下面两个变量ds,dt很重要,负责保留状态信息。
public DataSet ds = new DataSet();
public DataTable dt = new DataTable();
/// <summary>
///
更新数据信息
/// </summary>
public void updatData()
{
String sql = "select * from student";
if (mydatabase.doUpdate(ds, sql, dt.TableName))
{
MessageBox
.Show("
数据更新成功"
, "
提示"
, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("
数据更新失败,请检查数据是否合法!"
, "
提示"
, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
/// <summary>
///
初始化加载事件代码
/// </summary>
private void Form8_Load(object sender, EventArgs e)
{
ds = mydatabase.GetDataSet("select * from student");
dt = ds.Tables[0];
dataGridView1.DataSource = dt.DefaultView;
}
/// <summary>
///
更新数据信息
/// </summary>
private void btnUpdata_Click(object sender, EventArgs e)
{
updatData();//
更新数据
}
///
<summary>
///
删除数据信息
/// </summary>
private void btnDelete_Click(object sender, EventArgs e)
{
if (DialogResult.Yes == MessageBox.Show("
数据删除后将不可恢复,真的要删除吗?"
, "
提示"
, MessageBoxButtons.YesNo,MessageBoxIcon.Warning))
{
dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
dataGridView1.Refresh();
updatData();//
更新数据
}
}
/// <summary>
///
到头
/// </summary>
private void button1_Click(object sender, EventArgs e)
{
int i = dataGridView1.SelectedRows[0].Index;
dataGridView1.Rows[i].Selected = false;
dataGridView1.Rows[0].Selected = true;
showmess();
}
/// <summary>
///
下一条
/// </summary>
private void button3_Click(object sender, EventArgs e)
{
int i = dataGridView1.SelectedRows[0].Index;
if (i < dataGridView1.Rows.Count - 1)
{
dataGridView1.Rows[i].Selected = false;
dataGridView1.Rows[i + 1].Selected = true;
showmess();
}
else
{
MessageBox
.Show("
已经是最后一项了!"
, "
提示"
, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
/// <summary>
///
上一条
/// </summary>
private void button2_Click(object sender, EventArgs e)
{
int i = dataGridView1.SelectedRows[0].Index;
if (i != 0)
{
dataGridView1.Rows[i].Selected = false;
dataGridView1.Rows[i - 1].Selected = true;
showmess();
}
else
{
MessageBox
.Show("
已经是第一项了!"
, "
提示"
, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
/// <summary>
///
到尾
/// </summary>
private void button4_Click(object sender, EventArgs e)
{
int i = dataGridView1.SelectedRows[0].Index;
dataGridView1.Rows[i].Selected = false;
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Selected = true;
showmess();
}
/// <summary>
///
底部显示信息
/// </summary>
private void showmess()
{
toolStripStatusLabel1.Text = string.Format("
当前是第{0}条记录,共{1}条记录"
,dataGridView1.SelectedRows[0].Index+1,dataGridView1.Rows.Count);
toolStripProgressBar1.Value = (int)((double)(dataGridView1.SelectedRows[0].Index) / dataGridView1.Rows.Count*100);
}
///
退出系统
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
本文转自 qianshao 51CTO博客,原文链接:http://blog.51cto.com/qianshao/216109,如需转载请自行联系原作者