添加行提示:
其他信息: 当控件被数据绑定时,无法以编程方式向 DataGridView 的行集合中添加行
网上有找了一些资料,未解决,谢谢导入函数如下 OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Excel文件";
            ofd.FileName = "";
            ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);//为了获取特定的系统文件夹,可以使用System.Environment类的静态方法GetFolderPath()。该方法接受一个Environment.SpecialFolder枚举,其中可以定义要返回路径的哪个系统目录
            ofd.Filter = "Excel文件(*.xls)|*.xls";
            ofd.ValidateNames = true;     //文件有效性验证ValidateNames,验证用户输入是否是一个有效的Windows文件名
            ofd.CheckFileExists = true;  //验证路径有效性
            ofd.CheckPathExists = true; //验证文件有效性
            string strName = string.Empty;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                strName = ofd.FileName;
            }
            else
            {
                return;
            }            if (strName == "")
            {
                MessageBox.Show("没有选择Excel文件!无法进行数据导入");
                return;
            }            //打开            string ConnectionExcel = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + strName + ";Extended Properties ='Excel 8.0;HDR=NO;IMEX=1'";            OleDbConnection conn = new OleDbConnection(ConnectionExcel);
            conn.Open();
            System.Data.DataTable schemaTable = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);            dataGridWM.Rows.Clear();
            dataGridWM.Columns.Clear();            string tableName = schemaTable.Rows[0][2].ToString().Trim(new char[] { '$', '\'' });    //Sheet1            OleDbDataAdapter adp = new OleDbDataAdapter("Select * from  [" + tableName + "$]", conn);
            DataSet ds = new DataSet();
            adp.Fill(ds, "Book1");
            dataGridWM.DataSource = ds.Tables[0].DefaultView;
            conn.Close();