【问题标题】:richTextBox gets "extra line" on dataGridView CurrentCellChanged EventrichTextBox 在 dataGridView CurrentCellChanged 事件上获得“额外的行”
【发布时间】:2018-04-26 17:55:17
【问题描述】:

我在这里触发了三个事件,它允许用户在 datagridview 中查看单元格的全文,因为他们实时更改单元格,并且可以编辑单元格或 RichTextBox 的文本(如果文本字段太大)。 我正在使用: dataGridView1_CellValueChanged_Handler - 更新 SQL 并刷新 DGV dataGridView1_CurrentCellChanged_Handler - 更改 RTB 中的文本 richTextBox2_KeyDown_Handler - 更新 SQL 并刷新 DGV

问题是,在 RTB 中编辑文本时,当事情转一圈时,我会在 DGV 的单元格值之前多出一行。我已经追踪到这一点,可以看出正在发生以下情况:

  1. 在 DGV 中输入要更改的单元格,RTB 更新为值
  2. 值在 RTB 中更改,然后按 ENTER 提交更改
  3. DGV CurrentCell [0,0] 因为它已刷新,我们有一个新的 DGV
  4. 该值填充到 RTB 中
  5. 以编程方式,DGV 当前单元格恢复到更改之前的状态
  6. RTB 被清除和更新,它具有 [0,0] 值,而不是 新的 [x,y] 值,它在新值前面有一个空格。

目前还不确定如何解决此问题,请提供任何帮助或建议。

示例代码如下:


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),我没有遇到这个问题。

【问题讨论】:

  • 注意:这一行只在代码的任何地方出现一次。 richTextBox2.Text = dataGridView1.CurrentCell.Value.ToString().Trim(); ...并且总是以:richTextBox2.Clear(); 开头
  • 是否可以包含一个最小、完整和可验证的示例?
  • 在描述中添加了截图。由于数据的敏感性,我不得不省略很多细节。

标签: c# windows forms


【解决方案1】:

如果你不希望回车键创建一个新行,那么你应该取消按键(e.Handled = true; // STOP THE HANDLING)

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
    }
}

【讨论】:

  • 赢家,鸡肉晚餐。非常感谢,先生,我非常感谢您的帮助。这让我发疯了。
猜你喜欢
  • 2011-08-10
  • 2015-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-07
  • 2021-12-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多