我的单元格值没有被截断的原因是什么?

时间:2011-05-16 12:28:47

标签: c# winforms datagridview

以下是我在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个。

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

或许:dataGridView1.DataBind()