C#利用DataGridView实现数据的快速输入

时间:2024-03-05 12:44:37

在做管理软件时,常常需要表格输入功能。表格输入极大地加快了数据输入,提高了工作效率,当然也提高了软件的竞争性。笔者最近用C#在做一套CRM时,成功地使用C# 2005里面的表格控件DataGridView实现了表格输入功能,现在就把具体实现与各位分享: 
1. 初始化工作 
(1) 在Vs 2005 里面新建一个C# WinForm 应用程序: DataGridViewTest 
(2) 在窗体Form1上拖一个DataGridView控件:DataGridView1 
(3) 在DataGridView1里添加两个列: 
Column1: 
类型:DataGridViewComboBoxColumn 
HeaderText:时间 
DataPropertyName:DutyTime 
Column2: 
类型:DataGridViewTextBoxColumn 
HeaderText:时间 
DataPropertyName:DutyTime 

(4)在Form1类中添加两个私有属性: 
private DataTable m_Table;//输入组合框控件的下拉数据 
private DataTable m_DataTable;//与表格绑定的DataTable,即用户输入的最终数据 

(5)在Form1类里面定义一个结构体 

public struct MyRowData 

public MyRowData(int no, string enDay, string cnDay) 

No = no; 
EnDay = enDay; 
CnDay = cnDay; 

public int No; 
public string EnDay; 
public string CnDay; 
}

(6) 在Form1的load事件Form1_Load(object sender, EventArgs e) 加上以下初始化代码: 


this.dataGridView1.AllowUserToAddRows = true; 
this.dataGridView1.AllowUserToDeleteRows = true; 
this.dataGridView1.AutoGenerateColumns = false; 
DataColumn dc1, dc2, dc3 
dc1 = new DataColumn("No", typeof(int)); 
dc2 = new DataColumn("EnDay", typeof(string)); 
dc3 = new DataColumn("CnDay", typeof(string)); 
m_Table.Columns.Clear(); 
m_Table.Columns.AddRange(new DataColumn[] { dc1, dc2, dc3 }); 
MyRowData[] Datas = new MyRowData[7]{ 
new MyRowData( 1,"Monday","星期一"), 
new MyRowData( 2,"Tuesday","星期二"), 
new MyRowData( 3,"Wednesday","星期三" ), 
new MyRowData( 4,"Thursday","星期四" ), 
new MyRowData( 5, "Friday","星期五"), 
new MyRowData( 6,"Saturday","星期六"), 
new MyRowData( 7,"Sunday","星期日") 
}; 
this.m_Table.Rows.Clear(); 
foreach (MyRowData iData in Datas) 

this.m_Table.Rows.Add(iData.No, iData.EnDay, iData.CnDay); 


    
DataColumn dataDc1, dataDc2; 

dataDc1 = new DataColumn("DutyTime", typeof(int)); 
dataDc2 = new DataColumn("DutyPerson", typeof(string)); 
m_DataTable.Columns.Clear(); 
m_DataTable.Columns.AddRange(new DataColumn[] { dataDc1, dataDc2}); 
m_DataTable.Rows.Clear(); 

Column1.DataSource = this.m_Table; 
Column1.DisplayMember = "CnDay"; 
Column1.ValueMember = "No"; 

this.dataGridView1.DataSource = m_DataTable; 


2.消息处理(核心) 
protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData) //激活回车键 

int WM_KEYDOWN = 256; 
int WM_SYSKEYDOWN = 260; 

bool IsDataGridView1 = false; 
if (this.ActiveControl == this.dataGridView1) 

IsDataGridView1 = true; 

else 

if (this.ActiveControl is IDataGridViewEditingControl) 

if( (this.ActiveControl as IDataGridViewEditingControl).EditingControlDataGridView == this.dataGridView1) 

IsDataGridView1 = true; 




if (IsDataGridView1) //是否处于DataGridView1 上 

if (msg.Msg == WM_KEYDOWN ¦ msg.Msg == WM_SYSKEYDOWN) 

switch (keyData) 

case Keys.Delete: 
if (this.dataGridView1.CurrentCell != null 
&& this.dataGridView1.CurrentCell.RowIndex < this.m_DataTable.Rows.Count) 

this.m_DataTable.Rows[this.dataGridView1.CurrentCell.RowIndex].Delete(); 
this.m_DataTable.AcceptChanges(); 

break; 
case Keys.Enter: 
if (this.dataGridView1.CurrentCell is DataGridViewComboBoxCell 
¦ ¦ this.dataGridView1.CurrentCell is DataGridViewTextBoxCell 
)//处于输入格 

// SendKeys.Send("{F4}"); 
if (this.ActiveControl is System.Windows.Forms.DataGridViewComboBoxEditingControl 
¦ ¦ this.ActiveControl is System.Windows.Forms.DataGridViewTextBoxEditingControl 


SendKeys.Send("{Tab}"); 

else//非输入状态,将转入输入状态 

if (this.dataGridView1.CurrentCell is DataGridViewTextBoxCell)//快捷按键F2,进入编辑状态 

SendKeys.Send("{F2}"); 

else //combobox,快捷按键F4,弹出下拉框 

SendKeys.Send("{F4}"); 




else//处于非输入格 

SendKeys.Send("{Tab}"); 

return true; 
break; 



return false;