2014-03-13 57 views
0

我有這個類(的AddRecord)用方法返回一個數據表:的DataGridView無法顯示的數據表

public DataTable SelectRec(ComboBox cb, DateTimePicker dtp1, DateTimePicker dtp2) 
{ 
    SqlDataAdapter sqladpt = new SqlDataAdapter(); 
    SqlConnection conn = new SqlConnection(connectionStrings); 
    DataTable dt = new DataTable(); 

    try 
    { 
     conn.Open(); 
     SqlCommand myCmd = conn.CreateCommand(); 
     myCmd.CommandType = CommandType.StoredProcedure; 
     myCmd.CommandText = "SelectAccountRecord"; 
     myCmd.Parameters.AddWithValue("@date1", dtp1.Text); 
     myCmd.Parameters.AddWithValue("@date2", dtp1.Text); 
     myCmd.Parameters.AddWithValue("@newspaper", cb.Text); 
     myCmd.ExecuteNonQuery(); 
     sqladpt.SelectCommand = myCmd; 
     sqladpt.Fill(dt); 
     sqladpt.Dispose(); 
     //DataSource = ds.Tables["[a]"].DefaultView; 
    } 
    catch { } 

    return dt; 
} 

所以我呼籲其他此方法在我的datagridview顯示爲下面的數據表的內容

AddRecord ad = new AddRecord(); 
dgvRecorOverview.DataSource= ad.SelectRec(cbNewspaperRO, dtpFromDate, dtpToDate); 

請幫忙!

+0

有什麼錯誤? – Derek

回答

0

你必須給DataBind()爲網格分配DataSource

這樣

dgvRecorOverview.DataBind(); 
0

您必須設置DataGridView.AutoGenereateColumns = true;創建自動根據您設置爲DataSourceDataTable列後。檢查您填寫的DataTable (dt)是否具有預期的行和列。如果DataTable爲空,那麼顯然你沒有在網格中看到任何數據。