-
1
.
BindingNavigator
控件
(
导航条
)
(1)
BindingNavigator
控件的主要作用
BindingNavigator
控件提供了一个用户界面,在这个界
面中提供了一些按
钮,通过这些按钮能够帮助你完成导航记录、添加记录、删除记录的功
能。
(2)
BindingNavigator
控件的组成部分
BindingNavigator
控件本质上就是一个工具栏
(ToolStrip)
控件,在这个工
具栏中
带有一系列的按钮
(ToolStripButton)
、文本
框
(ToolStripTextBox)
、标签
(ToolStripLabel)
和分隔符
(To
olStripSeparator)
等。
< br>BindingNavigator
控件完成大多数常见的与数据有关的操作:<
/p>
定位数据、
添加数据和删除数据等。
默认
情况下,
BindingNavigator
控件如下图所示。
下表列出
BindingNavigator
控件包含的子控件并描述其功能。
< br>
BandingNavigator
控件包含的子控件
MoveFirstItem
按钮
<
/p>
MovePreviousItem
按钮
MoveNextItem
按钮
MoveLastItem
按钮
PositionItem
文本框
CountItem
标签
AddNewItem
按钮
DeleteItem
按钮
(3)
如何使用
BindingNa
vigator
控件
如果你想要使用
BindingNavigator
控件来导航记录、添加记录
和删除记录,
还必须借助
BindingSource
组件,只有这两个控件相互配合,才能够实现这些功
能。
1
BindingNavigator
将表中的记录移到第一行
将表中的记录移到上一行
将表中的记录移到下一行
将表中的记录移到最后一行
显示表中当前行的位置
显示表中的总行数
在表中添加一个新行
删除表中当前行
控件和
BindingSource
组件如何关联起来
?
p>
为了能够让
BindingNavigator
控件和
BindingSource
组件关联起来,
你只需
要设定
BindingNavigat
or
控件的
BindingSource
属性即可,
如下所示:
this
.gSource =
bindSource;
2
Bin
dingNavigator
控件和
BindingSourc
e
组件如何相互配合
?
对于
Bi
ndingNavigator
控件中每一个子控件,
Bind
ingSource
组件中都有一个
对应的方法来提供相应的功
能。
换句话说,当你在
Bindin
gNavigator
控件中点击某个子控件时,将会调用
Bi
ndingSource
组件中的某个方法或某个属性。
下表列
出了
BindingNavigator
控件
< br>中的子控件和
BindingSource
组件中对应的
方法或属性。
BindingNavigator
控件中的子控件和
BindingSource
组
件中对应的方法或属性
BindingNavigator<
/p>
控件中的子控件
MoveFirstItem
按钮
<
/p>
MovePreviousItem
按钮
MoveNextItem
按钮
MoveLastItem
按钮
PositionItem
文本框
CountItem
标签
AddNewItem
按钮
DeleteItem
按钮
BindingSource
组件中对应的方法或属性
< br>
MoveFirst
方法
MovePrevious
方法
MoveNext
方法
MoveLast
方法
Position
属性
C
ount
属性
AddNew
方法
RemoveCurrent
方法
(4)
如何在
BindingNav
igator
控件中添加“保存”和“取消”按钮
由于
BindingNavigator
控件本质
上是一个工具栏
(ToolStrip)
控件,如果你想
要添加“保存”和“取消”按钮,就只需要在这个工具栏中再多添加两个按钮,
并设置这两个按钮的对应的
Image
属性和
Text
属性即可,如下图所示:
2
.
使用导航条配合管理简单数据绑定
下
面通过一个示例来说明,如何使用导航条和
BindingSource
组件相互配合
来管理简单数据绑定。
示例
1
:
创建一个
Windows
应用程序,添加一个窗体,放置一些控件,将
QQ
数据库的
Users
表
中所有用户信息在这些控件中显示出来,
借助
BindingS
ource
组件,
对这些控件进行数据绑定,然后
使用
BindingNavigator
控
件和
BindingSource
组件相互配合
来完成导航、添加、修改和删除记录的功能。
程序的运行界面,如下所示:
程序的主要代码,如下所示:
public
partial
class
Form1
:
Form
{
//
建立数据库连接
private
static
string
connString =
Source=localhost;Initial
Catalog=QQ;Integrated
Security=true
;
public
static
SqlConnection
connection =
new
SqlConnection
(connString);
//
定义并创建数据适配器对象
private
SqlDataAdapter
dataAdapter =
new
SqlDataAdapter
();
//
定义并创建数据集对象
private
DataSet
dataSet =
new
DataSet
(
);
//
定义
BindingSource
对象
private
BindingSource
bindSource;
//
定义全局变量:账号
int
loginId = 0;
public
Form1()
{
InitializeComponent();
}
//
在窗体加载时,填充数据集并建立数据绑定
private
void
Form1_Load(
object
sender,
EventArgs
e)
{
try
{
//
查询记录用的
SQL
语句
string
strSql =
Sex, Age from
Users
;
//
创建
SqlCommand
对象,发布查询操作命令。
SqlCommand
selectCmd =
new
SqlCommand
(strSql,
connection);
//
设置
DataAdapter
对象的
SelectCommand
属
性
//
委托
SqlCommand
对象执行查询操作任务
Command = selectCmd;
//
使用数据适配器填充数据集
(dataSet,
);
//
创建
BindingSource
对象,将
BindingSource
组件绑定
到数据源。
bindSource =
new
BindingSource
(dataSet,
);
//
建立简单数据绑定,将控件绑定到
BindingSource
组件。
this
.(
, bindSource,
);
this
< br>.(
, bindSource,
);
this
.(
, bindSource,
);
this
< br>.(
, bindSource,
);
this
.(
, bindSource,
);
this
< br>.(
, bindSource,
);
//
将
BindingNavigator
控件和
BindingSource
组件关联
起来
this
.gSource =
bindSource;
}
catch
(
Exception
ex)
{
MessageBox
.Show(e);
}
}
//
删除
(注意:在此要触发
MouseDown
事件来获取账号)
private
void
bindingNavigatorDeleteI
tem_MouseDown(
object
sender,
MouseEventArgs
e)
{
//
删除之前,先获取账号文本框的值。
this
.loginId =
Co
nvert
.ToInt32(
this
.);
}
//
保存
private
void
btnSave_Click(
object
sender,
EventArgs
e)
{
try
{
//
结束当前编辑操作
this
.t();
//
先判
断数据集是否发生了改变
,
如果数据集没有改变,则不用
同步更新后台真实数据库
if
(
this
.nges() ==
false
)
{
return
;
}
//
添加记录用的
SQL
语句
string
insertSql =
String
.Format(
(
LoginPwd, NickName, Name, Sex, Age,
FaceId,ProvinceId, CityId)
+
,
this
.,
this
.,
this
.,
this
.,
this
.);
//
修改记录用的
SQL
语句
string
updateSql =
String
.Format(
L
oginPwd
= '{0}', NickName = '{1}', Name
= '{2}', Sex = '{3}', Age
= {4}
where Id = {5}
,
this
.,
this
.,
this
.,
this
.,
this
.,
this
.);
//
删除记录用的
SQL
语句
string
deleteSql =
String
.Format(
w
here Id = {0}
, loginId);
SqlCommand
insertCmd =
new
SqlCommand
(insertSql,
connection);
SqlCommand
updateCmd =
new
SqlCommand
(updateSql,
connection);
SqlCommand
deleteCmd =
new
SqlCommand
(deleteSql,
connection);
this
.Command = insertCmd; <
/p>
//
执行插
入操作
this
.Command = updateCmd; <
/p>
//
执行修
改操作
this
.Command = deleteCmd; <
/p>
//
执行删
除操作
/*
*
调用
DataAdapter
对象的
Update
方法,
*
将数据集
(
虚拟数据库
)
中的更
改
(
增、删、改
)
,提交给
后台真实数据库,