2014-10-08 129 views
0

我需要您的幫助,瞭解如何將List綁定到DataGridView。我試圖使用BindingList<T>,但它仍然沒有在我的gridview中顯示記錄。我嘗試使用List<T>,但它仍然無法正常工作。綁定列表<class>到DataGridView

下面是我使用的代碼:

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     ToolClientController ctrl = new ToolClientController(); 
     IpAddressTextbox.Text = ctrl.GetIPv4Config(); 
     PortNumberTextbox.Text = ctrl.GetPortNumber(); 
    } 

    private void BrowseButton_Click(object sender, EventArgs e) 
    { 
     DialogResult result = openFileDialog1.ShowDialog(); 
     if (result == DialogResult.OK) 
     { 
      ToolMsgFileTextbox.Text = openFileDialog1.FileName; 
     } 
    } 

    private void UploadButton_Click(object sender, EventArgs e) 
    { 
     if (string.IsNullOrWhiteSpace(ToolMsgFileTextbox.Text)) 
     { 
      FileTypeError.SetError(UploadButton, "Please enter filename."); 
      return; 
     } 

     if (!openFileDialog1.FileName.Contains(".txt")) 
     { 
      FileTypeError.SetError(UploadButton, "File should be in .txt"); 
      return; 
     } 

     ToolClientController ctrl = new ToolClientController();    

     List<ToolMessages> test = new List<ToolMessages>(); 
     ToolMessages tool = new ToolMessages(); 
     tool.IsPass = true; 
     tool.ToolMessageReply = string.Empty; 
     tool.ToolMessageRequest = "x"; 
     test.Add(tool); 

     MessageGridViews.AutoGenerateColumns = false; 

     DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn(); 
     col1.DataPropertyName = "ToolMessageRequest"; 
     col1.HeaderText = "Request";    
     MessageGridViews.Columns.Add(col1); 

     BindingList<ToolMessages> bind = new BindingList<ToolMessages>(test); 

     MessageGridViews.VirtualMode = true; 
     MessageGridViews.DataSource = bind; 
    } 
} 
+0

我的理解是正確的,你點擊'UploadButton'並且你沒有看到輸出,如我在下面的答案,即列「Request」wih值「x」?另外,如果你根本沒有使用它,那麼你在'UploadButton_Click'中創建'ToolClientController'是什麼?在不同的說明中,您是否嘗試將斷點放入'UploadButton_Click'方法中以實際看到會發生什麼?你的代碼是否得到執行? – 2014-10-08 09:52:35

回答

0

基於您的代碼我編寫簡單的WinForms應用程序:

public Form1() 
{ 
    InitializeComponent(); 
    List<ToolMessages> test = new List<ToolMessages>(); 
    ToolMessages tool = new ToolMessages(); 
    tool.IsPass = true; 
    tool.ToolMessageReply = string.Empty; 
    tool.ToolMessageRequest = "x"; 
    test.Add(tool); 

    dataGridView1.AutoGenerateColumns = false; 

    DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn(); 
    col1.DataPropertyName = "ToolMessageRequest"; 
    col1.HeaderText = "Request"; 
    dataGridView1.Columns.Add(col1); 

    BindingList<ToolMessages> bind = new BindingList<ToolMessages>(test); 

    dataGridView1.DataSource = bind; 
    dataGridView1.Show(); 
} 

我通過的WinForms設計師直接加入dataGridView1到形式,所以InitializeComponent方法看起來像:

private void InitializeComponent() 
{ 
    this.dataGridView1 = new System.Windows.Forms.DataGridView(); 
    ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); 
    this.SuspendLayout(); 
    // 
    // dataGridView1 
    // 
    this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; 
    this.dataGridView1.Location = new System.Drawing.Point(560, 57); 
    this.dataGridView1.Name = "dataGridView1"; 
    this.dataGridView1.Size = new System.Drawing.Size(240, 150); 
    this.dataGridView1.TabIndex = 1; 
    // 
    // Form1 
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
    this.ClientSize = new System.Drawing.Size(967, 399); 
    this.Controls.Add(this.dataGridView1); 
    this.Name = "Form1"; 
    this.Text = "Form1"; 
    ((System.ComponentModel.ISupportInitialize)(this.gridControl1)).EndInit(); 
    ((System.ComponentModel.ISupportInitialize)(this.gridView1)).EndInit(); 
    ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); 
    this.ResumeLayout(false); 
} 

而一切似乎t Ø工作就好...數據綁定到網格,輸出中的樣子: enter image description here

這正是我希望看到的,所以我沒有看到任何真正的問題與您的解決方案,除非您發佈大我可以在其上重現您遇到的問題的一段代碼。

+0

我會編輯我的問題,因爲我仍然無法獲取要顯示的記錄。 – Musikero31 2014-10-08 09:34:43

+0

我剛發現我的實體導致了這個問題。我設置了[[Browsable(true)]',它工作。 – Musikero31 2014-10-10 01:43:34