为什么DataBinding第二次没有工作?

时间:2014-08-18 19:46:08

标签: c# excel data-binding oledb

下面的代码根据文本框文本在excel中搜索记录并将其绑定到数据网格。但如果我尝试搜索相同的值并再次绑定它,我得到错误" DataBinding在列表中找不到适合所有绑定的行。"任何想法如何重置绑定?我尝试将formattingEnables设置为true,但它没有工作。谢谢

OleDbConnection connExcel = new OleDbConnection(_econnect);
        cmdExcel.Connection = connExcel;

        connExcel.Open();
        DataTable dt;
        DataSet ds = new DataSet();
        dt = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
        OleDbDataAdapter da = new OleDbDataAdapter();
        string sheetname = "BadgeID$";
        cmdExcel.CommandText = "select ID, Name, Dept from [" + sheetname + "] where ID in (" + sss +")";
        da.SelectCommand = cmdExcel;
        da.Fill(ds);
        connExcel.Close();

       bsdata .DataSource = ds.Tables [0]; //error here
       Invoke(new MethodInvoker(() => textBox1.DataBindings.Add("Text", bsdata, "Name")));
       Invoke(new MethodInvoker(() => textBox2.DataBindings.Add("Text", bsdata, "Dept")));

1 个答案:

答案 0 :(得分:0)

为每个文本框

调用DataBindings.Clear()