创建两个fpspread对象,并给两个对象引用DataTable, fpSPread1.DataSource = dt1,fpSpread2.DataSource = dt2,
dt1有行内容,dt2无行内容,通过按钮事件触发后台线程,
后台线程负责删除dt1的指定几行,并将删除的几行增加给dt2
不过,程序只要按照这个逻辑走,就容易导致fpSpread对象报错,显示大红叉。
请问,一个窗口内两个fpSpread对象,不能同时对它们的DataSource进行操作吗?
下面是我的代码,为方便查看已精简过。
- ///窗口加载
- void Form1_Load(object sender, EventArgs e)
- {
- _dtContent = new System.Data.DataTable();
- _dtContent.Columns.Add("列A", typeof(string));
- _dtContent.Columns.Add("列B", typeof(string));
- _dtContent.Rows.Add("名称1","型号1");
- _dtContent.Rows.Add("名称2","型号2");
- _dtContent.Rows.Add("名称3","型号3");
- _dtContent.Rows.Add("名称4","型号4");
- _dtContent.Rows.Add("名称5","型号5");
- _dtContent.Rows.Add("名称6","型号6");
- _fpSpreadContent.DataSource = _dtContent;//fpspread对象1 DataSource
- _dtHistory = new System.Data.DataTable();
- _dtHistory.Columns.Add("列A", typeof(string));
- _dtHistory.Columns.Add("列B", typeof(string));
- _fpSpreadHistory.DataSource = _dtHistory;//fpspread对象2 DataSource
- }
复制代码
- ///按钮1单击
- private void _Button1_Click(object sender, KeyPressEventArgs e)
- {
- pictureBox.Visible = true
- backgroundWorker.RunWorkerAsync();//异步运行后台线程
- }
复制代码
- ///后台线程开始
- private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
- {
- removeAt = new List<int>();
- removeAt.Add(0);
- removeAt.Add(2);
- removeAt.Add(4);
- for (int count = 0; count < removeAt.Count; count++) { _dtHistory.ImportRow(_dtContent.Rows[removeAt[count]]); }//增加指定列
- for (int count = 0; count < removeAt.Count; count++) { _dtContent.Rows.RemoveAt(removeAt[count]); } //移除指定列
- }
复制代码
- ///后台线程结束
- private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
- {
- pictureBox.Visible = false;
- }
复制代码
附报错截图
fpSpread报错.jpg
(38.01 KB, 下载次数: 529)
|
|