private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if ((e.ColumnIndex == CMD_INDEX) && (this.tvControllers.SelectedNode != null)) { KeyCommandPair kcp = m_keypads[this.tvControllers.SelectedNode.Text].KeyCommands.Single(r => r.Key == Convert.ToInt32(this.dataGridView1.Rows[e.RowIndex].Cells[CODE_INDEX].Value)); kcp.Command = Convert.ToString(this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value) ?? string.Empty; } }
private void UpdateSerialPortData(string cnt) { if (this.InvokeRequired) { this.BeginInvoke(new UpdateSerialPortDataHandler(UpdateSerialPortData), cnt); } else { bool isExisted = false; string keyValue = string.Empty; this.dataGridView1.ClearSelection(); string code = Convert.ToInt32(cnt.TrimEnd('\r', '\n'), 16).ToString(); foreach (DataGridViewRow dgvr in this.dataGridView1.Rows) { if (string.Equals(code, Convert.ToString(dgvr.Cells[CODE_INDEX].Value), StringComparison.OrdinalIgnoreCase)) { keyValue = Convert.ToString(dgvr.Cells[CMD_INDEX].Value); dgvr.Selected = true; isExisted = true; break; } } if (m_currentMode == KeypadMode.KeyInput) { if (!isExisted) { MessageBox.Show(string.Format("键{0}的映射命令没有定义", code)); } else { this.txtInput.Text += keyValue; this.txtInput.SelectionStart = this.txtInput.Text.Length; } } else { if (!isExisted) { this.dataGridView1.Rows.Add(false, code, string.Empty); if (this.tvControllers.SelectedNode != null) { KeyCommandPair kcp = new KeyCommandPair(); kcp.Key = Convert.ToInt32(code); m_keypads[this.tvControllers.SelectedNode.Text].KeyCommands.Add(kcp); } } } } }