我已经下载了一个应用程序示例源代码,并且我将一些加载Excel文件的代码更改为DGV。代码只是一个快速测试,不属于高效项目。
代码使用OLEDB将Excel文件中的数据加载到datagridview控件中。当应用程序首次执行时,它会正确显示窗口(此时无法访问excel)。
当从Excel执行加载的按钮时,令我惊讶的是,当OLEDB连接建立时,当前显示的窗口会“自动”重新调整大小,就像您手动缩小一样(如您所知,这不是一个功能在winforms中,但是当你按下ctrl + shift +减号时在网上可用。数据正确加载到DGV。
代码根本不会触及缩放属性或任何窗口属性。
我看不出这两个事件之间有任何逻辑关系。对我来说根本没有意义。
问题 我不希望这种缩放发生。我无法解释为什么这是hapening。您的建议表示赞赏。
平台 我在intel 64机器上使用VS2015和Windows 10
我的尝试 我使用debug来找到问题发生的位置。我执行其他功能,如使用FileDialog控件浏览,它工作。我尝试将文本写入窗口上的标签,该标签按预期工作。只有在显式或隐式地(通过数据适配器)打开连接后才会出现此问题!
代码 下面我介绍部分代码(但我觉得它没有帮助!)
OleDbConnection objCon = null;
...
private void cmdReadExcel_Click(object sender, EventArgs e)
{
objDS = new DataSet();
string szConStr = collect_Con(System.IO.Path.GetExtension(txtFilePath.Text), txtFilePath.Text);
try
{
string dtName = "myExcelFile";
objCon = new OleDbConnection(szConStr);
//Added the following to ensure that no other code
//may have influenced these values
//These values are the default anyway
this.AutoSize = false;
this.AutoSizeMode = AutoSizeMode.GrowOnly;
//up to here code works as expected
objCon.Open();//*** Zoom out occurs after this line executes!!!!!!!
objDTExcel = new DataTable(dtName);
string sql =
@"select * from [Sheet1$]";
objDA = new System.Data.OleDb.OleDbDataAdapter(sql, objCon);
//*** zoom out occurs here if the connection is not explicity opened!!!
objDA.Fill(objDTExcel);
dataGridView1.DataSource = objDTExcel;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (objDA != null)
{
objDA.Dispose();
objDA = null;
}
if (objCon != null)
{
objCon.Close();
objCon.Dispose();
objCon = null;
}
}
}
2次单独运行的示例图片: