求ado.net 2.0的一个实例
求bindingNavigator和textbox绑定的例子,具有增删编辑的功能,谢谢了
是不是用this.BindingContext[pubsDataSet, "authors"].AddNew();
这个好像是更新不能增加数据呀
问题点数:40、回复次数:6Top
1 楼net_lover(【孟子E章】)回复于 2006-03-04 10:46:53 得分 0
我看了一下
BindingNavigator 类 属于System.Windows.Forms空间下的,webform可能无法使用
Top
2 楼longxin123(龙的心)回复于 2006-03-04 12:31:08 得分 0
那Forms怎么用呀Top
3 楼longxin123(龙的心)回复于 2006-03-04 19:02:28 得分 0
世界第一顶Top
4 楼ghx88(辉)回复于 2006-03-04 20:32:33 得分 0
101个微软提供的Visual Studio 2005示例
http://www.pconline.com.cn/pcedu/empolder/net/0602/762469.htmlTop
5 楼longxin123(龙的心)回复于 2006-03-05 11:06:29 得分 0
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 UsingBindingNavigator
{
public partial class Form1 : Form
{
private BindingSource EmployeesBindingSource;
private System.Windows.Forms.BindingNavigator BindingNavigatorStandard;
private System.Windows.Forms.BindingNavigator BindingNavigatorCustom;
public Form1()
{
InitializeComponent();
// Create a BindingSource for all the BindingNavigators to use
this.EmployeesBindingSource = new BindingSource();
// The first BindingNavigator (Toolbox) was generated at design time
// by dragging the control from the Toolbox
SetupTab2();
SetupTab3();
}
private void Form1_Load(object sender, EventArgs e)
{
DataSet ds = RetrieveDataSet();
if (ds != null)
{
// Associate the DataSet with the BindingSource.
this.EmployeesBindingSource.DataMember = "Employee";
this.EmployeesBindingSource.DataSource = ds;
// Associate the BindingNavigators with the BindingSource
this.EmployeesBindingNavigator.BindingSource = this.EmployeesBindingSource;
this.BindingNavigatorStandard.BindingSource = this.EmployeesBindingSource;
this.BindingNavigatorCustom.BindingSource = this.EmployeesBindingSource;
// Bind the form controls
this.employeeIDTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.EmployeesBindingSource, "EmployeeID", true));
this.titleTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.EmployeesBindingSource, "Title", true));
this.emergencyContactIDTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.EmployeesBindingSource, "EmergencyContactID", true));
this.birthDateDateTimePicker.DataBindings.Add(new System.Windows.Forms.Binding("Value", this.EmployeesBindingSource, "BirthDate", true));
this.maritalStatusTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.EmployeesBindingSource, "MaritalStatus", true));
this.genderTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.EmployeesBindingSource, "Gender", true));
this.hireDateDateTimePicker.DataBindings.Add(new System.Windows.Forms.Binding("Value", this.EmployeesBindingSource, "HireDate", true));
this.salariedFlagCheckBox.DataBindings.Add(new System.Windows.Forms.Binding("CheckState", this.EmployeesBindingSource, "SalariedFlag", true));
this.baseRateTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.EmployeesBindingSource, "BaseRate", true));
this.payFrequencyTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.EmployeesBindingSource, "PayFrequency", true));
this.vacationHoursTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.EmployeesBindingSource, "VacationHours", true));
this.sickLeaveHoursTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.EmployeesBindingSource, "SickLeaveHours", true));
this.currentFlagCheckBox.DataBindings.Add(new System.Windows.Forms.Binding("CheckState", this.EmployeesBindingSource, "CurrentFlag", true));
}
}
public void SetupTab2()
{
// Generate the second BindingNavigator (Standard UI)
// Constructor parameter addStandardItems = true,
// meaning give the control the "standard" VCR type UI
this.BindingNavigatorStandard = new BindingNavigator(true);
// Place navigator on 2nd tab
this.tabPageStandard.Controls.Add(this.BindingNavigatorStandard);
this.BindingNavigatorStandard.Dock = DockStyle.Fill;
}
public void SetupTab3()
{
// Generate the third BindingNavigator (Custom UI)
// Constructor parameter addStandardItems = false,
// for constructing a custom UI
this.BindingNavigatorCustom = new BindingNavigator(false);
// Build the custom UI
// Generate buttons
ToolStripButton firstButton = new ToolStripButton("|<");
ToolStripButton prevButton = new ToolStripButton("<<");
ToolStripButton nextButton = new ToolStripButton(">>");
ToolStripButton lastButton = new ToolStripButton(">|");
ToolStripSeparator separator1 = new System.Windows.Forms.ToolStripSeparator();
ToolStripTextBox positionItem = new System.Windows.Forms.ToolStripTextBox();
ToolStripLabel countItem = new System.Windows.Forms.ToolStripLabel();
ToolStripSeparator separator2 = new System.Windows.Forms.ToolStripSeparator();
ToolStripButton addNewItem = new System.Windows.Forms.ToolStripButton("Add");
ToolStripButton deleteItem = new System.Windows.Forms.ToolStripButton("Delete");
positionItem.Text = "0";
positionItem.ToolTipText = "Current Index";
// Add buttons to the BindingNavigatorCustom, which is a toolstrip
// The order in which the buttons are added is
// the order in which buttons are displayed.
this.BindingNavigatorCustom.Items.AddRange( new ToolStripItem[]
{firstButton, prevButton, nextButton, lastButton,
separator1,
positionItem, countItem,
separator2,
addNewItem, deleteItem});
// Hook up controls to navigator functionality
this.BindingNavigatorCustom.MoveFirstItem = firstButton;
this.BindingNavigatorCustom.MoveLastItem = lastButton;
this.BindingNavigatorCustom.MoveNextItem = nextButton;
this.BindingNavigatorCustom.MovePreviousItem = prevButton;
this.BindingNavigatorCustom.PositionItem = positionItem;
this.BindingNavigatorCustom.CountItem = countItem;
this.BindingNavigatorCustom.CountItemFormat = "of {0}";
this.BindingNavigatorCustom.AddNewItem = addNewItem;
this.BindingNavigatorCustom.DeleteItem = deleteItem;
Top
6 楼longxin123(龙的心)回复于 2006-03-05 11:06:47 得分 0
// Place navigator on 3rd tab
this.tabPageCustom.Controls.Add(this.BindingNavigatorCustom);
this.BindingNavigatorCustom.Dock = DockStyle.Fill;
}
public DataSet RetrieveDataSet()
{
try
{
// Retrieve Employee data from database into a DataSet
// Build a connnection string to the database
SqlConnectionStringBuilder connectStringBuilder = new SqlConnectionStringBuilder();
connectStringBuilder.DataSource = @".\SQLEXPRESS";
connectStringBuilder.AttachDBFilename = @"C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AdventureWorks_Data.mdf";
connectStringBuilder.IntegratedSecurity = true;
connectStringBuilder.UserInstance = true;
// Prepare a DataSet to receive the Employee data
DataSet ds = new DataSet("Employees");
// Open connection to the AdventureWorks database
using (SqlConnection connection = new SqlConnection(connectStringBuilder.ConnectionString))
{
connection.Open();
// Retrieve Employee data
SqlCommand command = new SqlCommand(
"SELECT TOP 100 * FROM [HumanResources].[Employee]", connection);
using (SqlDataReader drEmployees = command.ExecuteReader())
{
ds.Load(
drEmployees,
LoadOption.OverwriteChanges,
new string[] { "Employee" });
}
// Close the connection to the database
connection.Close();
}
return ds;
}
catch (SqlException err)
{
MessageBox.Show(err.Message, "SQL Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}
}
}
------------------------------------------------------------------------------------
using (SqlConnection connection = new SqlConnection(connectStringBuilder.ConnectionString))
{
connection.Open();
// Retrieve Employee data
SqlCommand command = new SqlCommand(
"SELECT TOP 100 * FROM [HumanResources].[Employee]", connection);
using (SqlDataReader drEmployees = command.ExecuteReader())
{
ds.Load(
drEmployees,
LoadOption.OverwriteChanges,
new string[] { "Employee" });
}
是什么意思 ???为什么用using
Top




