2013-02-11 56 views
2

我需要在這個問題上您的專業知識: 我有一個DataGridView,我填了表適配器與圖像C#的DataGridView不顯示

public contractManage() 
     { 
      InitializeComponent(); 

      try 
      { 
       this.tblContractsTableAdapter.FillBy(this.tESTDataSet.tblContracts); 
      } 
      catch (System.Exception ex) 
      { 
       System.Windows.Forms.MessageBox.Show(ex.Message); 
      } 

      //Set color status 
      setStatus(); 

     } 

然後我要添加3列(在指數8, 9和10),它將根據我在數據庫中的值顯示不同的圖像 對於每一行,我在第一列中都有我的記錄ID,我查詢我的數據庫以獲得3個值 AtrOk true/false,PrOk true/false ,PoOk true/false

public void setStatus() 
    { 
     SqlConnection conn; 

     this.dataGridViewContractManage.AutoGenerateColumns = false; 
     this.dataGridViewContractManage.ReadOnly = false; 

     //Add images column 
     DataGridViewImageColumn icAtr = new DataGridViewImageColumn(); 
      icAtr.HeaderText = "ATR"; 
      icAtr.Image = null; 
      icAtr.Name = "cImgAtr"; 
      icAtr.Width = 35; 
      icAtr.DefaultCellStyle.NullValue = null; 
     this.dataGridViewContractManage.Columns.Add(icAtr); 

     DataGridViewImageColumn icPr = new DataGridViewImageColumn(); 
      icPr.HeaderText = "PR"; 
      icPr.Image = null; 
      icPr.Name = "cImgPr"; 
      icPr.Width = 35; 
      icPr.DefaultCellStyle.NullValue = null; 
     this.dataGridViewContractManage.Columns.Add(icPr); 

     DataGridViewImageColumn icPo = new DataGridViewImageColumn(); 
      icPo.HeaderText = "PO"; 
      icPo.Image = null; 
      icPo.Name = "cImgPo"; 
      icPo.Width = 35; 
      icPo.DefaultCellStyle.NullValue = null; 
     this.dataGridViewContractManage.Columns.Add(icPo); 

     //Browse through dataGridView rows 
     foreach (DataGridViewRow row in dataGridViewContractManage.Rows) 
     { 
      //Capture contract id in first cell 
      int id = Convert.ToInt32(row.Cells[0].Value); 
      bool atrOk = false; 
      bool prOk = false; 
      bool poOk = false; 

      //Query this id to get atr, pr, po status values 
      //Connection string 
      conn = sqlDbConnection.GetConnection(); 

      //Connect 
      conn.Open(); 

      try 
      { 
       SqlDataReader myReader = null; 
       SqlCommand newCmd = new SqlCommand("SELECT * FROM tblContracts WHERE tblContracts.ContractId=" + id, conn); 

       myReader = newCmd.ExecuteReader(); 

       while (myReader.Read()) 
       { 
        atrOk = Convert.ToBoolean(myReader["AtrOk"]); 
        prOk = Convert.ToBoolean(myReader["PrOk"]); 
        poOk = Convert.ToBoolean(myReader["PoOk"]); 
       } 

      } 
      catch (Exception e) 
      { 
       MessageBox.Show(e.ToString()); 
      } 
      finally 
      { 
       //Close DB connection 
       conn.Close(); 
      } 

      //MessageBox.Show("# " + id + "\nATR " + atrOk.ToString() + "\nPR " + prOk.ToString() + "\nPO " + poOk.ToString()); 

      //Set images in dataGridView depending on values 
      DataGridViewImageCell cellAtr = row.Cells[8] as DataGridViewImageCell; 
      DataGridViewImageCell cellPr = row.Cells[9] as DataGridViewImageCell; 
      DataGridViewImageCell cellPo = row.Cells[10] as DataGridViewImageCell; 

      cellAtr.ReadOnly = false; 
      cellPr.ReadOnly = false; 
      cellPo.ReadOnly = false; 

      if (atrOk == true) 
      { 
       cellAtr.Value = (System.Drawing.Image)Properties.Resources.Bullet_Green; 

      } 
      else 
      { 
       cellAtr.Value = (System.Drawing.Image)Properties.Resources.Bullet_Red; 

      } 

      if (prOk == true) 
      { 
       cellPr.Value = (System.Drawing.Image)Properties.Resources.Bullet_Green; 

      } 
      else 
      { 
       cellAtr.Value = (System.Drawing.Image)Properties.Resources.Bullet_Red; 

      } 

      if (poOk == true) 
      { 
       cellPo.Value = (System.Drawing.Image)Properties.Resources.Bullet_Green; 

      } 
      else 
      { 
       cellAtr.Value = (System.Drawing.Image)Properties.Resources.Bullet_Red; 
      } 

     } 

     MessageBox.Show("Done"); 
    } 

我不知道爲什麼,但似乎在DataGridView正確填寫,在圖像中設置的圖像列發出明確把它留給我在每個圖像單元


「空」從DJ KRAZE評論後,我用連接/關閉「演奏」(將其移出循環),但它並沒有真正改變任何東西。

綁定源(我使用的Visual C#GUI做到這一點對我來說)我xx.Designer.cs

partial class contractManage 
{ 
    /// <summary> 
    /// Variable nécessaire au concepteur. 
    /// </summary> 
    private System.ComponentModel.IContainer components = null; 

    /// <summary> 
    /// Nettoyage des ressources utilisées. 
    /// </summary> 
    /// <param name="disposing">true si les ressources managées doivent être supprimées ; sinon, false.</param> 
    protected override void Dispose(bool disposing) 
    { 
     if (disposing && (components != null)) 
     { 
      components.Dispose(); 
     } 
     base.Dispose(disposing); 
    } 

    #region Code généré par le Concepteur de composants 

    /// <summary> 
    /// Méthode requise pour la prise en charge du concepteur - ne modifiez pas 
    /// le contenu de cette méthode avec l'éditeur de code. 
    /// </summary> 
    private void InitializeComponent() 
    { 
     this.components = new System.ComponentModel.Container(); 
     System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle(); 
     System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle(); 
     this.tESTDataSet = new BombardierEngAdmin.TESTDataSet(); 
     this.tblContractsTableAdapter = new BombardierEngAdmin.TESTDataSetTableAdapters.tblContractsTableAdapter(); 
     this.dataGridViewContractManage = new System.Windows.Forms.DataGridView(); 
     this.tblContractsBindingSource = new System.Windows.Forms.BindingSource(this.components); 
     this.agencyDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); 
     this.endDateDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); 
     this.startDateDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); 
     this.contractTypeDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); 
     this.surnameDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); 
     this.firstNameDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); 
     this.contractStatusDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewImageColumn(); 
     this.contractIdDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); 
     ((System.ComponentModel.ISupportInitialize)(this.tESTDataSet)).BeginInit(); 
     ((System.ComponentModel.ISupportInitialize)(this.dataGridViewContractManage)).BeginInit(); 
     ((System.ComponentModel.ISupportInitialize)(this.tblContractsBindingSource)).BeginInit(); 
     this.SuspendLayout(); 
     // 
     // tESTDataSet 
     // 
     this.tESTDataSet.DataSetName = "TESTDataSet"; 
     this.tESTDataSet.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema; 
     // 
     // tblContractsTableAdapter 
     // 
     this.tblContractsTableAdapter.ClearBeforeFill = true; 
     // 
     // dataGridViewContractManage 
     // 
     this.dataGridViewContractManage.AllowUserToAddRows = false; 
     this.dataGridViewContractManage.AllowUserToDeleteRows = false; 
     this.dataGridViewContractManage.AllowUserToResizeRows = false; 
     this.dataGridViewContractManage.AutoGenerateColumns = false; 
     this.dataGridViewContractManage.BackgroundColor = System.Drawing.Color.White; 
     this.dataGridViewContractManage.BorderStyle = System.Windows.Forms.BorderStyle.None; 
     this.dataGridViewContractManage.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Single; 
     dataGridViewCellStyle1.Font = new System.Drawing.Font("Segoe UI", 8F); 
     this.dataGridViewContractManage.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1; 
     this.dataGridViewContractManage.ColumnHeadersHeight = 25; 
     this.dataGridViewContractManage.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.DisableResizing; 
     this.dataGridViewContractManage.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { 
     this.contractIdDataGridViewTextBoxColumn, 
     this.contractStatusDataGridViewTextBoxColumn, 
     this.firstNameDataGridViewTextBoxColumn, 
     this.surnameDataGridViewTextBoxColumn, 
     this.contractTypeDataGridViewTextBoxColumn, 
     this.startDateDataGridViewTextBoxColumn, 
     this.endDateDataGridViewTextBoxColumn, 
     this.agencyDataGridViewTextBoxColumn}); 
     this.dataGridViewContractManage.DataSource = this.tblContractsBindingSource; 
     dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft; 
     dataGridViewCellStyle2.BackColor = System.Drawing.SystemColors.Window; 
     dataGridViewCellStyle2.Font = new System.Drawing.Font("Segoe UI", 8F); 
     dataGridViewCellStyle2.ForeColor = System.Drawing.SystemColors.ControlText; 
     dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight; 
     dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText; 
     dataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.False; 
     this.dataGridViewContractManage.DefaultCellStyle = dataGridViewCellStyle2; 
     this.dataGridViewContractManage.EnableHeadersVisualStyles = false; 
     this.dataGridViewContractManage.GridColor = System.Drawing.Color.Gainsboro; 
     this.dataGridViewContractManage.Location = new System.Drawing.Point(4, 4); 
     this.dataGridViewContractManage.Name = "dataGridViewContractManage"; 
     this.dataGridViewContractManage.RowHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Single; 
     this.dataGridViewContractManage.RowHeadersDefaultCellStyle = dataGridViewCellStyle1; 
     this.dataGridViewContractManage.RowHeadersVisible = false; 
     this.dataGridViewContractManage.RowHeadersWidth = 20; 
     this.dataGridViewContractManage.RowTemplate.ReadOnly = true; 
     this.dataGridViewContractManage.RowTemplate.Resizable = System.Windows.Forms.DataGridViewTriState.False; 
     this.dataGridViewContractManage.Size = new System.Drawing.Size(897, 502); 
     this.dataGridViewContractManage.TabIndex = 1; 
     this.dataGridViewContractManage.CellContentDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridViewContractManage_CellContentDoubleClick); 
     // 
     // tblContractsBindingSource 
     // 
     this.tblContractsBindingSource.DataMember = "tblContracts"; 
     this.tblContractsBindingSource.DataSource = this.tESTDataSet; 
     // 
     // agencyDataGridViewTextBoxColumn 
     // 
     this.agencyDataGridViewTextBoxColumn.DataPropertyName = "Agency"; 
     this.agencyDataGridViewTextBoxColumn.HeaderText = "Agency"; 
     this.agencyDataGridViewTextBoxColumn.Name = "agencyDataGridViewTextBoxColumn"; 
     this.agencyDataGridViewTextBoxColumn.Width = 150; 
     // 
     // endDateDataGridViewTextBoxColumn 
     // 
     this.endDateDataGridViewTextBoxColumn.DataPropertyName = "EndDate"; 
     this.endDateDataGridViewTextBoxColumn.HeaderText = "End"; 
     this.endDateDataGridViewTextBoxColumn.Name = "endDateDataGridViewTextBoxColumn"; 
     this.endDateDataGridViewTextBoxColumn.Width = 80; 
     // 
     // startDateDataGridViewTextBoxColumn 
     // 
     this.startDateDataGridViewTextBoxColumn.DataPropertyName = "StartDate"; 
     this.startDateDataGridViewTextBoxColumn.HeaderText = "Start"; 
     this.startDateDataGridViewTextBoxColumn.Name = "startDateDataGridViewTextBoxColumn"; 
     this.startDateDataGridViewTextBoxColumn.Width = 80; 
     // 
     // contractTypeDataGridViewTextBoxColumn 
     // 
     this.contractTypeDataGridViewTextBoxColumn.DataPropertyName = "ContractType"; 
     this.contractTypeDataGridViewTextBoxColumn.HeaderText = "Type"; 
     this.contractTypeDataGridViewTextBoxColumn.Name = "contractTypeDataGridViewTextBoxColumn"; 
     this.contractTypeDataGridViewTextBoxColumn.Width = 150; 
     // 
     // surnameDataGridViewTextBoxColumn 
     // 
     this.surnameDataGridViewTextBoxColumn.DataPropertyName = "Surname"; 
     this.surnameDataGridViewTextBoxColumn.HeaderText = "Surname"; 
     this.surnameDataGridViewTextBoxColumn.Name = "surnameDataGridViewTextBoxColumn"; 
     // 
     // firstNameDataGridViewTextBoxColumn 
     // 
     this.firstNameDataGridViewTextBoxColumn.DataPropertyName = "FirstName"; 
     this.firstNameDataGridViewTextBoxColumn.HeaderText = "FirstName"; 
     this.firstNameDataGridViewTextBoxColumn.Name = "firstNameDataGridViewTextBoxColumn"; 
     // 
     // contractStatusDataGridViewTextBoxColumn 
     // 
     this.contractStatusDataGridViewTextBoxColumn.DataPropertyName = "ContractStatus"; 
     this.contractStatusDataGridViewTextBoxColumn.HeaderText = "Status"; 
     this.contractStatusDataGridViewTextBoxColumn.Name = "contractStatusDataGridViewTextBoxColumn"; 
     this.contractStatusDataGridViewTextBoxColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True; 
     this.contractStatusDataGridViewTextBoxColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic; 
     this.contractStatusDataGridViewTextBoxColumn.Width = 35; 
     // 
     // contractIdDataGridViewTextBoxColumn 
     // 
     this.contractIdDataGridViewTextBoxColumn.DataPropertyName = "ContractId"; 
     this.contractIdDataGridViewTextBoxColumn.HeaderText = "#"; 
     this.contractIdDataGridViewTextBoxColumn.Name = "contractIdDataGridViewTextBoxColumn"; 
     this.contractIdDataGridViewTextBoxColumn.ReadOnly = true; 
     this.contractIdDataGridViewTextBoxColumn.Width = 50; 
     // 
     // contractManage 
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
     this.BackColor = System.Drawing.Color.White; 
     this.Controls.Add(this.dataGridViewContractManage); 
     this.Name = "contractManage"; 
     this.Size = new System.Drawing.Size(921, 526); 
     ((System.ComponentModel.ISupportInitialize)(this.tESTDataSet)).EndInit(); 
     ((System.ComponentModel.ISupportInitialize)(this.dataGridViewContractManage)).EndInit(); 
     ((System.ComponentModel.ISupportInitialize)(this.tblContractsBindingSource)).EndInit(); 
     this.ResumeLayout(false); 

    } 

    #endregion 

    private TESTDataSet tESTDataSet; 
    private TESTDataSetTableAdapters.tblContractsTableAdapter tblContractsTableAdapter; 
    private System.Windows.Forms.DataGridView dataGridViewContractManage; 
    private System.Windows.Forms.DataGridViewTextBoxColumn contractIdDataGridViewTextBoxColumn; 
    private System.Windows.Forms.DataGridViewImageColumn contractStatusDataGridViewTextBoxColumn; 
    private System.Windows.Forms.DataGridViewTextBoxColumn firstNameDataGridViewTextBoxColumn; 
    private System.Windows.Forms.DataGridViewTextBoxColumn surnameDataGridViewTextBoxColumn; 
    private System.Windows.Forms.DataGridViewTextBoxColumn contractTypeDataGridViewTextBoxColumn; 
    private System.Windows.Forms.DataGridViewTextBoxColumn startDateDataGridViewTextBoxColumn; 
    private System.Windows.Forms.DataGridViewTextBoxColumn endDateDataGridViewTextBoxColumn; 
    private System.Windows.Forms.DataGridViewTextBoxColumn agencyDataGridViewTextBoxColumn; 
    private System.Windows.Forms.BindingSource tblContractsBindingSource; 
} 
} 
+0

布萊斯看看你foreach循環似乎你關閉連接,所以如果我沒有記錯的話,你可能會做錯事..還你在哪裏調用的DataGridView的DataBind()方法... ? – MethodMan 2013-02-11 14:21:11

回答

2

與您的代碼的問題是,你的圖像分配到不正確的定義屬性。您應該設置圖片屬性不是屬性。

您獲取空值的原因是因爲當您創建列時,將Image屬性設置爲null,然後永遠不會更改它。

**編輯**

嘗試這樣做吧。我已經做到了,每個對象的創建都在同一個上下文中。

 if (atrOk == true) 
     { 
      DataGridViewImageCell cellAtr = row.Cells[8] as DataGridViewImageCell; 
      cellAtr.Value = (System.Drawing.Image)Properties.Resources.Bullet_Green; 

     } 
     else 
     { 
      DataGridViewImageCell cellAtr = row.Cells[8] as DataGridViewImageCell; 
      cellAtr.Value = (System.Drawing.Image)Properties.Resources.Bullet_Red; 

     } 

     if (prOk == true) 
     { 
      DataGridViewImageCell cellPr = row.Cells[9] as DataGridViewImageCell; 
      cellPr.Value = (System.Drawing.Image)Properties.Resources.Bullet_Green; 

     } 
     else 
     { 
      DataGridViewImageCell cellAtr = row.Cells[8] as DataGridViewImageCell; 
      cellAtr.Value = (System.Drawing.Image)Properties.Resources.Bullet_Red; 

     } 

     if (poOk == true) 
     { 
      DataGridViewImageCell cellPo = row.Cells[10] as DataGridViewImageCell; 
      cellPo.Value = (System.Drawing.Image)Properties.Resources.Bullet_Green; 

     } 
     else 
     { 
      DataGridViewImageCell cellAtr = row.Cells[8] as DataGridViewImageCell; 
      cellAtr.Value = (System.Drawing.Image)Properties.Resources.Bullet_Red; 
     } 
+0

謝謝你Derek!我剛剛檢查過,這適用於我的DataGridViewImageColumn ... 其實我想在DataGridViewImageCell上的這個屬性!? – Brice 2013-02-11 15:21:03

+0

我已經更新了這個,你的代碼看起來是正確的,但是,嘗試在設置該值時在同一個上下文中聲明對象類型。我不相信它會起作用,但無論如何都要嘗試。 – Derek 2013-02-11 15:54:30

+1

感謝Derek的幫助,不幸的是,我仍然錯過了一些東西,因爲我沒有設法使您的解決方案工作。 我最終以編程方式生成了我的完整dataGridView;) 這不是一個解決方案,但我花了太多時間試圖調整我的東西... 謝謝 – Brice 2013-02-12 08:40:09