我有一个带有数据网格视图控件的winform。有一次,我可以使用向上和向下箭头导航网格视图中的行,并按下表单捕获的用户键,以允许管理员输入代码进入管理面板。
现在,由于某些原因,datagridview将不会注册按键,并且form_KeyDown事件不会在任何键被按下时触发。
网格视图的任何键事件都不会被重写,并且窗体的KeyPreview属性设置为true。
要让数据网格视图注册关键字,我还需要检查或更改哪些内容?
编辑我想不出任何相关的实际代码。这是处理按键操作的唯一事件。但是这个事件并没有触发。
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frm1_KeyDown);
private void frm1_KeyDown(object sender, KeyEventArgs e)
{
//omitted
}正如我所说的,除了这个,没有处理按键的代码,它也不会触发。
以下是keydown事件代码
下面是为数据网格视图生成的代码
//
// dgvMachines
//
this.dgvMachines.AllowUserToAddRows = false;
this.dgvMachines.AllowUserToDeleteRows = false;
this.dgvMachines.AllowUserToOrderColumns = true;
dataGridViewCellStyle1.BackColor = System.Drawing.Color.LightGray;
this.dgvMachines.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1;
this.dgvMachines.AutoGenerateColumns = false;
this.dgvMachines.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
this.dgvMachines.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.DisplayedCells;
dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle2.BackColor = System.Drawing.SystemColors.Control;
dataGridViewCellStyle2.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
dataGridViewCellStyle2.ForeColor = System.Drawing.SystemColors.WindowText;
dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
this.dgvMachines.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle2;
this.dgvMachines.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dgvMachines.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.clmDomain,
this.clmMachineName,
this.ClmMachineDescription,
this.clmRDPName,
this.clmRDPPassword,
this.ClmUserName,
this.ClmLoginTime,
this.clmStatus,
this.clmIPAddress,
this.clmID,
this.clmSourceHostName});
this.dgvMachines.DataSource = this.bsMachineDGRow;
dataGridViewCellStyle8.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle8.BackColor = System.Drawing.SystemColors.Window;
dataGridViewCellStyle8.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
dataGridViewCellStyle8.ForeColor = System.Drawing.SystemColors.ControlText;
dataGridViewCellStyle8.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle8.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle8.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
this.dgvMachines.DefaultCellStyle = dataGridViewCellStyle8;
this.dgvMachines.EditMode = System.Windows.Forms.DataGridViewEditMode.EditOnF2;
this.dgvMachines.Location = new System.Drawing.Point(24, 65);
this.dgvMachines.MultiSelect = false;
this.dgvMachines.Name = "dgvMachines";
dataGridViewCellStyle9.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle9.BackColor = System.Drawing.SystemColors.Control;
dataGridViewCellStyle9.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
dataGridViewCellStyle9.ForeColor = System.Drawing.SystemColors.WindowText;
dataGridViewCellStyle9.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle9.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle9.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
this.dgvMachines.RowHeadersDefaultCellStyle = dataGridViewCellStyle9;
this.dgvMachines.RowHeadersVisible = false;
this.dgvMachines.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
this.dgvMachines.Size = new System.Drawing.Size(753, 510);
this.dgvMachines.TabIndex = 28;
this.dgvMachines.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvMachines_CellClick);
this.dgvMachines.CellDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvMachines_CellDoubleClick);
this.dgvMachines.RowEnter += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvMachines_RowEnter);
this.dgvMachines.MouseDown += new System.Windows.Forms.MouseEventHandler(this.dgvMachines_MouseDown);发布于 2014-01-28 07:06:49
尝试将表单中的KeyPreview属性更改为false。datagridview本身能够处理箭头键。
另一个可能导致问题的属性是SelectionMode:如果设置为FullRowSelect,则不能在单元格之间导航。
如果您需要用户输入代码,请提供一个textBox以在此处输入代码,然后执行取消搜索
https://stackoverflow.com/questions/21392697
复制相似问题