DataGridTableStyle ts = new DataGridTableStyle();
ts.MappingName = ds.Tables[0].TableName;DataGridTextBoxColumn tbc1 = new DataGridTextBoxColumn();
tbc1.MappingName = "营业员编号";
tbc1.HeaderText = "1";
tbc1.TextBox.KeyDown += new KeyEventHandler(this.Column_KeyDown);
ts.GridColumnStyles.Add(tbc1);DataGridTextBoxColumn tbc2 = new DataGridTextBoxColumn();
tbc2.MappingName = "营业员姓名";
tbc2.HeaderText = "2";
tbc2.TextBox.KeyDown += new KeyEventHandler(this.Column_KeyDown);
ts.GridColumnStyles.Add(tbc2); dataGrid1.TableStyles.Add(ts);dataGrid1.DataSource = ds.Tables[0].DefaultView;
-----------------------------------------
private void Column_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
this.SalesPeople = dataGrid1[dataGrid1.CurrentCell.RowNumber,0].ToString();
this.DialogResult = DialogResult.OK;
}
}
ts.MappingName = ds.Tables[0].TableName;DataGridTextBoxColumn tbc1 = new DataGridTextBoxColumn();
tbc1.MappingName = "营业员编号";
tbc1.HeaderText = "1";
tbc1.TextBox.KeyDown += new KeyEventHandler(this.Column_KeyDown);
ts.GridColumnStyles.Add(tbc1);DataGridTextBoxColumn tbc2 = new DataGridTextBoxColumn();
tbc2.MappingName = "营业员姓名";
tbc2.HeaderText = "2";
tbc2.TextBox.KeyDown += new KeyEventHandler(this.Column_KeyDown);
ts.GridColumnStyles.Add(tbc2); dataGrid1.TableStyles.Add(ts);dataGrid1.DataSource = ds.Tables[0].DefaultView;
-----------------------------------------
private void Column_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
this.SalesPeople = dataGrid1[dataGrid1.CurrentCell.RowNumber,0].ToString();
this.DialogResult = DialogResult.OK;
}
}
如果窗体将接收所有键事件,则为 true;如果窗体上当前选定控件接收键事件,则为 false。默认值为 false。
{
// TODO: 添加 UserControl1.ProcessCmdKey 实现
int WM_KEYDOWN = 256;
int WM_SYSKEYDOWN = 260;
if((msg.Msg==WM_KEYDOWN)||(msg.Msg==WM_SYSKEYDOWN))
{
switch(keyData)
{
case Keys.Enter:
SendKeys.Send("{E}");
return true;
}
}
return base.ProcessCmdKey (ref msg, keyData);
}
}
----------------------------------
private void Column_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode == Keys.E)
{
this.SalesPeople = dataGrid1[dataGrid1.CurrentCell.RowNumber,0].ToString();
this.DialogResult = DialogResult.OK;
}
}