gpt4 book ai didi

c# - Datagridview 在选择复选框时崩溃

转载 作者:太空狗 更新时间:2023-10-30 01:29:36 24 4
gpt4 key购买 nike

情况

我正在开发一个程序,我从数据网格中选择 X 行并将它们转发以进行处理。因此,我将从我的列表中选择 5 名学生,并在按钮单击事件中将他们的 ID 发送到另一种方法。

到目前为止,我已经将我的数据加载到我的网格中,我已经通过两种方式完成了这项工作,一种是使用表的数据源,另一种是使用表适配器。像下面这样。

 private void printStudentCard_Load(object sender, EventArgs e)
{

// metroGrid2.Rows.Add(true);


string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string sql = "SELECT listOfFields FROM Students";
SqlConnection connection = new SqlConnection(connectionString);
SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection);
DataSet ds = new DataSet();
connection.Open();
dataadapter.Fill(ds, "Students");
connection.Close();
metroGrid2.DataSource = ds;
metroGrid2.DataMember = "Students";


}

现在对于这两个表,我都输入了编辑列选项并选择添加复选框按钮。

问题

问题是,当我选择一个复选框来“选中它”时,程序会立即崩溃并显示以下内容

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in System.Windows.Forms.dll

Additional information: Specified argument was out of the range of valid values.

并指向

static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1()); //Error on here
}

在 program.cs 文件中。 (以为我认为这与它无关);

<ExceptionString>System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: rowIndex

我知道超出范围的异常很可能是因为长度无效,但我真的不确定会在哪里发生。

提供的任何帮助将不胜感激,为上下文添加我的其余代码

    DataTable dt = new DataTable("Students");
private void printMultiCard_Load(object sender, EventArgs e)
{

string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string sql = "SELECT listOfFields FROM Students";
SqlConnection connection = new SqlConnection(connectionString);
SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection);
DataSet ds = new DataSet();
connection.Open();
dataadapter.Fill(ds, "Students");
connection.Close();
metroGrid2.DataSource = ds;
metroGrid2.DataMember = "Students";


}

private void metroGrid1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

}

private void metroGrid2_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

}

private void metroGrid2_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show("stest");
}

堆栈跟踪

<StackTrace>   at System.Windows.Forms.DataGridViewCell.GetValue(Int32 rowIndex)
at System.Windows.Forms.DataGridViewCell.SetValue(Int32 rowIndex, Object value)
at DevComponents.DotNetBar.Controls.DataGridViewCheckBoxXCell.OnMouseUp(DataGridViewCellMouseEventArgs e)
at System.Windows.Forms.DataGridViewCell.OnMouseUpInternal(DataGridViewCellMouseEventArgs e)
at System.Windows.Forms.DataGridView.OnCellMouseUp(DataGridViewCellMouseEventArgs e)
at System.Windows.Forms.DataGridView.OnMouseUp(MouseEventArgs e)
at System.Windows.Forms.Control.WmMouseUp(Message&amp;amp; m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message&amp;amp; m)
at System.Windows.Forms.DataGridView.WndProc(Message&amp;amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp;amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp;amp; m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp;amp; msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at MPID_App.Program.Main() in H:\Documents\Projects\Application\MApp\MApp\Program.cs:line 20
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()</StackTrace><ExceptionString>System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: rowIndex
at System.Windows.Forms.DataGridViewCell.GetValue(Int32 rowIndex)
at System.Windows.Forms.DataGridViewCell.SetValue(Int32 rowIndex, Object value)
at DevComponents.DotNetBar.Controls.DataGridViewCheckBoxXCell.OnMouseUp(DataGridViewCellMouseEventArgs e)
at System.Windows.Forms.DataGridViewCell.OnMouseUpInternal(DataGridViewCellMouseEventArgs e)
at System.Windows.Forms.DataGridView.OnCellMouseUp(DataGridViewCellMouseEventArgs e)
at System.Windows.Forms.DataGridView.OnMouseUp(MouseEventArgs e)
at System.Windows.Forms.Control.WmMouseUp(Message&amp;amp; m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message&amp;amp; m)
at System.Windows.Forms.DataGridView.WndProc(Message&amp;amp; m)

最佳答案

堆栈跟踪在崩溃发生前不久显示以下行:

DevComponents.DotNetBar.Controls.DataGridViewCheckBoxXCell.OnMouseUp

因此我建议恢复到开箱即用的 DataGridView,以隔离问题是否是由该组件引起的。

问题不包含 MCVE ,但是我创建的示例将带有学生表的数据集放入现成的 DataGridView 的数据源中,其中 DataTable 中的列之一是复选框列,我手动添加了一个复选框列(就像你正在做的那样)没有这个问题。

一些不相关的提示:SqlConnectionSqlDataAdapter 都是 IDisposable 因此应该在 using block 中。完成后,您将不需要 connection.Close();,因为退出 using block 将调用 Dispose(),它将为您做到这一点。

关于c# - Datagridview 在选择复选框时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51110256/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com