文件第二次打开文件给错误
我有一个开始工作的程序。我可以打开并存储文件。但是,当我打开文件(例如文件1)时,程序会读取它,而不是想打开第二个文件(例如文件2)时,我会遇到错误。
打开文件带有bindingsource.datasource
我假设存在问题,但我试图将其设置为null
,但这会给其他错误。
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
string CheckConnection;
_bindingSource.DataSource = null;
using (OpenFileDialog ofd = new OpenFileDialog())
{
ofd.Filter = "XML File (*.xml)|*.xml";
ofd.Title = "Open Client Profile File";
if (ofd.ShowDialog() == DialogResult.OK)
{
lblCompanyName.Enabled = true;
lblClientNumber.Enabled = true;
lblSiteName.Enabled = true;
lblMachineTotal.Enabled = true;
lblImo.Enabled = true;
cmbMachineName.Enabled = true;
lblPower.Enabled = true;
lblMachineType.Enabled = true;
lblFrequency.Enabled = true;
lblSpeed.Enabled = true;
//Deserialize
_root = HelperXml.DeserializeXMLFileToObject<XmlRoot>(ofd.FileName);
ClientFileName = ofd.FileName; //Store current filename
lblCompanyName.ForeColor = Color.Black;
lblCompanyName.Text = "Company Name: " + _root.CompanyProfile.CompanyName;
lblSiteName.Text ="Sitename: " + _root.CompanyProfile.SiteName;
lblImo.Text = "IMO: " + _root.CompanyProfile.Imo.ToString();
lblMachineTotal.Text = "Machine Total: " + _root.CompanyProfile.MachineTotal.ToString();
lblClientNumber.Text = "Clientnumber: " + _root.CompanyProfile.ClientNumber.ToString();
_bindingSource.DataSource = _root.MachineProfiles.ToList();
cmbMachineName.DataSource = _bindingSource;
lblPower.DataBindings.Add("Text", _bindingSource, nameof(MachineProfile.NominalPower));
lblFrequency.DataBindings.Add("Text", _bindingSource, nameof(MachineProfile.Frequency));
lblMachineType.DataBindings.Add("Text", _bindingSource, nameof(MachineProfile.TypeDescription));
lblSpeed.DataBindings.Add("Text", _bindingSource, nameof(MachineProfile.NominalSpeed));
_bindingSource2.DataSource = _root.MachineMeasurements.ToList();
dataGridView1.DataSource = _bindingSource2;
dataGridView1.DataBindings.Add("Text", _bindingSource2, nameof(MachineMeasurement.MeasurementDate));
}
}
CheckConnection = statusLblDevice.Text.ToString();
//Enable "ImportData" button function only when a clientfile has been loaded and a device has been detected.
if (CheckConnection.Contains("VM25")== true && lblSiteName.Text != null)
{
importDataToolStripMenuItem.Enabled = true;
}
}
以上是打开文件的代码,该文件已连接到其他类,但我认为没有问题。我认为在运行此代码之前,全部都应该为空。 有人知道我该怎么做吗?
没有添加任何代码,我会收到此错误:
System.ArgumentException: 'This causes two bindings in the collection to bind to the same property.
的代码时收到的错误消息:DataMember'
System.ArgumentException: 'Cannot bind to the property or column NominalPower on the DataSource.
这是我添加用于制作BindingSource null参数名称
I have a program that start work. I can open and store files. But when I open a file (e.g. File 1) the program reads it and than when I want to open a second file (e.g. File 2) I get errors.
Opening files goes with bindingSource.DataSource
I assume the issue is there but I have tried to set it to Null
but that give other errors.
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
string CheckConnection;
_bindingSource.DataSource = null;
using (OpenFileDialog ofd = new OpenFileDialog())
{
ofd.Filter = "XML File (*.xml)|*.xml";
ofd.Title = "Open Client Profile File";
if (ofd.ShowDialog() == DialogResult.OK)
{
lblCompanyName.Enabled = true;
lblClientNumber.Enabled = true;
lblSiteName.Enabled = true;
lblMachineTotal.Enabled = true;
lblImo.Enabled = true;
cmbMachineName.Enabled = true;
lblPower.Enabled = true;
lblMachineType.Enabled = true;
lblFrequency.Enabled = true;
lblSpeed.Enabled = true;
//Deserialize
_root = HelperXml.DeserializeXMLFileToObject<XmlRoot>(ofd.FileName);
ClientFileName = ofd.FileName; //Store current filename
lblCompanyName.ForeColor = Color.Black;
lblCompanyName.Text = "Company Name: " + _root.CompanyProfile.CompanyName;
lblSiteName.Text ="Sitename: " + _root.CompanyProfile.SiteName;
lblImo.Text = "IMO: " + _root.CompanyProfile.Imo.ToString();
lblMachineTotal.Text = "Machine Total: " + _root.CompanyProfile.MachineTotal.ToString();
lblClientNumber.Text = "Clientnumber: " + _root.CompanyProfile.ClientNumber.ToString();
_bindingSource.DataSource = _root.MachineProfiles.ToList();
cmbMachineName.DataSource = _bindingSource;
lblPower.DataBindings.Add("Text", _bindingSource, nameof(MachineProfile.NominalPower));
lblFrequency.DataBindings.Add("Text", _bindingSource, nameof(MachineProfile.Frequency));
lblMachineType.DataBindings.Add("Text", _bindingSource, nameof(MachineProfile.TypeDescription));
lblSpeed.DataBindings.Add("Text", _bindingSource, nameof(MachineProfile.NominalSpeed));
_bindingSource2.DataSource = _root.MachineMeasurements.ToList();
dataGridView1.DataSource = _bindingSource2;
dataGridView1.DataBindings.Add("Text", _bindingSource2, nameof(MachineMeasurement.MeasurementDate));
}
}
CheckConnection = statusLblDevice.Text.ToString();
//Enable "ImportData" button function only when a clientfile has been loaded and a device has been detected.
if (CheckConnection.Contains("VM25")== true && lblSiteName.Text != null)
{
importDataToolStripMenuItem.Enabled = true;
}
}
Above is the code that opens a file this is connected to other classes but I think there is not the issue. I think prior to run this code all should be empty or so.
Does anybody know how I could do this?
Without adding any code I get this error:
System.ArgumentException: 'This causes two bindings in the collection to bind to the same property.
This is the error message I get when I added the code for making the bindingSource null
System.ArgumentException: 'Cannot bind to the property or column NominalPower on the DataSource.
Parameter name: dataMember'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论