在过滤dataGridView时,对象引用未设置为对象的实例 [英] Object reference not set to an instance of an object, While Filtering dataGridView
本文介绍了在过滤dataGridView时,对象引用未设置为对象的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hello All,
代码工作正常。但是在过滤dataGridView2时出现错误对象引用没有设置为对象的实例
Hello All,
Code works Fine. But While Filtering dataGridView2 i got an error"Object reference not set to an instance of an object"
private void Form1_Load(object sender, EventArgs e)
{
BindGridForExcel();
BindGridForSql();
}
private void BindGridForExcel()
{
try
{
OleDbConnection theConnection = new OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;data source=D:\\DotNet\\Copy of Depreciation working Oct-13.xlsx ;Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1;\"");
theConnection.Open();
OleDbDataAdapter theDataAdapter = new OleDbDataAdapter("SELECT * FROM [Working$]", theConnection);
DataSet theDS = new DataSet();
DataTable dt = new DataTable();
theDataAdapter.Fill(dt);
this.dataGridView1.DataSource = dt.DefaultView;
theConnection.Close();
}
catch (Exception ex)
{
}
}
private void BindGridForSql()
{
string connectionString = "Data Source=192.168.0.11;Initial Catalog=30_Nov_13;User ID=sa;Password=super";
string sql = "SELECT * from [@BA_ODPV] ";
using (var connection = new SqlConnection(connectionString))
using (var command = new SqlCommand(sql, connection))
using (var adapter = new SqlDataAdapter(command))
{
connection.Open();
var dataEntry1 = new DataTable();
adapter.Fill(dataEntry1);
dataGridView2.DataSource = dataEntry1;
}
}
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
DataGridViewSelectedRowCollection rc = null;
rc = dataGridView1.SelectedRows;
itemcode = rc[0].Cells["F1"].Value.ToString();
for (int i = 0; i < dataGridView2.Rows.Count; i++)
{
if (dataGridView2.Rows[i].Cells[2].Value.ToString() == itemcode)
{
dataGridView2.Rows[i].Selected = true;
dataGridView2.Rows[i].Visible = true;
}
}
}
推荐答案
,连接);
DataSet theDS = new DataSet();
DataTable dt = new DataTable();
theDataAdapter.Fill(dt);
this .dataGridView1.DataSource = dt.DefaultView;
theConnection.Close ();
}
catch (例外情况)
{
}
}
private void BindGridForSql()
{
string connectionString = 数据源= 192.168 .0.11;初始目录= 30_Nov_13;用户ID = sa;密码=超级;
string sql = SELECT *来自[@BA_ODPV];
使用( var connection = new SqlConnection(connectionString))
使用( var command = new SqlCommand(sql,connection))
使用( var adapter = new SqlDataAdapter(command))
{
connection.Open();
var dataEntry1 = new DataTable();
adapter.Fill(dataEntry1);
dataGridView2.DataSource = dataEntry1;
}
}
私有 void dataGridView1_DoubleClick( object sender,EventArgs e)
{
DataGridViewSelectedRowCollection rc = null ;
rc = dataGridView1.SelectedRows;
itemcode = rc [ 0 ]。单元格[ F1跨度>] Value.ToString();
for ( int i = 0 ; i < dataGridView2.Rows.Count; i ++)
{
if (dataGridView2.Rows [i] .Cells [ 2 ]。Value.ToString()== itemcode)
{
dataGridView2.Rows [i] .Selected = true ;
dataGridView2.Rows [i] .Visible = true ;
}
}
}
", theConnection); DataSet theDS = new DataSet(); DataTable dt = new DataTable(); theDataAdapter.Fill(dt); this.dataGridView1.DataSource = dt.DefaultView; theConnection.Close(); } catch (Exception ex) { } } private void BindGridForSql() { string connectionString = "Data Source=192.168.0.11;Initial Catalog=30_Nov_13;User ID=sa;Password=super"; string sql = "SELECT * from [@BA_ODPV] "; using (var connection = new SqlConnection(connectionString)) using (var command = new SqlCommand(sql, connection)) using (var adapter = new SqlDataAdapter(command)) { connection.Open(); var dataEntry1 = new DataTable(); adapter.Fill(dataEntry1); dataGridView2.DataSource = dataEntry1; } } private void dataGridView1_DoubleClick(object sender, EventArgs e) { DataGridViewSelectedRowCollection rc = null; rc = dataGridView1.SelectedRows; itemcode = rc[0].Cells["F1"].Value.ToString(); for (int i = 0; i < dataGridView2.Rows.Count; i++) { if (dataGridView2.Rows[i].Cells[2].Value.ToString() == itemcode) { dataGridView2.Rows[i].Selected = true; dataGridView2.Rows[i].Visible = true; } } }
可能的错误:
每当你试图访问一个对象索引时,请确保该对象不为null ...
尝试添加此行
Possible errors :
whenever u try to access an object index,, please make sure that the object is not null...
try adding this line
if( dt != null )
above
this.dataGridView1.DataSource = dt.DefaultView;
try adding this line
if( rc != null && rc.count > )
if( rc[0].cells.count > 0)
itemcode = rc[0].Cells["F1"].Value.ToString();
if( dataGridView2 != null && datagridview2.rows.count > 0)
for (int i = 0; i < dataGridView2.Rows.Count; i++)
if( datagridview2.rows[i].cells.count >2 )
if (dataGridView2.Rows[i].Cells[2].Value.ToString() == itemcode)
这篇关于在过滤dataGridView时,对象引用未设置为对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文