如何在网格视图的单元格中显示图标c#

时间:2012-06-14 03:42:32

标签: c# winforms datagridview icons

我想在特定单元格中显示图标。

我正在使用此代码。我已经评论了我想要显示图标的位置:

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;

2 个答案:

答案 0 :(得分:4)

实现此目的的一种方法是添加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;

答案 1 :(得分:0)

您可以使用DataGridViewImageColumn

来制作