我的单元格值没有被截断的任何原因

本文关键字:任何原 单元格 我的 | 更新日期: 2023-09-27 17:56:49

这是我在Winforms DataGridView中尝试做的事情:

private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex > 1)
    {
        string tmpValue = Convert.ToString(dataGridView1[e.ColumnIndex, e.RowIndex].Value);
        if (tmpValue.Length >= 3)
        {
            //At thsi point, I'm 100% sure that tmpValue has "253" as it's value.
            dataGridView1[e.ColumnIndex, e.RowIndex].Value = tmpValue.Substring(0, 2);
        }
    }
}

但是,我离开的单元格的值没有改变。

如果我在单元格中键入 258 并离开,它应该有 25。

有什么建议吗?

我的单元格值没有被截断的任何原因

您可能需要进行验证才能进行验证。 最后,添加this.Validate();

您可能还想尝试改用 CellEndEdit 事件,因为这甚至在单元格离开和验证事件之后触发。

也许: dataGridView1.DataBind()