《WinForm开发系列之控件篇》Item2 BindingNavigator

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;
}

 

 

posted @ 2010-01-27 11:29  Sue_娜  阅读(3908)  评论(0编辑  收藏  举报