c# 数据表单元格更新问题

c# Datatable cells updating issue

我在数据 table 的单元格中输入数据时出错。 错误代码: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());
                }

您正在尝试从另一个线程更新 UI。 您需要在后面的表单代码中创建一个方法来更新您的数据网格视图,然后使用调用从您的任务中调用它:

UpdateMethodName?.Invoke(…)