System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds); dataGridView1.DataSource = ds.Tables[0]; //执行到这一句发生以下错误
指定的参数已超出有效值的范围。
参数名: rowIndex奇怪的是这个错误用Try捕获不到。调试时,如果我在dataGridView1.DataSource = ds.Tables[0]上设置断点,直接继续,不会发生错误。有谁遇到过?帮帮忙。
请参见此消息的结尾。************** 异常文本 **************
System.ArgumentOutOfRangeException: 指定的参数已超出有效值的范围。
参数名: rowIndex
在 System.Windows.Forms.DataGridViewCell.GetInheritedStyle(DataGridViewCellStyle inheritedCellStyle, Int32 rowIndex, Boolean includeColors)
在 System.Windows.Forms.DataGridViewCell.GetPreferredHeight(Int32 rowIndex, Int32 width)
在 System.Windows.Forms.DataGridViewCell.OnCellDataAreaMouseEnterInternal(Int32 rowIndex)
在 System.Windows.Forms.DataGridViewCell.OnMouseMoveInternal(DataGridViewCellMouseEventArgs e)
在 System.Windows.Forms.DataGridView.OnCellMouseMove(DataGridViewCellMouseEventArgs e)
在 System.Windows.Forms.DataGridView.UpdateMouseEnteredCell(HitTestInfo hti, MouseEventArgs e)
在 System.Windows.Forms.DataGridView.OnMouseMove(MouseEventArgs e)
在 System.Windows.Forms.Control.WmMouseMove(Message& m)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.DataGridView.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
************** 已加载的程序集 **************
mscorlib
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3603 (GDR.050727-3600)
基本代码: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
EtTrackerWatch2010
程序集版本: 1.0.0.0
Win32 版本: 1.0.0.0
基本代码: file:///D:/VS2008%20Project/Prj2010/EtTrackerWatch2010/bin/Debug/EtTrackerWatch2010.exe
----------------------------------------
System.Data
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
基本代码: file:///C:/WINDOWS/assembly/GAC_32/System.Data/2.0.0.0__b77a5c561934e089/System.Data.dll
----------------------------------------
System
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Windows.Forms
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3082 (QFE.050727-3000)
基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Transactions
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
基本代码: file:///C:/WINDOWS/assembly/GAC_32/System.Transactions/2.0.0.0__b77a5c561934e089/System.Transactions.dll
----------------------------------------
System.EnterpriseServices
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
基本代码: file:///C:/WINDOWS/assembly/GAC_32/System.EnterpriseServices/2.0.0.0__b03f5f7f11d50a3a/System.EnterpriseServices.dll
----------------------------------------
System.Windows.Forms.resources
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms.resources/2.0.0.0_zh-CHS_b77a5c561934e089/System.Windows.Forms.resources.dll
----------------------------------------
mscorlib.resources
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3603 (GDR.050727-3600)
基本代码: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------************** JIT 调试 **************
要启用实时(JIT)调试,
该应用程序或计算机的 .config 文件(machine.config)的 system.windows.forms 节中必须设置
jitDebugging 值。
编译应用程序时还必须启用
调试。例如: <configuration>
<system.windows.forms jitDebugging="true" />
</configuration>启用 JIT 调试后,任何无法处理的异常
都将被发送到在此计算机上注册的 JIT 调试器,
而不是由此对话框处理。
ds.Tables[0]当然不存在 指定的参数已超出有效值的范围
先确定SQL
参数名: rowIndex
这个说明有时候你的ds中没有table表了,
那是不是有在哪里整到table呢?
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0]; //执行到这一句发生以下错误报错的那一次调试,肯定是ds为空,没有talbe,即后台获取数据没有成功
public void loadData()
{
System.Data.SqlClient.SqlConnection cn = new System.Data.SqlClient.SqlConnection(Program.Cnstring);
try
{
cn.Open();
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.Connection = cn;
while (this.Visible)
{
cmd.CommandText = "select tabstrings.stringname,DCI,DCV,cabinetname, case fuzestatus when 0 then 'Good' else 'Bad' end fuzestatus,case spdstatus when 0 then 'Good' else 'Bad' end spdstatus,trackername "
+ "from tabstrings inner join (select tabdcinforec.stringname,dcv,dci,spdstatus,fuzestatus from tabdcinforec inner join "
+ "(select stringname,max(recdatetime) recdatetime from tabdcinforec group by stringname) b "
+ "on tabdcinforec.stringname=b.stringname and tabdcinforec.recdatetime =b.recdatetime) c "
+ "on tabstrings.stringname=c.stringname order by tabstrings.Stringname,cabinetname,trackername"; System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
//dataGridView1.DataSource = ds.Tables[0];
dataGridView1.Columns.Clear();
dataGridView1.Columns.Add("1", "1");
dataGridView1.Columns.Add("2", "2");
dataGridView1.Columns.Add("3", "3");
dataGridView1.Columns.Add("4", "4");
dataGridView1.Columns.Add("5", "5");
dataGridView1.Columns.Add("6", "6");
dataGridView1.Columns.Add("7", "7");
dataGridView1.Rows.Clear();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
dataGridView1.Rows.Add(ds.Tables[0].Rows[i][0].ToString(), ds.Tables[0].Rows[i][1].ToString(), ds.Tables[0].Rows[i][2].ToString(), ds.Tables[0].Rows[i][3].ToString(), ds.Tables[0].Rows[i][4].ToString(), ds.Tables[0].Rows[i][5].ToString(), ds.Tables[0].Rows[i][6].ToString());
}
if (ds.Tables[0].Rows.Count > 0)
{
dataGridView1.Columns[0].Width = 111;
dataGridView1.Columns[1].Width = 87;
dataGridView1.Columns[2].Width = 87;
dataGridView1.Columns[3].Width = 93;
dataGridView1.Columns[4].Width = 93;
dataGridView1.Columns[5].Width = 93;
dataGridView1.Columns[6].Width = 92;
setcolor();
}
ds.Dispose();
da.Dispose(); for (int i = 0; i < 6000; i++)
{
System.Threading.Thread.Sleep(10);
if (!this.Visible) break;
} }
cmd.Dispose();
cn.Close();
cn.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
cn.Dispose();
}
dataGridView1.DataSource = null; //先清除一下
dataGridView1.DataSource = ds.Tables[0];
if (dataGridView1.DataSource != ds.TableName)
{
dataGridView1.DataSource = ds.TableName;
dataGridView1.DataBind();
}
是个线程,窗口载入后立即Start.
我不使用线程,在主程序中直接调用也是一样的,赋值DataSource就可能会出错,一行一行填就没事。
也就是说你在主Form的Form_Load()事件处理方法当中,设置DataSource也会出现这个问题?
另外,在StackTrace当中,我看到有MouseMove消息过来,按道理来说,你在设置DataSource属性,为什么一下了跳跃到这里,更靠前的StackTrace是什么样子的,还是没有了?有没有从设置DataSource的Set_DataSource逻辑走到抛异常的GetInheritedStyle方法的整个StackTrace?
整个窗口中,就这个一个函数,还有个Form_Load,在Form_Load就一句话,直接调用这个函数,效果是一样的。
在设置DataSource前面,加上这个看看,里面有没有表!
看看有没有数据出来
感觉像是没有数据写DataSet
你不是说你打断点调试的时候,就不会出错,
那你怎么知道是这一句呢,
我觉得是不是你哪块数组下表超出索引了啊
{
LoadData();
} private void LoadData()
{
DataTable table = new DataTable();
table.Columns.Add(new DataColumn("1"));
table.Columns.Add(new DataColumn("2")); DataRow r = table.NewRow();
r["1"] = "r11";
r["2"] = "r12";
table.Rows.Add(r); DataRow r1 = table.NewRow();
r1["1"] = "r21";
r1["2"] = "r22";
table.Rows.Add(r1); this.dataGridView1.DataSource = table;
}
把数据绑定放到一个循环里是什么意思
没有试过哦,即便证明我用另一个Table就可以正常工作,也不足以解决问题啊,那就更玄乎了,总不能说是Ds中的表有问题吧。不过,我可以试试看。
有关调用实时(JIT)调试而不是此对话框的详细信息,
请参见此消息的结尾。************** 异常文本 **************
System.NullReferenceException: 未将对象引用设置到对象的实例。
在 System.Windows.Forms.DataGridViewRow.BuildInheritedRowStyle(Int32 rowIndex, DataGridViewCellStyle inheritedRowStyle)
在 System.Windows.Forms.DataGridViewRow.Paint(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow)
在 System.Windows.Forms.DataGridView.PaintRows(Graphics g, Rectangle boundingRect, Rectangle clipRect, Boolean singleHorizontalBorderAdded)
在 System.Windows.Forms.DataGridView.PaintGrid(Graphics g, Rectangle gridBounds, Rectangle clipRect, Boolean singleVerticalBorderAdded, Boolean singleHorizontalBorderAdded)
在 System.Windows.Forms.DataGridView.OnPaint(PaintEventArgs e)
在 System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
在 System.Windows.Forms.Control.WmPaint(Message& m)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.DataGridView.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
************** 已加载的程序集 **************
mscorlib
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3603 (GDR.050727-3600)
基本代码: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
EtTrackerWatch2010
程序集版本: 1.0.0.0
Win32 版本: 1.0.0.0
基本代码: file:///D:/VS2008%20Project/Prj2010/EtTrackerWatch2010/bin/Debug/EtTrackerWatch2010.exe
----------------------------------------
System.Data
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
基本代码: file:///C:/WINDOWS/assembly/GAC_32/System.Data/2.0.0.0__b77a5c561934e089/System.Data.dll
----------------------------------------
System
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Windows.Forms
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3082 (QFE.050727-3000)
基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Transactions
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
基本代码: file:///C:/WINDOWS/assembly/GAC_32/System.Transactions/2.0.0.0__b77a5c561934e089/System.Transactions.dll
----------------------------------------
System.EnterpriseServices
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
基本代码: file:///C:/WINDOWS/assembly/GAC_32/System.EnterpriseServices/2.0.0.0__b03f5f7f11d50a3a/System.EnterpriseServices.dll
----------------------------------------
System.Windows.Forms.resources
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms.resources/2.0.0.0_zh-CHS_b77a5c561934e089/System.Windows.Forms.resources.dll
----------------------------------------
mscorlib.resources
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3603 (GDR.050727-3600)
基本代码: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------************** JIT 调试 **************
要启用实时(JIT)调试,
该应用程序或计算机的 .config 文件(machine.config)的 system.windows.forms 节中必须设置
jitDebugging 值。
编译应用程序时还必须启用
调试。例如: <configuration>
<system.windows.forms jitDebugging="true" />
</configuration>启用 JIT 调试后,任何无法处理的异常
都将被发送到在此计算机上注册的 JIT 调试器,
而不是由此对话框处理。
有可能是自己没有注意到的线程。因为你提到了, 打了断点不会报错!
一般会出现这种情况, 原因都是:因为断点导致了运行时间被延长!有些瞬间没有被搞定的东西。打了断点, 断下来, 再开的时候拖得比较慢, 已经搞定了。加上报错抱在style上, 用reflector看了下, 就在开头对rowindex的判断上,
rowindex小于0, 或者超过了count。一个cell被paint, 在取style的时候居然自己所在的行已经消失了,
我觉得单线程里基本上不会发生这种问题。
那么可以换成timer。 非要线程的话, 做好lock。
{
DataTable table = new DataTable();
table.Columns.Add(new DataColumn("1"));
table.Columns.Add(new DataColumn("2")); DataRow r = table.NewRow();
r["1"] = "r11";
r["2"] = "r12";
table.Rows.Add(r); DataRow r1 = table.NewRow();
r1["1"] = "r21";
r1["2"] = "r22";
table.Rows.Add(r1); lock (this.dataGridView1)
{
this.dataGridView1.DataSource = table;
}
}
以前跨线程访问TextBox之类控件,不用Invoke也不设置CheckForIllegalCrossThreadCalls = false,编译器会报错的,用DataGridView没有报错,我以为不用Invoke也没问题的,没想到就是不行。昨天晚上我就用委托一试就知道了,果然还是跨线程访问控件引起的共享冲突。如果楼上朋友有兴趣,我可以把代码贴出来。