System.IndexOutOfRangeException:在 Ubuntu 上使用两个 Windows 窗体 DataGridView 的主/详细表单的列表位置
我有一个Windows表单,其中包含2个代表1:N关系的数据网格视图。当用户从父表(对应第一个数据网格视图对应)单击一行时,与该行相关的子表中的元组显示在第二个数据网格视图中。
问题在于,当我与表单进行交互并在数据网格视图中单击时,该程序随着输出而崩溃:
System.IndexOutOfRangeException: list position
at System.Windows.Forms.CurrencyManager.get_Current () [0x0001c] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.CurrencyManager.EndCurrentEdit () [0x0000a] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.CurrencyManager.set_Position (System.Int32 value) [0x00037] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.DataGridView.UpdateBindingPosition (System.Int32 position) [0x0000e] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore (System.Int32 columnIndex, System.Int32 rowIndex, System.Boolean setAnchorCellAddress, System.Boolean validateCurrentCell, System.Boolean throughMouseClick) [0x001c7] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.DataGridView.MoveCurrentCell (System.Int32 x, System.Int32 y, System.Boolean select, System.Boolean isControl, System.Boolean isShift, System.Boolean scroll) [0x00105] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.DataGridView.OnListPositionChanged (System.Object sender, System.EventArgs args) [0x0004c] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.DataGridView.ResetRows () [0x0005d] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.DataGridView.OnListChanged (System.Object sender, System.ComponentModel.ListChangedEventArgs args) [0x0004d] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.CurrencyManager.OnListChanged (System.ComponentModel.ListChangedEventArgs args) [0x00008] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.CurrencyManager.ListChangedHandler (System.Object sender, System.ComponentModel.ListChangedEventArgs e) [0x0003d] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.BindingSource.OnListChanged (System.ComponentModel.ListChangedEventArgs e) [0x00019] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.BindingSource.ResetBindings (System.Boolean metadataChanged) [0x0000b] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.BindingSource.SetList (System.Collections.IList l) [0x000b8] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.BindingSource.ResetList () [0x00112] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.BindingSource.OnParentCurrencyManagerChanged (System.Object sender, System.EventArgs args) [0x00006] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at (wrapper delegate-invoke) <Module>.invoke_void_object_EventArgs(object,System.EventArgs)
at System.Windows.Forms.CurrencyManager.OnCurrentChanged (System.EventArgs e) [0x0001d] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.CurrencyManager.set_Position (System.Int32 value) [0x00044] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.DataGridView.UpdateBindingPosition (System.Int32 position) [0x0000e] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore (System.Int32 columnIndex, System.Int32 rowIndex, System.Boolean setAnchorCellAddress, System.Boolean validateCurrentCell, System.Boolean throughMouseClick) [0x001c7] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.DataGridView.OnMouseDown (System.Windows.Forms.MouseEventArgs e) [0x0014e] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.Control.WmLButtonDown (System.Windows.Forms.Message& m) [0x0008b] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.Control.WndProc (System.Windows.Forms.Message& m) [0x001bc] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.DataGridView.WndProc (System.Windows.Forms.Message& m) [0x00000] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.Control+ControlWindowTarget.OnMessage (System.Windows.Forms.Message& m) [0x00000] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.Control+ControlNativeWindow.WndProc (System.Windows.Forms.Message& m) [0x0000b] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.NativeWindow.WndProc (System.IntPtr hWnd, System.Windows.Forms.Msg msg, System.IntPtr wParam, System.IntPtr lParam) [0x00085] in <6d635ac3dc1c4424ad385ded79f1e868>:0
您可以向我解释发生了什么/我在做什么错以及如何修复它?
我猜这是相关的源代码(也有自动生成的 form1.designer.cs 文件,但我相信问题不存在):
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace A1
{
public partial class Form1 : Form
{
SqlConnection sqlConnection;
SqlDataAdapter dataAdapterPosition, dataAdapterSalary;
DataSet dataSet;
SqlCommandBuilder sqlCommandBuilderSalary;
BindingSource bindingSourcePosition, bindingSourceSalary;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var connectionString = Program.getConnectionString();
if (connectionString == null)
{
Program.LogError("[error][Form1.button1_Click()] connectionString is null after reading it from file. ");
return;
}
Console.WriteLine("[log][Form1.button1_Click()] connectionString: " + connectionString);
if ((sqlConnection = new SqlConnection(connectionString)) == null)
{
Program.LogError("[error][Form1.button1_Click()] creating a SQL Connection failed");
return;
}
Console.WriteLine("[log][Form1.button1_Click()] created SQL Connection");
try
{
// open the connection
sqlConnection.Open();
Console.WriteLine("[log][Form1.button1_Click()] Opened the SQL Connection");
Console.WriteLine("[log][Form1.button1_Click()] The state of the connection: " + sqlConnection.State);
// switch to the CoffeeShopDB
using (SqlCommand sqlCommand = new SqlCommand("USE [CoffeeShopDB]", sqlConnection))
{
sqlCommand.ExecuteNonQuery();
Console.WriteLine("[log][Form1.button1_Click()] Switched to [CoffeeShopDB]");
}
dataAdapterPosition = new SqlDataAdapter("select * from Position", sqlConnection);
dataAdapterSalary = new SqlDataAdapter("select * from Salary", sqlConnection);
sqlCommandBuilderSalary = new SqlCommandBuilder(dataAdapterSalary);
dataSet = new DataSet();
dataAdapterPosition.Fill(dataSet, "Position");
dataAdapterSalary.Fill(dataSet, "Salary");
dataSet.Relations.Add(new DataRelation("FK_pid_Position_to_Salary", dataSet.Tables["Position"].Columns["pid"], dataSet.Tables["Salary"].Columns["pidfk"]));
bindingSourcePosition = new BindingSource { DataSource = dataSet, DataMember = "Position" };
bindingSourceSalary = new BindingSource { DataSource = bindingSourcePosition, DataMember = "FK_pid_Position_to_Salary" };
dataGridView1.DataSource = bindingSourcePosition;
dataGridView2.DataSource = bindingSourceSalary;
// END: free resources
//sqlConnection.Close();
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (dataSet != null)
{
try
{
Console.WriteLine("[log][Form1.button2_Click()] Updated " + dataAdapterSalary.Update(dataSet, "Salary") + " rows");
}
catch (Exception exception)
{
Program.LogError("[error][Form1.button2_Click()] Exception caught: " + exception.Message);
}
}
else
{
Program.LogError("[error][Form1.button2_Click()] dataSet is null");
}
}
}
}
编辑:以下是生成的文件:
我尝试包装application.run(new form1());
在一个try-catch块中,但无济于事。例外未抓住。
编辑2: 我只能在骑手和单个开发的Linux(Ubuntu)上获得这种行为。在窗户上,一切正常。
I have a Windows Form which contains 2 data grid views which represent a 1:n relationship. When the user clicks on a row from the parent table (corresponding to the first data grid view), then the tuples from the child table related to that row appear in the second data grid view.
The problem is that when I'm interacting with the form and clicking inside the data grid views, the program crashes with this output:
System.IndexOutOfRangeException: list position
at System.Windows.Forms.CurrencyManager.get_Current () [0x0001c] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.CurrencyManager.EndCurrentEdit () [0x0000a] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.CurrencyManager.set_Position (System.Int32 value) [0x00037] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.DataGridView.UpdateBindingPosition (System.Int32 position) [0x0000e] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore (System.Int32 columnIndex, System.Int32 rowIndex, System.Boolean setAnchorCellAddress, System.Boolean validateCurrentCell, System.Boolean throughMouseClick) [0x001c7] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.DataGridView.MoveCurrentCell (System.Int32 x, System.Int32 y, System.Boolean select, System.Boolean isControl, System.Boolean isShift, System.Boolean scroll) [0x00105] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.DataGridView.OnListPositionChanged (System.Object sender, System.EventArgs args) [0x0004c] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.DataGridView.ResetRows () [0x0005d] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.DataGridView.OnListChanged (System.Object sender, System.ComponentModel.ListChangedEventArgs args) [0x0004d] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.CurrencyManager.OnListChanged (System.ComponentModel.ListChangedEventArgs args) [0x00008] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.CurrencyManager.ListChangedHandler (System.Object sender, System.ComponentModel.ListChangedEventArgs e) [0x0003d] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.BindingSource.OnListChanged (System.ComponentModel.ListChangedEventArgs e) [0x00019] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.BindingSource.ResetBindings (System.Boolean metadataChanged) [0x0000b] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.BindingSource.SetList (System.Collections.IList l) [0x000b8] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.BindingSource.ResetList () [0x00112] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.BindingSource.OnParentCurrencyManagerChanged (System.Object sender, System.EventArgs args) [0x00006] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at (wrapper delegate-invoke) <Module>.invoke_void_object_EventArgs(object,System.EventArgs)
at System.Windows.Forms.CurrencyManager.OnCurrentChanged (System.EventArgs e) [0x0001d] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.CurrencyManager.set_Position (System.Int32 value) [0x00044] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.DataGridView.UpdateBindingPosition (System.Int32 position) [0x0000e] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore (System.Int32 columnIndex, System.Int32 rowIndex, System.Boolean setAnchorCellAddress, System.Boolean validateCurrentCell, System.Boolean throughMouseClick) [0x001c7] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.DataGridView.OnMouseDown (System.Windows.Forms.MouseEventArgs e) [0x0014e] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.Control.WmLButtonDown (System.Windows.Forms.Message& m) [0x0008b] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.Control.WndProc (System.Windows.Forms.Message& m) [0x001bc] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.DataGridView.WndProc (System.Windows.Forms.Message& m) [0x00000] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.Control+ControlWindowTarget.OnMessage (System.Windows.Forms.Message& m) [0x00000] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.Control+ControlNativeWindow.WndProc (System.Windows.Forms.Message& m) [0x0000b] in <6d635ac3dc1c4424ad385ded79f1e868>:0
at System.Windows.Forms.NativeWindow.WndProc (System.IntPtr hWnd, System.Windows.Forms.Msg msg, System.IntPtr wParam, System.IntPtr lParam) [0x00085] in <6d635ac3dc1c4424ad385ded79f1e868>:0
Can you explain me what is happening/what am I doing wrong and how to fix it?
Here is the relevant source code, I guess (there's also the automatically generated Form1.Designer.cs file but I believe that the problem isn't there):
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace A1
{
public partial class Form1 : Form
{
SqlConnection sqlConnection;
SqlDataAdapter dataAdapterPosition, dataAdapterSalary;
DataSet dataSet;
SqlCommandBuilder sqlCommandBuilderSalary;
BindingSource bindingSourcePosition, bindingSourceSalary;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var connectionString = Program.getConnectionString();
if (connectionString == null)
{
Program.LogError("[error][Form1.button1_Click()] connectionString is null after reading it from file. ");
return;
}
Console.WriteLine("[log][Form1.button1_Click()] connectionString: " + connectionString);
if ((sqlConnection = new SqlConnection(connectionString)) == null)
{
Program.LogError("[error][Form1.button1_Click()] creating a SQL Connection failed");
return;
}
Console.WriteLine("[log][Form1.button1_Click()] created SQL Connection");
try
{
// open the connection
sqlConnection.Open();
Console.WriteLine("[log][Form1.button1_Click()] Opened the SQL Connection");
Console.WriteLine("[log][Form1.button1_Click()] The state of the connection: " + sqlConnection.State);
// switch to the CoffeeShopDB
using (SqlCommand sqlCommand = new SqlCommand("USE [CoffeeShopDB]", sqlConnection))
{
sqlCommand.ExecuteNonQuery();
Console.WriteLine("[log][Form1.button1_Click()] Switched to [CoffeeShopDB]");
}
dataAdapterPosition = new SqlDataAdapter("select * from Position", sqlConnection);
dataAdapterSalary = new SqlDataAdapter("select * from Salary", sqlConnection);
sqlCommandBuilderSalary = new SqlCommandBuilder(dataAdapterSalary);
dataSet = new DataSet();
dataAdapterPosition.Fill(dataSet, "Position");
dataAdapterSalary.Fill(dataSet, "Salary");
dataSet.Relations.Add(new DataRelation("FK_pid_Position_to_Salary", dataSet.Tables["Position"].Columns["pid"], dataSet.Tables["Salary"].Columns["pidfk"]));
bindingSourcePosition = new BindingSource { DataSource = dataSet, DataMember = "Position" };
bindingSourceSalary = new BindingSource { DataSource = bindingSourcePosition, DataMember = "FK_pid_Position_to_Salary" };
dataGridView1.DataSource = bindingSourcePosition;
dataGridView2.DataSource = bindingSourceSalary;
// END: free resources
//sqlConnection.Close();
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (dataSet != null)
{
try
{
Console.WriteLine("[log][Form1.button2_Click()] Updated " + dataAdapterSalary.Update(dataSet, "Salary") + " rows");
}
catch (Exception exception)
{
Program.LogError("[error][Form1.button2_Click()] Exception caught: " + exception.Message);
}
}
else
{
Program.LogError("[error][Form1.button2_Click()] dataSet is null");
}
}
}
}
EDIT: here are the generated files:
I tried wrapping Application.Run(new Form1());
in a try-catch block, but to no avail. The exception isn't caught.
EDIT 2:
I get only get this behaviour on Linux (Ubuntu), in both Rider and MonoDevelop. On Windows everything works fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论