WinForm之中BindingNavigator控件的使用
在微软WinForm中,BindingNavigator控件主要用来绑定数据。可以将一个数据集合与该控件绑定,以进行数据

联动的显示效果

窗体设计器:

代码

partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null ;

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing"> 如果应释放托管资源,为 true;否则为 false。 </param>
protected override void Dispose( bool disposing)
{
if (disposing && (components != null ))
{
components.Dispose();
}
base .Dispose(disposing);
}

#region Windows 窗体设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this .components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager( typeof (Form1));
this .bindingNavigator1 = new System.Windows.Forms.BindingNavigator( this .components);
this .bindingNavigatorMoveFirstItem = new System.Windows.Forms.ToolStripButton();
this .bindingNavigatorMovePreviousItem = new System.Windows.Forms.ToolStripButton();
this .bindingNavigatorSeparator = new System.Windows.Forms.ToolStripSeparator();
this .bindingNavigatorPositionItem = new System.Windows.Forms.ToolStripTextBox();
this .bindingNavigatorCountItem = new System.Windows.Forms.ToolStripLabel();
this .bindingNavigatorSeparator1 = new System.Windows.Forms.ToolStripSeparator();
this .bindingNavigatorMoveNextItem = new System.Windows.Forms.ToolStripButton();
this .bindingNavigatorMoveLastItem = new System.Windows.Forms.ToolStripButton();
this .bindingNavigatorSeparator2 = new System.Windows.Forms.ToolStripSeparator();
this .bindingNavigatorAddNewItem = new System.Windows.Forms.ToolStripButton();
this .bindingNavigatorDeleteItem = new System.Windows.Forms.ToolStripButton();
this .dataGridView1 = new System.Windows.Forms.DataGridView();
this .toolStripTextBox1 = new System.Windows.Forms.ToolStripTextBox();
this .toolStripButton2 = new System.Windows.Forms.ToolStripButton();
((System.ComponentModel.ISupportInitialize)( this .bindingNavigator1)).BeginInit();
this .bindingNavigator1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)( this .dataGridView1)).BeginInit();
this .SuspendLayout();
//
// bindingNavigator1
//
this .bindingNavigator1.AddNewItem = this .bindingNavigatorAddNewItem;
this .bindingNavigator1.CountItem = this .bindingNavigatorCountItem;
this .bindingNavigator1.DeleteItem = null ;
this .bindingNavigator1.Items.AddRange( new System.Windows.Forms.ToolStripItem[] {
this .bindingNavigatorMoveFirstItem,
this .bindingNavigatorMovePreviousItem,
this .bindingNavigatorSeparator,
this .bindingNavigatorPositionItem,
this .bindingNavigatorCountItem,
this .bindingNavigatorSeparator1,
this .bindingNavigatorMoveNextItem,
this .bindingNavigatorMoveLastItem,
this .bindingNavigatorSeparator2,
this .bindingNavigatorAddNewItem,
this .bindingNavigatorDeleteItem,
this .toolStripTextBox1,
this .toolStripButton2});
this .bindingNavigator1.Location = new System.Drawing.Point( 0 , 0 );
this .bindingNavigator1.MoveFirstItem = this .bindingNavigatorMoveFirstItem;
this .bindingNavigator1.MoveLastItem = this .bindingNavigatorMoveLastItem;
this .bindingNavigator1.MoveNextItem = this .bindingNavigatorMoveNextItem;
this .bindingNavigator1.MovePreviousItem = this .bindingNavigatorMovePreviousItem;
this .bindingNavigator1.Name = " bindingNavigator1 " ;
this .bindingNavigator1.PositionItem = this .bindingNavigatorPositionItem;
this .bindingNavigator1.Size = new System.Drawing.Size( 470 , 25 );
this .bindingNavigator1.TabIndex = 0 ;
this .bindingNavigator1.Text = " bindingNavigator1 " ;
//
// bindingNavigatorMoveFirstItem
//
this .bindingNavigatorMoveFirstItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this .bindingNavigatorMoveFirstItem.Image = ((System.Drawing.Image)(resources.GetObject( " bindingNavigatorMoveFirstItem.Image " )));
this .bindingNavigatorMoveFirstItem.Name = " bindingNavigatorMoveFirstItem " ;
this .bindingNavigatorMoveFirstItem.RightToLeftAutoMirrorImage = true ;
this .bindingNavigatorMoveFirstItem.Size = new System.Drawing.Size( 23 , 22 );
this .bindingNavigatorMoveFirstItem.Text = " 移到第一条记录 " ;
this .bindingNavigatorMoveFirstItem.Click += new System.EventHandler( this .bindingNavigatorMoveFirstItem_Click);
//
// bindingNavigatorMovePreviousItem
//
this .bindingNavigatorMovePreviousItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this .bindingNavigatorMovePreviousItem.Image = ((System.Drawing.Image)(resources.GetObject( " bindingNavigatorMovePreviousItem.Image " )));
this .bindingNavigatorMovePreviousItem.Name = " bindingNavigatorMovePreviousItem " ;
this .bindingNavigatorMovePreviousItem.RightToLeftAutoMirrorImage = true ;
this .bindingNavigatorMovePreviousItem.Size = new System.Drawing.Size( 23 , 22 );
this .bindingNavigatorMovePreviousItem.Text = " 移到上一条记录 " ;
this .bindingNavigatorMovePreviousItem.Click += new System.EventHandler( this .bindingNavigatorMovePreviousItem_Click);
//
// bindingNavigatorSeparator
//
this .bindingNavigatorSeparator.Name = " bindingNavigatorSeparator " ;
this .bindingNavigatorSeparator.Size = new System.Drawing.Size( 6 , 25 );
//
// bindingNavigatorPositionItem
//
this .bindingNavigatorPositionItem.AccessibleName = " 位置 " ;
this .bindingNavigatorPositionItem.AutoSize = false ;
this .bindingNavigatorPositionItem.Name = " bindingNavigatorPositionItem " ;
this .bindingNavigatorPositionItem.Size = new System.Drawing.Size( 50 , 21 );
this .bindingNavigatorPositionItem.Text = " 0 " ;
this .bindingNavigatorPositionItem.ToolTipText = " 当前位置 " ;
//
// bindingNavigatorCountItem
//
this .bindingNavigatorCountItem.Name = " bindingNavigatorCountItem " ;
this .bindingNavigatorCountItem.Size = new System.Drawing.Size( 35 , 22 );
this .bindingNavigatorCountItem.Text = " / {0} " ;
this .bindingNavigatorCountItem.ToolTipText = " 总项数 " ;
//
// bindingNavigatorSeparator1
//
this .bindingNavigatorSeparator1.Name = " bindingNavigatorSeparator " ;
this .bindingNavigatorSeparator1.Size = new System.Drawing.Size( 6 , 25 );
//
// bindingNavigatorMoveNextItem
//
this .bindingNavigatorMoveNextItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this .bindingNavigatorMoveNextItem.Image = ((System.Drawing.Image)(resources.GetObject( " bindingNavigatorMoveNextItem.Image " )));
this .bindingNavigatorMoveNextItem.Name = " bindingNavigatorMoveNextItem " ;
this .bindingNavigatorMoveNextItem.RightToLeftAutoMirrorImage = true ;
this .bindingNavigatorMoveNextItem.Size = new System.Drawing.Size( 23 , 22 );
this .bindingNavigatorMoveNextItem.Text = " 移到下一条记录 " ;
this .bindingNavigatorMoveNextItem.Click += new System.EventHandler( this .bindingNavigatorMoveNextItem_Click);
//
// bindingNavigatorMoveLastItem
//
this .bindingNavigatorMoveLastItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this .bindingNavigatorMoveLastItem.Image = ((System.Drawing.Image)(resources.GetObject( " bindingNavigatorMoveLastItem.Image " )));
this .bindingNavigatorMoveLastItem.Name = " bindingNavigatorMoveLastItem " ;
this .bindingNavigatorMoveLastItem.RightToLeftAutoMirrorImage = true ;
this .bindingNavigatorMoveLastItem.Size = new System.Drawing.Size( 23 , 22 );
this .bindingNavigatorMoveLastItem.Text = " 移到最后一条记录 " ;
this .bindingNavigatorMoveLastItem.Click += new System.EventHandler( this .bindingNavigatorMoveLastItem_Click);
//
// bindingNavigatorSeparator2
//
this .bindingNavigatorSeparator2.Name = " bindingNavigatorSeparator " ;
this .bindingNavigatorSeparator2.Size = new System.Drawing.Size( 6 , 25 );
//
// bindingNavigatorAddNewItem
//
this .bindingNavigatorAddNewItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this .bindingNavigatorAddNewItem.Image = ((System.Drawing.Image)(resources.GetObject( " bindingNavigatorAddNewItem.Image " )));
this .bindingNavigatorAddNewItem.Name = " bindingNavigatorAddNewItem " ;
this .bindingNavigatorAddNewItem.RightToLeftAutoMirrorImage = true ;
this .bindingNavigatorAddNewItem.Size = new System.Drawing.Size( 23 , 22 );
this .bindingNavigatorAddNewItem.Text = " 新添 " ;
//
// bindingNavigatorDeleteItem
//
this .bindingNavigatorDeleteItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this .bindingNavigatorDeleteItem.Image = ((System.Drawing.Image)(resources.GetObject( " bindingNavigatorDeleteItem.Image " )));
this .bindingNavigatorDeleteItem.Name = " bindingNavigatorDeleteItem " ;
this .bindingNavigatorDeleteItem.RightToLeftAutoMirrorImage = true ;
this .bindingNavigatorDeleteItem.Size = new System.Drawing.Size( 23 , 22 );
this .bindingNavigatorDeleteItem.Text = " 删除 " ;
this .bindingNavigatorDeleteItem.Click += new System.EventHandler( this .bindingNavigatorDeleteItem_Click);
//
// dataGridView1
//
this .dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this .dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
this .dataGridView1.Location = new System.Drawing.Point( 0 , 25 );
this .dataGridView1.Name = " dataGridView1 " ;
this .dataGridView1.RowTemplate.Height = 23 ;
this .dataGridView1.Size = new System.Drawing.Size( 470 , 255 );
this .dataGridView1.TabIndex = 1 ;
//
// toolStripTextBox1
//
this .toolStripTextBox1.Name = " toolStripTextBox1 " ;
this .toolStripTextBox1.Size = new System.Drawing.Size( 100 , 25 );
//
// toolStripButton2
//
this .toolStripButton2.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this .toolStripButton2.Image = ((System.Drawing.Image)(resources.GetObject( " toolStripButton2.Image " )));
this .toolStripButton2.ImageTransparentColor = System.Drawing.Color.Magenta;
this .toolStripButton2.Name = " toolStripButton2 " ;
this .toolStripButton2.Size = new System.Drawing.Size( 23 , 22 );
this .toolStripButton2.Text = " toolStripButton2 " ;
this .toolStripButton2.Click += new System.EventHandler( this .toolStripButton2_Click);
//
// Form1
//
this .AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this .AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this .ClientSize = new System.Drawing.Size( 470 , 280 );
this .Controls.Add( this .dataGridView1);
this .Controls.Add( this .bindingNavigator1);
this .Name = " Form1 " ;
this .Text = " Form1 " ;
this .Load += new System.EventHandler( this .Form1_Load);
((System.ComponentModel.ISupportInitialize)( this .bindingNavigator1)).EndInit();
this .bindingNavigator1.ResumeLayout( false );
this .bindingNavigator1.PerformLayout();
((System.ComponentModel.ISupportInitialize)( this .dataGridView1)).EndInit();
this .ResumeLayout( false );
this .PerformLayout();

}

#endregion

private System.Windows.Forms.BindingNavigator bindingNavigator1;
private System.Windows.Forms.ToolStripButton bindingNavigatorAddNewItem;
private System.Windows.Forms.ToolStripLabel bindingNavigatorCountItem;
private System.Windows.Forms.ToolStripButton bindingNavigatorDeleteItem;
private System.Windows.Forms.ToolStripButton bindingNavigatorMoveFirstItem;
private System.Windows.Forms.ToolStripButton bindingNavigatorMovePreviousItem;
private System.Windows.Forms.ToolStripSeparator bindingNavigatorSeparator;
private System.Windows.Forms.ToolStripTextBox bindingNavigatorPositionItem;
private System.Windows.Forms.ToolStripSeparator bindingNavigatorSeparator1;
private System.Windows.Forms.ToolStripButton bindingNavigatorMoveNextItem;
private System.Windows.Forms.ToolStripButton bindingNavigatorMoveLastItem;
private System.Windows.Forms.ToolStripSeparator bindingNavigatorSeparator2;
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.ToolStripTextBox toolStripTextBox1;
private System.Windows.Forms.ToolStripButton toolStripButton2;
}

代码设计器:
代码

public partial class Form1 : Form
{

SqlConnection conn = new SqlConnection( " Data Source=192.168.1.56;Initial Catalog=RenMinRoadBright;User ID=sa;Password=tendency; " );
SqlDataAdapter ada = new SqlDataAdapter();
BindingSource bind = new BindingSource();
public Form1()
{
InitializeComponent();
}

void BindData( string strcontion)
{
DataSet ds = new DataSet();
ada.SelectCommand = new SqlCommand();
ada.SelectCommand.CommandText = " select * from Instruction " + strcontion; // 指令连接池
ada.SelectCommand.CommandType = CommandType.Text;
ada.SelectCommand.Connection = conn;
ada.Fill(ds, " table " );

dataGridView1.AutoGenerateColumns = true ;
dataGridView1.DataSource = ds;
dataGridView1.DataMember = " table " ;

dataGridView1.MultiSelect = false ; // 不可多选

bind.DataSource = ds.Tables[ " table " ];
bindingNavigator1.BindingSource = bind;

}
// 绑定数据
private void Form1_Load( object sender, EventArgs e)
{
BindData( "" );
}

// 弹出是否删除项
private void bindingNavigatorDeleteItem_Click( object sender, EventArgs e)
{
if ( ! ( dataGridView1.CurrentRow.IsNewRow))
{
if (MessageBox.Show( " 确认删除该笔数据? " , " 请选择 " , MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
{
dataGridView1.Rows.Remove( this .dataGridView1.CurrentRow);

}
}

}

// 查询绑定数据
private void toolStripButton2_Click( object sender, EventArgs e)
{
BindData( " where I_EquipmentID= " + Convert.ToInt32(toolStripTextBox1.Text.Trim()));
}
// 上一条记录
private void bindingNavigatorMovePreviousItem_Click( object sender, EventArgs e)
{
bind.MovePrevious();
}
// 第一条记录
private void bindingNavigatorMoveFirstItem_Click( object sender, EventArgs e)
{
bind.MoveFirst();
}
// 下一条记录
private void bindingNavigatorMoveNextItem_Click( object sender, EventArgs e)
{
bind.MoveNext();
}
// 最后一条记录
private void bindingNavigatorMoveLastItem_Click( object sender, EventArgs e)
{
bind.MoveLast();
}

// 定位 item=this.BindingNavigator.PositionItem;
}

转载于:https://www.cnblogs.com/Sue_/articles/1657344.html

《WinForm开发系列之控件篇》Item2 BindingNavigator相关推荐

  1. 《WinForm开发系列之控件篇》Item1 BackgroungWorker

    cranejuan的专栏 BackgroundWorker实现原理 winfom組件---BackgroundWorker 转载于:https://www.cnblogs.com/Sue_/artic ...

  2. 《WinForm开发系列之控件篇》Item18 FileSystemWatcher(暂无)

    暂无 转载于:https://www.cnblogs.com/Sue_/articles/1657381.html

  3. 《WinForm开发系列之控件篇》Item28 LinkView(暂无)

    暂无 转载于:https://www.cnblogs.com/Sue_/articles/1657443.html

  4. 《WinForm开发系列之控件篇》Item22 HelpProvider(暂无)

    暂无 转载于:https://www.cnblogs.com/Sue_/articles/1657390.html

  5. 《WinForm开发系列之控件篇》Item33 NotifyIcon(暂无)

    暂无 转载于:https://www.cnblogs.com/Sue_/articles/1657452.html

  6. 《WinForm开发系列之控件篇》Item13 DirectoryEntry(暂无)

    暂无 转载于:https://www.cnblogs.com/Sue_/articles/1657367.html

  7. 《WinForm开发系列之控件篇》Item25 Lable(暂无)

    暂无 转载于:https://www.cnblogs.com/Sue_/articles/1657440.html

  8. 《WinForm开发系列之控件篇》Item3 BindingSource (暂无)

    暂无 转载于:https://www.cnblogs.com/Sue_/articles/1657348.html

  9. 《WinForm开发系列之控件篇》Item31 MenuStrip(暂无)

    暂无 转载于:https://www.cnblogs.com/Sue_/articles/1657446.html

最新文章

  1. tidevice安装(MacOS)
  2. 基础数据仓库环境搭建(三)Zookeeper的安装与配置和操作
  3. Winform打砖块游戏制作step by step第5节---重构代码,利用继承多态
  4. 【题解】弃疗Nim (2019,5.23)
  5. 《Python数据挖掘:概念、方法与实践》——1.5节小结
  6. oracle触发器中保存临时数据,Oracle使用游标触发器存储实操
  7. linux 中用vi编辑器替换字符
  8. dsp c语言程序设计,DSP C语言程序设计.pdf
  9. 【鲲鹏HCIA考试】随堂习题卷三
  10. DeepinXP_V5.8完美/增强精简版2合1
  11. Case when的用法
  12. 计算机专业暑假实践心得7篇,计算机专业实习周记总结10篇
  13. dell进入u盘启动模式_戴尔笔记本u盘启动按F12没有usb启动项 dellPC用f12没有u盘启动如何办...
  14. 双代号网络图节点编号原则_网络绘图进行的节点编号时原则
  15. 心肌损伤的标志物题库【1】
  16. iOS组件化——蘑菇街案例分析
  17. 用计算机弹歌旧梦一场歌词,早知惊鸿一场何必情深一往是什么歌 《旧梦一场》歌词哪里听...
  18. 【技术贴】解决前台js传参中文乱码
  19. bim 模型web页面展示_HTML5/WebGL技术BIM模型轻量化Web浏览解决方案
  20. Mac上解决XXX.app已损坏 无法打开

热门文章

  1. 一维信号小波阈值去噪
  2. SQL Server XML 数据类型
  3. 对比学习顶会论文系列-3-2
  4. string字符串转换为Date日期格式
  5. qt Redis使用
  6. 网页设计排版中哪些元素比较重要?
  7. 【20220207】【信号处理】希尔伯特变换定义及解调原理
  8. dseo13b打开自动消失_刚安装的WIN764位系统
  9. Visa在2020年东京奥运会和残奥会前公布“Visa之队”阵容
  10. Leetcode_137_Single Number II