DataGridView 单元格在输入时不会转换为 int

问题描述 投票:0回答:1

我在加载应用程序时用数字填充 DataGridView,并且我想允许用户更改表的值。

如果用户输入的内容无法转换为整数,则将其转换为 0。单元格为空? 0.

变量保持整数很重要,因为我希望排序控件按数字顺序而不是按字母顺序对其进行排序。

这是我为单元格值更改调用的函数:

private void BossDropDataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    //If the content is empty, then put 0 in it
    if (bossDropDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null ||
        bossDropDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "")
        bossDropDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = 0;

    try
    {
        bossDropDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = Convert.ToInt32(bossDropDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
    }
    catch
    {
        bossDropDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = 0;
    }
// Some silly debugging to see if it works:
    if (bossDropDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.GetType() != typeof(Int32))
        MessageBox.Show("I couldn't convert it to int tho. The type is " + bossDropDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.GetType().Name);
}

果然:

its a string...

此外,如果我尝试对表进行排序,程序就会崩溃:

System.ArgumentException
  HResult=0x80070057
  Message=Object must of type int32
  Source=mscorlib
  StackTrace:
   at System.Int32.CompareTo(Object value)
   at System.Collections.Comparer.Compare(Object a, Object b)
   at System.Windows.Forms.DataGridViewRowCollection.RowComparer.CompareObjects(Object value1, Object value2, Int32 rowIndex1, Int32 rowIndex2)
   at System.Windows.Forms.DataGridViewRowCollection.RowArrayList.Pivot(Int32 left, Int32 center, Int32 right)
   at System.Windows.Forms.DataGridViewRowCollection.RowArrayList.CustomQuickSort(Int32 left, Int32 right)
   at System.Windows.Forms.DataGridViewRowCollection.RowArrayList.CustomSort(RowComparer rowComparer)
   at System.Windows.Forms.DataGridViewRowCollection.Sort(IComparer customComparer, Boolean ascending)
   at System.Windows.Forms.DataGridView.SortInternal(IComparer comparer, DataGridViewColumn dataGridViewColumn, ListSortDirection direction)
   at System.Windows.Forms.DataGridView.Sort(DataGridViewColumn dataGridViewColumn, ListSortDirection direction)
   at System.Windows.Forms.DataGridView.OnColumnHeaderMouseClick(DataGridViewCellMouseEventArgs e)
   at System.Windows.Forms.DataGridView.OnMouseClick(MouseEventArgs e)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.DataGridView.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at Program_name_here.Main() in Path-here\Program.cs:line 19

我尝试使用 Convert.ToInt32(...) 转换变量 但变量仍然是 String 类型。 我希望变量能够转换其类型。

c# winforms datagridview
1个回答
0
投票

起初,我尝试将“Value”更改为“FormattedValue”,但这也没有解决问题。

有帮助的是添加 DataGridView_CellParsing 事件,如下所示:

private void bossDropDataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
{
    if (!int.TryParse(e.Value.ToString(), out int parsedValue))
    {
        // If parsing fails, set the value to 0
        e.Value = 0;
        e.ParsingApplied = true;
    }
    else
    {
        // If parsing succeeds, set the parsed value
        e.Value = parsedValue;
        e.ParsingApplied = true;
    }
}

我没有依赖 CellValueChanged 事件,而是使用 CellParsing 事件,该事件发生在解析单元格值并将其提交到表本身之前,这几乎解决了这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.