一个纠结了很长时间的问题,请高手帮忙解决,重新大红叉
如图:绑定dataGridView控件时,有时候会出现一个大大红叉,如下图:纠结很长时间,一直没有解决,请高手帮忙解决一下。
代码:
//清除最后自动生成的行strLocalConn
dgv.AutoGenerateColumns = false;
dgv.AllowUserToAddRows = false;
SqlConnection cn = new SqlConnection(strConn);
cn.Open();
SqlCommand cm = new SqlCommand(strSql, cn);
SqlDataAdapter da = new SqlDataAdapter(cm);
DataSet ds = new DataSet();
da.Fill(ds, "SoftWare");
ds.Tables["Software"].Columns.Add("ViewStar", typeof(string));
ds.Tables["Software"].Columns.Add("ViewDown", typeof(string));
ds.Tables["Software"].Columns.Add("ViewSoftSize", typeof(string));
ds.Tables["Software"].Columns.Add("ViewSoftScore", typeof(string));
ds.Tables["Software"].Columns.Add("ViewSoftComment", typeof(string));
ds.Tables["Software"].Columns.Add("ViewSoftName", typeof(string));
ds.Tables["Software"].Columns.Add("ViewSoftNameVersion", typeof(string));
for (int i = 0; i < ds.Tables["SoftWare"].Rows.Count; i++)
{
DataRow dr = ds.Tables["SoftWare"].Rows[i];
string strSoftIntro = dr["soft_intro"].ToString();
if (strSoftIntro.Length > 20)
{
strSoftIntro = strSoftIntro.Substring(0, 20) + "...";
}
dr["ViewSoftName"] = dr["soft_name"].ToString() + " " + dr["soft_version"].ToString() + "\r\n\r\n" + strSoftIntro;
dr["ViewDown"] = "下载";
dr["down_url"] = "http://www.test.com/" + dr["down_url"].ToString();
dr["ViewSoftSize"] = dr["soft_size"].ToString() + "M";
dr["ViewSoftComment"] = dr["soft_comment"].ToString() + "评论";
dr["ViewSoftNameVersion"] = dr["soft_name"].ToString() + dr["soft_version"].ToString();
//dr["ViewSoftScore"] = GetSoftwareVoteScore(dr["soft_name"].ToString() + dr["soft_version"].ToString()) + "分 我要投票";
}
dgv.DataSource = ds.Tables["SoftWare"].DefaultView;
da.Dispose();
cn.Close();
//清除当前选择行行为
dgv.CurrentCell = null;
异常错误信息:
有关调用实时(JIT)调试而不是此对话框的详细信息,
请参见此消息的结尾。
************** 异常文本 **************
System.NullReferenceException: 未将对象引用设置到对象的实例。
在 System.Windows.Forms.DataGridViewImageCell.PaintPrivate(Graphics g, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates elementState, Object formattedValue, String errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts, Boolean computeContentBounds, Boolean computeErrorIconBounds, Boolean paint)
在 System.Windows.Forms.DataGridViewImageCell.Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates elementState, Object value, Object formattedValue, String errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
在 System.Windows.Forms.DataGridViewCell.PaintWork(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
在 System.Windows.Forms.DataGridViewRow.PaintCells(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow, DataGridViewPaintParts paintParts)
在 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.3053 (netfxsp.050727-3000)
基本代码: file:///C:/Windows/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
TZWS.Client
程序集版本: 1.0.0.0
Win32 版本: 1.0.0.0
基本代码: file:///E:/Visual%20Studio%202008/RunWo.TZWSSolution/TZWS.Client/bin/Debug/TZWS.Client.exe
----------------------------------------
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
程序集版本: 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.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
----------------------------------------
TZWS.BLL
程序集版本: 1.0.0.0
Win32 版本: 1.0.0.0
基本代码: file:///E:/Visual%20Studio%202008/RunWo.TZWSSolution/TZWS.Client/bin/Debug/TZWS.BLL.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.3053 (netfxsp.050727-3000)
基本代码: file:///C:/Windows/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
TZWS.DAL
程序集版本: 1.0.0.0
Win32 版本: 1.0.0.0
基本代码: file:///E:/Visual%20Studio%202008/RunWo.TZWSSolution/TZWS.Client/bin/Debug/TZWS.DAL.DLL
----------------------------------------
TZWS.DBSqlHelp
程序集版本: 1.0.0.0
Win32 版本: 1.0.0.0
基本代码: file:///E:/Visual%20Studio%202008/RunWo.TZWSSolution/TZWS.Client/bin/Debug/TZWS.DBSqlHelp.DLL
----------------------------------------
DevComponents.DotNetBar2
程序集版本: 9.0.0.5
Win32 版本: 9.0.0.5
基本代码: file:///C:/Windows/assembly/GAC_MSIL/DevComponents.DotNetBar2/9.0.0.5__c39c3242a43eee2b/DevComponents.DotNetBar2.dll
----------------------------------------
IrisSkin2
程序集版本: 2006.3.22.45
Win32 版本: 2006.3.22.45
基本代码: file:///E:/Visual%20Studio%202008/RunWo.TZWSSolution/TZWS.Client/bin/Debug/IrisSkin2.DLL
----------------------------------------
Accessibility
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
基本代码: file:///C:/Windows/assembly/GAC_MSIL/Accessibility/2.0.0.0__b03f5f7f11d50a3a/Accessibility.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.3053 (netfxsp.050727-3000)
基本代码: file:///C:/Windows/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
TZWS.Utility
程序集版本: 1.0.0.0
Win32 版本: 1.0.0.0
基本代码: file:///E:/Visual%20Studio%202008/RunWo.TZWSSolution/TZWS.Client/bin/Debug/TZWS.Utility.DLL
----------------------------------------
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.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
----------------------------------------
************** JIT 调试 **************
要启用实时(JIT)调试,
该应用程序或计算机的 .config 文件(machine.config)的 system.windows.forms 节中必须设置
jitDebugging 值。
编译应用程序时还必须启用
调试。
例如:
<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>
启用 JIT 调试后,任何无法处理的异常
都将被发送到在此计算机上注册的 JIT 调试器,
而不是由此对话框处理。
--------------------编程问答-------------------- http://yueqing.blog.163.com/blog/static/3208259200792944447196/ --------------------编程问答-------------------- 楼上兄弟这个方法我试过,还是没有用。 --------------------编程问答-------------------- 安装 .NET 3.5 SP1 看看。
文件路径等等问题。 --------------------编程问答-------------------- 应该要判断查询结果为空的问题
if(ds!=null)
{
//为datagridview加数据
}
else
{
messagebox.show("无结果!");
} --------------------编程问答-------------------- 提示Null,应该是某个对象有时候值为空引起。
--------------------编程问答-------------------- 没有用啊,都试过了,谁能帮助解决一下。 --------------------编程问答-------------------- 错误一大串,,其实就一个对象未实例啦。。
DataSet ds = new DataSet();
da.Fill(ds, "SoftWare");
把DataSet改成DataTable一定可以。。。
如下:
DataTable ds = new DataTable();
da.Fill(ds, "SoftWare");
最好循环前判断 ds !=null --------------------编程问答--------------------
补充:.NET技术 , C#