using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
ComboBox combo; //手动声明combox
DateTimePicker date; //手动声明DataTimePicker控件
DataSet ds=new DataSet(); //数据集
private System.Windows.Forms.DataGrid allData;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.allData = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.allData)).BeginInit();
this.SuspendLayout();
//
// allData
//
this.allData.DataMember = "";
this.allData.Dock = System.Windows.Forms.DockStyle.Top;
this.allData.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.allData.Location = new System.Drawing.Point(0, 0);
this.allData.Name = "allData";
this.allData.Size = new System.Drawing.Size(528, 224);
this.allData.TabIndex = 0;
this.allData.CurrentCellChanged += new System.EventHandler(this.allData_CurrentCellChanged);
this.allData.Leave += new System.EventHandler(this.allData_Leave);
this.allData.Scroll += new System.EventHandler(this.allData_Scroll);
//
// button1
//
this.button1.Location = new System.Drawing.Point(224, 240);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "保存";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(528, 273);
this.Controls.Add(this.button1);
this.Controls.Add(this.allData);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.allData)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
OleDbConnection con=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Application.StartupPath+"
\\Users.mdb");
OleDbDataAdapter da=new OleDbDataAdapter("select * from [User]",con);
try
{
da.Fill(ds,"User"); //填充数据集
}
catch(Exception er)
{
MessageBox.Show(er.Message);
}
combo=new ComboBox(); //实例化combox
combo.Items.Add("男"); //加入内容
combo.Items.Add("女");
combo.Visible=false; //设为不可见
combo.SelectedIndexChanged+=new EventHandler(combo_SelectedIndexChanged); //注册委托
//combo.SelectedIndex=0;
this.allData.Controls.Add(combo); //将combox加入DataGrid控件
date=new DateTimePicker(); //实例化DataTimePicker控件
date.Value=DateTime.Now; //初始化
date.ValueChanged+=new EventHandler(date_ValueChanged); //注册委托
date.Visible=false; //设为不可见
this.allData.Controls.Add(date); //加入DataGrid控件
this.allData.DataSource=ds.Tables[0]; //数据绑定
}
//combox选项改变事件
private void combo_SelectedIndexChanged(object sender, EventArgs e)
{
//注意:一定要操作DataGrid控件,而不是操作DataSet,因为可能会指到还未添加到DataSet的新行上
//以下语句容易出错:
//ds.Tables[0].Rows[allData.CurrentRowIndex][2]=combo.SelectedItem;
allData[allData.CurrentCell.RowNumber,allData.CurrentCell.ColumnNumber]=combo.SelectedItem;
}
//DataTimePicker控件的值改变事件
private void date_ValueChanged(object sender, EventArgs e)
{
//注意:一定要操作DataGrid控件,而不是操作DataSet,因为可能会指到还未添加到DataSet的新行上
//以下语句容易出错:
//ds.Tables[0].Rows[allData.CurrentRowIndex][3]=data.Value.ToShortDataString();
allData[allData.CurrentCell.RowNumber,allData.CurrentCell.ColumnNumber]=date.Value.ToShortDateString();
}
//DataGrid控件的单元格改变事件
private void allData_CurrentCellChanged(object sender, System.EventArgs e)
{
int col=allData.CurrentCell.ColumnNumber; //得到所选单元格的所在列号
//如果为2(第三列)
if(col==2)
{
if(date.Visible)
date.Visible=false;
Rectangle rect=allData.GetCellBounds(allData.CurrentCell); //得到该单元格的矩形大小
combo.Location=new Point(rect.Left,rect.Top); //设置combox出现的位置
combo.SetBounds(rect.Left,rect.Top,rect.Width,rect.Height); //设置combox的大小
combo.Visible=true; //设置为可见
//确定显示哪个值
for(int i=0;i<combo.Items.Count;i++)
{
if(combo.Items[i].ToString().Trim()==allData[allData.CurrentCell.RowNumber,allData.CurrentCell.ColumnNumber].ToString().Trim())
{
combo.SelectedIndex=i;
break;
}
}
}
//如果为3(第四列)
else if(col==3)
{
if(combo.Visible)
combo.Visible=false;
Rectangle rect=allData.GetCellBounds(allData.CurrentCell); //得到当前列的矩形
date.Location=new Point(rect.Left,rect.Top); //设置控件的位置
date.SetBounds(rect.Left,rect.Top,rect.Width,rect.Height); //设置大小
date.Visible=true; //设为可见
}
//其他列则不显示combox和DataTimePicker控件
else
{
combo.Visible=false;
date.Visible=false;
}
}
//DataGrid控件的滚动事件
private void allData_Scroll(object sender, System.EventArgs e)
{
combo.Visible=false;
date.Visible=false;
}
//保存按钮点击事件
private void button1_Click(object sender, System.EventArgs e)
{
OleDbConnection con=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Application.StartupPath+"
\\Users.mdb");
OleDbCommand cmd=new OleDbCommand(); //用于更改的Command
cmd.Connection=con;
cmd.CommandText="update [User] set id=?,name=?,sex=?,joindate=? where id=? and name=? and sex=? and joindate=?";
cmd.Parameters.Add("newid",OleDbType.Char,10,"id");
cmd.Parameters.Add("newname",OleDbType.Char,10,"name");
cmd.Parameters.Add("newsex",OleDbType.Char,2,"sex");
cmd.Parameters.Add("newdate",OleDbType.Date,0,"joindate");
OleDbParameter para=cmd.Parameters.Add("oldid",OleDbType.Char,10,"id");
para.SourceVersion=DataRowVersion.Original;
para=cmd.Parameters.Add("oldname",OleDbType.Char,10,"name");
para.SourceVersion=DataRowVersion.Original;
para=cmd.Parameters.Add("oldsex",OleDbType.Char,2,"sex");
para.SourceVersion=DataRowVersion.Original;
para=cmd.Parameters.Add("olddate",OleDbType.Date,0,"joindate");
para.SourceVersion=DataRowVersion.Original;
OleDbCommand insertcmd=new OleDbCommand(); //用于插入的Command
insertcmd.Connection=con;
insertcmd.CommandText="insert into [User] values(?,?,?,?)";
insertcmd.Parameters.Add("newid",OleDbType.Char,10,"id");
insertcmd.Parameters.Add("newname",OleDbType.Char,10,"name");
insertcmd.Parameters.Add("newsex",OleDbType.Char,2,"sex");
insertcmd.Parameters.Add("newdate",OleDbType.Date,0,"joindate");
OleDbDataAdapter da=new OleDbDataAdapter();
da.UpdateCommand=cmd;
da.InsertCommand=insertcmd;
try
{
//只更新已被修改或新添加的行
da.Update(ds.Tables[0].Select("","",DataViewRowState.ModifiedCurrent|DataViewRowState.Added));
}
catch(Exception er)
{
MessageBox.Show(er.Message);
}
MessageBox.Show("OK");
}
//DataGrid控件的失去焦点事件
private void allData_Leave(object sender, System.EventArgs e)
{
combo.Visible=false;
date.Visible=false;
}
}
}