日期:2014-05-17 浏览次数:21510 次
int n = 0; string sqlsign = "select count(*) from HouseHold,DetailAccount where HouseHold.HouseID = DetailAccount.HouseID and HouseHold.Area = '" + comboBox1.Text.ToString().Trim() + "'"; string sql = "select * from HouseHold,DetailAccount where HouseHold.HouseID = DetailAccount.HouseID and HouseHold.Area = '" + comboBox1.Text.ToString().Trim() + "'"; OleDbConnection conn = dbObj.GetConnection(); OleDbCommand comm = new OleDbCommand(sql, conn); OleDbCommand commsign = new OleDbCommand(sqlsign, conn); conn.Open(); n = (int)commsign.ExecuteScalar(); comm.ExecuteNonQuery(); OleDbDataAdapter da = new OleDbDataAdapter(comm); DataSet ds = new DataSet(); da.Fill(ds, "HouseHold"); for (int i = 0; i < n; i++) { dataGridView1.Rows[i].Cells["index_no"].Value = i+1; dataGridView1.Rows[i].Cells["HouseID"].Value = ds.Tables["HouseHold"].Rows[i][0].ToString(); dataGridView1.Rows[i].Cells["theName"].Value = ds.Tables["HouseHold"].Rows[i][8].ToString(); dataGridView1.Rows[i].Cells["theOwner"].Value = ds.Tables["HouseHold"].Rows[i][2].ToString(); dataGridView1.Rows[i].Cells["Category"].Value = ds.Tables["HouseHold"].Rows[i][3].ToString(); dataGridView1.Rows[i].Cells["Area"].Value = ds.Tables["HouseHold"].Rows[i][5].ToString(); dataGridView1.Rows[i].Cells["Address"].Value = ds.Tables["HouseHold"].Rows[i][4].ToString(); }
messagebox.show( dataGridView1.Rows.Count.ToString());
------解决方案--------------------
先要添加行,才能对每个得进行操作
dataGridView1.Rows.Add()