我需要你在这个问题上的专业知识: 我有一个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是/否,PrOk是/否,PoOk是/否
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正确填充,设置图像列中的图像发送清除它在每个图像单元格中留下“null”
在DJ Kraze的评论之后,我已经“玩”了连接/关闭(将其移出循环)但它并没有真正改变任何东西。
我的xx.Designer.cs中定义了绑定源(我使用Visual C#GUI为我做了)
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 :(得分: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;
}