📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.IO;
namespace TestDataGrid
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private SqlDataAdapter adapter;
private DataSet dataset;
private DataView dataview;
private string tableName="personal";
private string connString= "server=localhost;Integrated Security=SSPI;database=webdevelop";
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button buttonAppend;
private System.Windows.Forms.Button buttonCancel;
private System.Windows.Forms.Button buttonDelete;
private System.Windows.Forms.Button buttonSave;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.DataGridTableStyle dataGridTableStyle1;
private System.Windows.Forms.Button buttonFromPhoto;
private System.Windows.Forms.Button buttonPrint;
private System.Windows.Forms.Button buttonClearPhoto;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn2;
private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn3;
private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn4;
private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn5;
private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
Cursor.Current=Cursors.WaitCursor;
FillComboBox(ref this.comboBox1,"department");
//设置默认选项
if(this.comboBox1.Items.Count>0)
{
this.comboBox1.SelectedIndex=0;
}
//取部门号
string str=(this.comboBox1.SelectedIndex==-1)
?"-1":this.comboBox1.SelectedItem.ToString().Substring(1,2);
//创建SqlDataAdapter对象并生成相应SQL语句
BuildAdapter("select * from "+tableName);
dataset=new DataSet();
//将数据表填充到dataset中
adapter.Fill(dataset,tableName);
//创建惟一性约束,防止有重复的人员编号
UniqueConstraint constraint = new UniqueConstraint(
new DataColumn[]{dataset.Tables[0].Columns[0]});
dataset.Tables[0].Constraints.Add(constraint);
//创建视图
dataview=new DataView(dataset.Tables[0]);
//不允许用户直接添加新行(不在最后一行显示添加新行标志)
dataview.AllowNew=false;
//设置过滤条件
dataview.RowFilter="substring(编号,1,2)='"+str+"'";
//不允许单击列表头排序
this.dataGrid1.AllowSorting=false;
//绑定数据源
this.dataGrid1.SetDataBinding(dataview,"");
//鼠标箭头恢复默认值
Cursor.Current=Cursors.Default;
}
/// <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.dataGrid1 = new System.Windows.Forms.DataGrid();
this.dataGridTableStyle1 = new System.Windows.Forms.DataGridTableStyle();
this.dataGridTextBoxColumn1 = new System.Windows.Forms.DataGridTextBoxColumn();
this.dataGridTextBoxColumn2 = new System.Windows.Forms.DataGridTextBoxColumn();
this.dataGridTextBoxColumn3 = new System.Windows.Forms.DataGridTextBoxColumn();
this.dataGridTextBoxColumn4 = new System.Windows.Forms.DataGridTextBoxColumn();
this.dataGridTextBoxColumn5 = new System.Windows.Forms.DataGridTextBoxColumn();
this.buttonAppend = new System.Windows.Forms.Button();
this.buttonCancel = new System.Windows.Forms.Button();
this.buttonDelete = new System.Windows.Forms.Button();
this.buttonSave = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.label1 = new System.Windows.Forms.Label();
this.buttonFromPhoto = new System.Windows.Forms.Button();
this.buttonPrint = new System.Windows.Forms.Button();
this.buttonClearPhoto = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = "personal";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(16, 40);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(376, 312);
this.dataGrid1.TabIndex = 0;
this.dataGrid1.TableStyles.AddRange(new System.Windows.Forms.DataGridTableStyle[] {
this.dataGridTableStyle1});
this.dataGrid1.Click += new System.EventHandler(this.dataGrid1_Click);
this.dataGrid1.CurrentCellChanged += new System.EventHandler(this.dataGrid1_CurrentCellChanged);
//
// dataGridTableStyle1
//
this.dataGridTableStyle1.DataGrid = this.dataGrid1;
this.dataGridTableStyle1.GridColumnStyles.AddRange(new System.Windows.Forms.DataGridColumnStyle[] {
this.dataGridTextBoxColumn1,
this.dataGridTextBoxColumn2,
this.dataGridTextBoxColumn3,
this.dataGridTextBoxColumn4,
this.dataGridTextBoxColumn5});
this.dataGridTableStyle1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGridTableStyle1.MappingName = "personal";
//
// dataGridTextBoxColumn1
//
this.dataGridTextBoxColumn1.Format = "";
this.dataGridTextBoxColumn1.FormatInfo = null;
this.dataGridTextBoxColumn1.HeaderText = "编号";
this.dataGridTextBoxColumn1.MappingName = "编号";
this.dataGridTextBoxColumn1.NullText = "";
this.dataGridTextBoxColumn1.Width = 75;
//
// dataGridTextBoxColumn2
//
this.dataGridTextBoxColumn2.Format = "";
this.dataGridTextBoxColumn2.FormatInfo = null;
this.dataGridTextBoxColumn2.HeaderText = "姓名";
this.dataGridTextBoxColumn2.MappingName = "姓名";
this.dataGridTextBoxColumn2.NullText = "";
this.dataGridTextBoxColumn2.Width = 75;
//
// dataGridTextBoxColumn3
//
this.dataGridTextBoxColumn3.Format = "";
this.dataGridTextBoxColumn3.FormatInfo = null;
this.dataGridTextBoxColumn3.HeaderText = "性别";
this.dataGridTextBoxColumn3.MappingName = "性别";
this.dataGridTextBoxColumn3.NullText = "";
this.dataGridTextBoxColumn3.Width = 40;
//
// dataGridTextBoxColumn4
//
this.dataGridTextBoxColumn4.Format = "";
this.dataGridTextBoxColumn4.FormatInfo = null;
this.dataGridTextBoxColumn4.HeaderText = "工资";
this.dataGridTextBoxColumn4.MappingName = "工资";
this.dataGridTextBoxColumn4.NullText = "";
this.dataGridTextBoxColumn4.Width = 40;
//
// dataGridTextBoxColumn5
//
this.dataGridTextBoxColumn5.Format = "";
this.dataGridTextBoxColumn5.FormatInfo = null;
this.dataGridTextBoxColumn5.HeaderText = "出生日期";
this.dataGridTextBoxColumn5.MappingName = "出生日期";
this.dataGridTextBoxColumn5.NullText = "";
this.dataGridTextBoxColumn5.Width = 75;
//
// buttonAppend
//
this.buttonAppend.Anchor = System.Windows.Forms.AnchorStyles.None;
this.buttonAppend.Location = new System.Drawing.Point(400, 280);
this.buttonAppend.Name = "buttonAppend";
this.buttonAppend.Size = new System.Drawing.Size(71, 23);
this.buttonAppend.TabIndex = 9;
this.buttonAppend.Text = "添加";
this.buttonAppend.Click += new System.EventHandler(this.buttonAppend_Click);
//
// buttonCancel
//
this.buttonCancel.Anchor = System.Windows.Forms.AnchorStyles.None;
this.buttonCancel.Location = new System.Drawing.Point(488, 320);
this.buttonCancel.Name = "buttonCancel";
this.buttonCancel.Size = new System.Drawing.Size(71, 23);
this.buttonCancel.TabIndex = 10;
this.buttonCancel.Text = "放弃";
this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click);
//
// buttonDelete
//
this.buttonDelete.Anchor = System.Windows.Forms.AnchorStyles.None;
this.buttonDelete.Location = new System.Drawing.Point(488, 280);
this.buttonDelete.Name = "buttonDelete";
this.buttonDelete.Size = new System.Drawing.Size(71, 23);
this.buttonDelete.TabIndex = 7;
this.buttonDelete.Text = "删除";
this.buttonDelete.Click += new System.EventHandler(this.buttonDelete_Click);
//
// buttonSave
//
this.buttonSave.Anchor = System.Windows.Forms.AnchorStyles.None;
this.buttonSave.Location = new System.Drawing.Point(400, 320);
this.buttonSave.Name = "buttonSave";
this.buttonSave.Size = new System.Drawing.Size(71, 23);
this.buttonSave.TabIndex = 8;
this.buttonSave.Text = "保存";
this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click);
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(16, 16);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(112, 144);
this.pictureBox1.TabIndex = 12;
this.pictureBox1.TabStop = false;
//
// groupBox1
//
this.groupBox1.Controls.Add(this.pictureBox1);
this.groupBox1.Location = new System.Drawing.Point(416, 48);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(136, 168);
this.groupBox1.TabIndex = 13;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "照片";
//
// comboBox1
//
this.comboBox1.Location = new System.Drawing.Point(56, 8);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 20);
this.comboBox1.TabIndex = 14;
this.comboBox1.Text = "comboBox1";
this.comboBox1.SelectionChangeCommitted += new System.EventHandler(this.comboBox1_SelectionChangeCommitted);
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(32, 23);
this.label1.TabIndex = 15;
this.label1.Text = "部门";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// buttonFromPhoto
//
this.buttonFromPhoto.Location = new System.Drawing.Point(256, 8);
this.buttonFromPhoto.Name = "buttonFromPhoto";
this.buttonFromPhoto.TabIndex = 16;
this.buttonFromPhoto.Text = "导入照片";
this.buttonFromPhoto.Click += new System.EventHandler(this.buttonFromPhoto_Click);
//
// buttonPrint
//
this.buttonPrint.Location = new System.Drawing.Point(472, 8);
this.buttonPrint.Name = "buttonPrint";
this.buttonPrint.TabIndex = 17;
this.buttonPrint.Text = "打印预览";
this.buttonPrint.Click += new System.EventHandler(this.buttonPrint_Click);
//
// buttonClearPhoto
//
this.buttonClearPhoto.Location = new System.Drawing.Point(368, 8);
this.buttonClearPhoto.Name = "buttonClearPhoto";
this.buttonClearPhoto.TabIndex = 16;
this.buttonClearPhoto.Text = "移除照片";
this.buttonClearPhoto.Click += new System.EventHandler(this.buttonClearPhoto_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(0, 0);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -