且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在过滤dataGridView时,对象引用未设置为对象的实例

更新时间:2022-02-17 04:29:31

,连接);
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)