2012-06-14 41 views
0

我想在特定单元格中显示图标。如何在网格视图c的单元格中显示图标#

我正在使用此代码。我评论,我想显示的图标:

string lastScripDate = this.GetLastScripDate(sFolderPath, pb); 
string sQuery = "select * from database"; 
DataTable table = new DataTable(); 
table = this.Retrieve_Data(sQuery, "database"); 
pb.Maximum = table.Rows.Count; 
pb.Value = 0; 
double num = 0.0; 
double num2 = 0.0; 
double num3 = 0.0; 
double num4 = 0.0; 
if (table.Rows.Count > 0) 
{ 
    for (int i = 0; i < table.Rows.Count; i++) 
    { 
     Application.DoEvents(); 
     dgv.Rows.Add(); 
     dgv.Rows[i].Cells["Sname"].Value = table.Rows[i]["Symbol"].ToString(); 
     dgv.Rows[i].Cells["clsprice"].Value = table.Rows[i]["SCRIP"].ToString(); 
     string str3 = table.Rows[i]["TradeType"].ToString(); 
     dgv.Rows[i].Cells["trd"].Value = str3; 
     string str4 = str3; 
     if (str4 != null) 
     { 
      if (!(str4 == "p")) 
      { 
       if (str4 == "q") 
       { 
        goto Label_01DD; 
       } 
      } 
      else 
      { 
       dgv.Rows[i].Cells["trd"].Style.ForeColor = Color.Green;///////I Want to Display Icon in this Cell(Up.Ico) 
      } 
     } 
     goto Label_020B; 
    Label_01DD: 

     dgv.Rows[i].Cells["Trade"].Style.ForeColor = Color.Red;///////////////An Want to Display Icon in this Cell also(Down.Ico 

    Label_020B: 
    dgv.Rows[i].Cells["date"].Value = table.Rows[i]["TDate"].ToString(); 
     num = Convert.ToDouble(table.Rows[i]["CPrice"]); 
     dgv.Rows[i].Cells["tp"].Value = string.Format("{0:0.00}", num); 
     num2 = Convert.ToDouble(table.Rows[i]["Price"]); 
     dgv.Rows[i].Cells["tp"].Value = string.Format("{0:0.00}", num2); 
     num3 = Convert.ToDouble(table.Rows[i]["Loss"]); 
     dgv.Rows[i].Cells["sl"].Value = string.Format("{0:0.00}", num3); 
     try 
     { 
      num4 = Convert.ToDouble(table.Rows[i]["Ratio"]); 
      dgv.Rows[i].Cells["Ratio"].Value = string.Format("{0:0.00}", num4); 
     } 
     catch 
     { } 
     pb.Value++; 
    } 
    if (pb.Value == pb.Maximum) 
    { 
     pb.Value = pb.Minimum; 
    } 
    return true; 
} 
return false; 
+1

对于将来的问题,请考虑提供显着较小的代码示例(通常10行代码就足够了)。避免在为其他人阅读的代码中使用“goto”,因为它的使用通常会被忽视,并且可能会减少获得有针对性的答案的机会。 –

+0

**转到**不是好模式。 – Kiquenet

回答

2

的一种方式,以达致这将是添加DataGridViewImageColumn使其然后设置图标到该小区。下面是一个示例

 dataGridView1.Columns.Add(new DataGridViewImageColumn()); 
     dataGridView1.Rows.Add(2); 

     DataGridViewImageCell cell = (DataGridViewImageCell)dataGridView1.Rows[0].Cells[0]; 

     Icon ic = new Icon("icon.ico"); 

     cell.Value = ic; 
相关问题