【问题标题】:c# Datatable cells updating issuec#数据表单元格更新问题
【发布时间】:2020-04-08 07:23:52
【问题描述】:

在数据表的单元格中输入数据时出现错误。 错误代码:StackTrace: at System.Windows.Forms.BindingSource.get_Count() at System.Windows.Forms.DataGridViewCell.SetValue(Int32 rowIndex, Object value) at :line 1633 at System.Threading.Tasks.Task.Execute()

第 1633 行:TableGridview1.Rows[i].Cells[21].Value = teknikresimim;

是否应该在每次更新单元格后重新绑定?

for (int i = 0; i < TableGridview1.Rows.Count; i++)
                {
                    #region kolon_adi tanimla
                    string parcano_kolon = Convert.ToString(TableGridview1.Rows[i].Cells[1].Value);
                    string rev_kolon = Convert.ToString(TableGridview1.Rows[i].Cells[2].Value);
                    string op1_kolon = Convert.ToString(TableGridview1.Rows[i].Cells[13].Value);
                    string dwgyolu_kolon = Convert.ToString(TableGridview1.Rows[i].Cells[23].Value);
                    string pdfyolu_kolon = Convert.ToString(TableGridview1.Rows[i].Cells[24].Value);
                    string dizinler = Convert.ToString(TableGridview1.Rows[i].Cells[25].Value);
                    string sirano = Convert.ToString(TableGridview1.Rows[i].Cells[38].Value);
                    #endregion
                    string pdfim = null;
                    string teknikresimim = null;
                    string dwg_kontrol = null;
                        var taskList = new List<Task>
                        {
                            Task.Factory.StartNew(() =>
                            {
                                pdfim = PDFdir(parcano_kolon, dizinler, rev_kolon, sirano);
                                TableGridview1.Rows[i].Cells[22].Value = pdfim;
                                TableGridview1.UpdateCellValue(22, i);
                            }),
                            Task.Factory.StartNew(() =>
                            {
                                teknikresimim = TeknikDir(parcano_kolon, dizinler)[0];
                                TableGridview1.Rows[i].Cells[21].Value = teknikresimim;
                                TableGridview1.UpdateCellValue(21, i);
                            }),
                            Task.Factory.StartNew(() =>
                            {
                                dwg_kontrol = DwgBul(parcano_kolon, rev_kolon, op1_kolon, sirano)[0];
                                TableGridview1.Rows[i].Cells[20].Value = dwg_kontrol;
                                TableGridview1.UpdateCellValue(20, i);
                            })
                        };
                        Task.WaitAll(taskList.ToArray());
                }

【问题讨论】:

  • 您不能从后台线程更新 GUI 元素(或扩展的数据绑定数据)。此错误是设计使然。
  • 什么错误?你能发布一个完整的错误信息吗?
  • @Zer0 我试过了。但给出了同样的问题
  • @Sinatr; System.InvalidOperationException HResult=0x80131509 İleti=BindingSource kendisinin veri kaynağı olamaz。 DataSource ve DataMember özelliklerini BindingSource'a geri başvuran değerlere ayarlamayın。 Kaynak=System.Windows.Forms StackTrace:在 System.Windows.Forms.BindingSource.get_Count() 在 System.Windows.Forms.DataGridViewCell.SetValue(Int32 rowIndex, Object value) 在 GetBOMList.GetBOMListx.c__DisplayClass59_1. GetBOMList.cs 中的 b__3():System.Threading.Tasks.Task.Execute() 的第 1633 行

标签: c# datagridview bindingsource


【解决方案1】:

您正在尝试从不同的线程更新 UI。 您需要在后面的表单代码中创建一个方法来更新您的 datagridview,然后使用调用从您的任务中调用它:

UpdateMethodName?.Invoke(…)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-07
    • 2011-01-27
    • 1970-01-01
    • 1970-01-01
    • 2014-12-03
    • 2013-01-25
    相关资源
    最近更新 更多