我在这里触发了三个事件,它允许用户在datagridview中查看单元格的全文,因为它们实时更改单元格,并且可以编辑单元格或richTextBox的文本(如果文字字段太大了)。 我正在使用: dataGridView1_CellValueChanged_Handler - 更新SQL并刷新DGV dataGridView1_CurrentCellChanged_Handler - 更改RTB中的文本 richTextBox2_KeyDown_Handler - 更新SQL并刷新DGV
问题在于,当在RTB中编辑文本时,当事情走完整圈时,我在DGV的单元格值之前得到一条额外的行。我已经跟踪了这个并且可以告诉以下情况发生了:
尚不确定如何解决此问题,请求任何帮助或建议。
以下示例代码:
private void handler_dataGridView1_CellValueChanged(object sender,DataGridViewCellEventArgs e){
if(initialized){
string query = "";
string table = tabControl1.TabPages[(tabControl1.SelectedIndex)].Text;
string column = (string)dataGridView1.Columns[e.ColumnIndex].HeaderText;
int sqlID = (int)dataGridView1.Rows[e.RowIndex].Cells[0].Value;
if(dataGridView1.CurrentCell.ValueType.ToString() == "System.DateTime"){
DateTime valueD = Convert.ToDateTime(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
query = (@"UPDATE [" + globalDatabase + @"].[dbo].[" + table + @"] SET [" + column + @"] = '" + valueD + @"' WHERE [ID] = '" + sqlID + @"'");
}else if(dataGridView1.CurrentCell.ValueType.ToString() == "System.String"){
string valueS = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
query = (@"UPDATE [" + globalDatabase + @"].[dbo].[" + table + @"] SET [" + column + @"] = '" + valueS + @"' WHERE [ID] = '" + sqlID + @"'");
}else{
MessageBox.Show("Unhandled data type in method handler_dataGridView1_CellValueChanged.");
}
WriteSQL(query);
}else{}
}
}
private void dataGridView1_CurrentCellChanged(object sender, EventArgs e){
if(dataGridView1.CurrentCell != null && dataGridView1.CurrentCell.Value != null){
MessageBox.Show(dataGridView1.CurrentCell.Value.ToString().Trim());
MessageBox.Show(richTextBox2.Text.ToString().Trim());
richTextBox2.Clear();
richTextBox2.Text = dataGridView1.CurrentCell.Value.ToString().Trim();
}
}
private void richTextBox2_KeyDown_Handler(object sender, KeyEventArgs e){
if(e.KeyValue == 13 && dataGridView1.CurrentCell.RowIndex != -1 && richTextBox2.Text.ToString().Trim() != dataGridView1.CurrentCell.Value.ToString().Trim()){
int col = dataGridView1.CurrentCell.ColumnIndex;
int row = dataGridView1.CurrentCell.RowIndex;
string ID = dataGridView1.CurrentRow.Cells[0].Value.ToString();
string table = tabControl1.TabPages[(tabControl1.SelectedIndex)].Text;
string column = dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].HeaderText.ToString();
string value = richTextBox2.Text.ToString().Trim();
string query = @"UPDATE [" + globalDatabase + @"].[dbo].[" + table + @"] SET [" + column + @"] = '" + value + @"' WHERE [ID] = '" + ID + @"'";
WriteSQL(query);
RefreshDGV1();
dataGridView1.CurrentCell = this.dataGridView1[col, row];
}
}
起点,DGV单元处于活动状态,文本处于RTB
在RTB中更改了值,并按下了ENTER键。值将在刷新时从DGV的单元格[0,0]值变为文本,该值将更改为更新的单元格[x,y]值。
剩下的是该单元格的新值,上面有一条额外的行。
我追踪了事件,基本上就是这样:
申请开始
dataGridView1_CellValueChanged_Handler
dataGridView1_CellValueChanged_Handler
dataGridView1_CellValueChanged_Handler
选择要更改的单元格
dataGridView1_CellValueChanged_Handler
更改RTB中文本的值,按ENTER键
richTextBox2_KeyDown_Handler
dataGridView1_CellValueChanged_Handler
dataGridView1_CellValueChanged_Handler
dataGridView1_CellValueChanged_Handler
当我从单元格内部编辑单元格时(启动dataGridView1_CellValueChanged_Handler)我没有遇到这个问题。
答案 0 :(得分:2)
如果您不想使用回车键创建新行,则应取消按键(e.Handled = true; //停止处理)
private void richTextBox2_KeyDown_Handler(object sender, KeyEventArgs e){
if(e.KeyValue == 13 && dataGridView1.CurrentCell.RowIndex != -1 && richTextBox2.Text.ToString().Trim() != dataGridView1.CurrentCell.Value.ToString().Trim()){
int col = dataGridView1.CurrentCell.ColumnIndex;
int row = dataGridView1.CurrentCell.RowIndex;
string ID = dataGridView1.CurrentRow.Cells[0].Value.ToString();
string table = tabControl1.TabPages[(tabControl1.SelectedIndex)].Text;
string column = dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].HeaderText.ToString();
string value = richTextBox2.Text.ToString().Trim();
string query = @"UPDATE [" + globalDatabase + @"].[dbo].[" + table + @"] SET [" + column + @"] = '" + value + @"' WHERE [ID] = '" + ID + @"'";
WriteSQL(query);
RefreshDGV1();
dataGridView1.CurrentCell = this.dataGridView1[col, row];
e.Handled = true; // STOP THE HANDLING
}
}