2014-03-28 119 views
0

所以我有一個從我的數據庫顯示記錄的問題。我的表單只有兩個數據網格。這裏是我的代碼:來自sql數據庫的記錄沒有顯示在數據網格上

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Data.SqlClient; 

namespace testare_selectie 
{ 
    public partial class Form1 : Form 
    { 
     DataSet ds = new DataSet(); 
     SqlDataAdapter daVendors = new SqlDataAdapter(); 
     SqlDataAdapter daInvoices = new SqlDataAdapter(); 
     SqlConnection cs = new SqlConnection("Data Source=USER-PC\\SQLEXPRESS;Initial Catalog=db;integrated security = TRUE"); 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      SqlCommand slctVendors = new SqlCommand("SELECT * FROM Developerss ",cs); 
      daVendors.SelectCommand = slctVendors; 
      daVendors.Fill(ds,"Developerss"); 

      SqlCommand slctInvoices = new SqlCommand("SELECT * FROM Games", cs); 
      daVendors.SelectCommand = slctInvoices; 
      daVendors.Fill(ds, "Games"); 

      dgV.DataSource = ds.Tables["Developerss"]; 
      dgI.DataSource = ds.Tables["Games"]; 
     } 
    } 
} 

此外,我想提一提,我以前這樣做過一個項目,那也有一個顯示按鈕,它的工作,我不知道爲什麼這個人是不是。我跟進了YouTube上的一個教程。我檢查了很多次,但我似乎無法弄清楚這些錯誤或連接參數。

預先感謝您!

視頻來源:https://www.youtube.com/watch?v=m_K__V0rIz4

+0

缺少你的dgV.DataBind()和dgI.DataBind()? –

+0

這個人沒有添加它:(加上我沒有添加到我以前的項目,當我顯示數據 – dsanatomy

回答

1

的WinForms DataGridView中不需要數據綁定。所以這部分是正確的。如果數據未顯示,請檢查以下內容:

  1. 數據存在於源表中。 (直接查詢OR SQL分析器結果以獲得真實指示)
  2. 檢查數據網格視圖的可見性。
  3. ds.Tables [0] .Rows.Count
  4. 上的斷點檢查Form1_Load方法是否真正被調用。檢查表單的屬性以查看Load事件連接。

應該指出真正的原因。

+0

檢查了前兩個。但我的斷點沒有打:/ – dsanatomy

+0

是Form1_Load連接了嗎?也許事件處理程序根本沒有被調用,試着雙擊表單的標題欄來查看它是否創建了一個新的事件處理程序,或者進入相同的方法 –

+0

是的,它似乎修復它!非常感謝! – dsanatomy

相關問題