【问题标题】:Firing The DataGridView CellValueChanged Event Immediately for doing thousand separator立即触发 DataGridView CellValueChanged 事件以执行千位分隔符
【发布时间】:2016-12-15 14:42:56
【问题描述】:

恳请您耐心阅读我的故事,解决一个看似简单的问题,

我的主要目的是在datagridview中做千位分隔符, 这意味着当我输入 12345 时,它变为 12,345 与其他任务的不同之处在于,在用户输入时执行此操作,而不是在他或她离开单元格时执行此操作,

我在 datagridview 事件中找不到任何方法,只要我在单元格中添加一个数字就会触发,例如,如果目的是在 feeColumn 中写入 12345,它必须被触发五次,

经过多次研究,我发现了这段代码要做,但它有一些问题:

myGrid:

ItemName  Quantity  FeeColumn


Private void grdTrading_CurrentCellDirtyStateChanged(object sender, System.EventArgs e) 
{
    If (grdTrading.IsCurrentCellDirty )
    {
        grdTrading.CommitEdit(DataGridViewDataErrorContexts.Commit)

    }
}

Private void grdTrading_CellValueChanged(object sender , System.Windows.FormsDataGridViewCellEventArgs e)
{
   grdTrading.Columns[ColumnFee.Name].DefaultCellStyle.Format=(Decimal.Parse(grdTrading.Rows[0].Cells[ColumnFee.Name].Value.Tostring())).Tostring("#,##0")
}

现在出了什么问题?

  1. 当它第一次转到 grdTrading_CurrentCellDirtyStateChanged 并执行 commitedit 时,它不再允许在单元格中放入任何数据并在用户更改其单元格时删除数据

  2. 现在你假设有数据,它成功运行,一旦我在费用列中添加任何数字,甚至运行cellValueChanged, 我的意思是 1 然后 12 然后 123 然后 1234 然后 12345 但是当我打字时,我的网格中仍然没有逗号,

我真的厌倦了这个问题,欢迎任何帮助或任何其他解决方案,朋友,

【问题讨论】:

  • 您似乎将列的样式格式设置为单元格的实际值,即column.DefaultCellStyle.Format = "123,456"。
  • 如果您想在输入时自动应用千位分隔符,请先尝试使用普通的TextBox。然后,如果成功,则处理DataGridView 的EditingControlShowing 事件并获取显示为编辑控件的TextBox 并将逻辑应用于它。
  • @stuartd 谢谢你的评论,是的,我就是这样做的,现在我做了一个多么愚蠢的工作,我可以请你和我分享你的想法吗?
  • @RezaAghaei thx reza 对于您的评论,看来您也是来自伊朗,您的意思是我可以使用 EditingControlShowing 做到这一点?我可以请你与我分享你的代码,因为我曾经用另一种方法这样做过,但它希望再次成功:(谢谢你的帮助
  • 我从来没有做过,但是我确定是否有办法在输入时添加千位分隔符,您应该首先尝试使用普通的TextBox 来实现它。当您输入DataGridViewTextBoxColumn 时,实际上您输入的是源自TextBox 的DataGridViewTextBoxEditingControl。因此,您应该首先尝试使用TextBox 实现逻辑(独立于DataGridView),如果成功,然后通过处理EditingControlShowing 将逻辑应用于编辑控件。所以从TextBox开始。

标签: c# winforms datagridview event-handling digit-separator


【解决方案1】:

正如 Reza Aghaei 正确指出的那样,这里的想法是处理 DataGridView.EditingControlShowing 事件。他在TextBox 控件上练习的建议是正确的,我看到您通常从您在 cmets 中的链接中获得了这个想法。你现在只需要把它全部连接起来:

this.dataGridView1.EditingControlShowing += this.DataGridView1_EditingControlShowing;

private void DataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    if (e.Control is TextBox)
    {
        TextBox tb = e.Control as TextBox;
        tb.TextChanged -= Tb_TextChanged;
        tb.TextChanged += Tb_TextChanged;
    }
}

现在您可以处理TextBox.TextChanged 事件,类似于您尝试过的in your other question. 下面我展示了我的实现,对选择光标的位置进行了一些调整相对于它之前的位置.

private void Tb_TextChanged(object sender, EventArgs e)
{
    TextBox tb = sender as TextBox;
    ulong value;
    string text = tb.Text.Replace(",", "");
    string old = tb.Text;

    if (ulong.TryParse(text, out value))
    {
        int start = tb.SelectionStart;
        tb.Text = string.Format("{0:n0}", value);
        tb.SelectionStart = Math.Max(0, start + tb.Text.Length - old.Length);

        if (tb.SelectionStart > 0 && tb.Text[tb.SelectionStart - 1] == ',')
        {
            tb.SelectionStart--;
        }
    }
}

希望这会有所帮助。


旁注:如果这让您的用户感到厌烦(完全合理,因为这不是正常的预期行为 - 在实时编辑期间更改用户输入),很容易恢复到:

private void DataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    ulong value;

    if (ulong.TryParse(e.Value?.ToString(), out value))
    {
        e.Value = string.Format("{0:n0}", value);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-20
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多