c# – Datagridview在选中复选框时崩溃
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – Datagridview在选中复选框时崩溃,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6169字,纯文字阅读大概需要9分钟。
内容图文
![c# – Datagridview在选中复选框时崩溃](/upload/InfoBanner/zyjiaocheng/789/24e9cd8033e24a74ac0940f07e75c584.jpg)
情况
我正在研究一个程序,从数据网格中,我选择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; m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message&amp; m)
at System.Windows.Forms.DataGridView.WndProc(Message&amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&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; m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message&amp; m)
at System.Windows.Forms.DataGridView.WndProc(Message&amp; m)
解决方法:
堆栈跟踪在崩溃发生前不久显示以下行:
DevComponents.DotNetBar.Controls.DataGridViewCheckBoxXCell.OnMouseUp
所以我建议恢复到开箱即用的DataGridView,以确定问题是否是由该组件引起的.
这个问题不包含MCVE,但是我创建的示例将带有Student表的DataSet放入一个开箱即用的DataGridView的DataSource中,其中DataTable中的一列是复选框列,并且我手动添加了一个复选框列(就像你正在做的那样)没有这个问题.
一些不相??关的提示:SqlConnection和SqlDataAdapter都是IDisposable所以应该使用块.完成后,您将不需要connection.Close();,因为退出using块将调用Dispose(),它将为您执行此操作.
内容总结
以上是互联网集市为您收集整理的c# – Datagridview在选中复选框时崩溃全部内容,希望文章能够帮你解决c# – Datagridview在选中复选框时崩溃所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。