// 更新数据 private void dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (SubFunction == null) { return; } int row = e.RowIndex; int column = e.ColumnIndex; var cell = dataGridView[column, row]; if (cell.ReadOnly) { return; } // 不需要解码 if (!needSendParsing(SubFunction)) { // 只有用户编辑的单元格才会同步到子功能 var cellValue = cell.Value; string value = "00"; if (cellValue != null) { value = cellValue.ToString(); } else { dataGridView[column, row].Value = value; } SubFunction.Data[row - 1] = Utils.HexToByte(value); //更新第一行 UpdateFirstRow(); } else //需要进行解码 { // 获取解码值 VdfSignal signal = cell.Tag as VdfSignal; //Debug.Assert(signal != null); // 更新值 string cellValue = string.Empty; if (cell.Value != null) { cellValue = cell.Value.ToString(); } VdfDecoder.Decode(SubFunction.Data, signal, cellValue); UpdateFirstRow(); } }
// 进行校验 private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { var cell = dataGridView[e.ColumnIndex, e.RowIndex]; if (cell.ReadOnly) { return; } if (!needSendParsing(SubFunction)) { //保证输入的数值,是16进制值 try { Convert.ToInt32(e.FormattedValue.ToString(), 16); } catch (Exception) { MessageBox.Show("输入的字符必须是Hex字符", "校验出错", MessageBoxButtons.OK, MessageBoxIcon.Error); e.Cancel = true; } } else { VdfSignal signal = cell.Tag as VdfSignal; //signal居然可能为空 // 这儿只是进行验证,不更新数据 try { var copy = new List <byte>(); copy.AddRange(SubFunction.Data); VdfDecoder.Decode(copy, signal, e.FormattedValue.ToString()); } catch (Exception ex) { MessageBox.Show(ex.Message, "校验出错", MessageBoxButtons.OK, MessageBoxIcon.Error); e.Cancel = true; } } }