如何在Component One FlexGrid的列中添加文本和图像?

时间:2014-04-28 11:59:45

标签: c#-4.0 using componentone c1flexgrid

我使用下面提到的代码段来显示带图片的文字。但是我无法用它显示图像。

是否无法从代码访问图像路径?

C1.Win.C1FlexGrid.C1FlexGrid gAuditL = new C1.Win.C1FlexGrid.C1FlexGrid();
.
.
.
gAuditL.DataSource = AuditLogVieweryDT;// this is datasource
for (int i = gAuditL.Row.Fixed; i < gAuditL.Rows.Count; i++)
//foreach  row in grid 
{
 string severity = gAuditL[i, gAuditL.Cols["Severity"].Index].ToString();
 if (severity == "Information")
 {
   this.gAuditL.SetCellImage(i, 0,Image.FromFile(@".\\Resources\information.bmp"));
   this.gAuditL.SetData(i, 0, "Information");
 }
 if (severity == "Warning")
 {
   this.gAuditL.SetCellImage(i, 0, Image.FromFile(@".\\Resources\warning.bmp"));
   this.gAuditL.SetData(i, 0, "Warning");
 }
 if (severity == "Critical")
 {
   this.gAuditL.SetCellImage(i, 0, Image.FromFile(@".\\Resources\critical.bmp"));
   this.gAuditL.SetData(i, 0, "Critical");
 }
 if (severity == "Unspecified")
 {
   this.gAuditL.SetCellImage(i, 0, Image.FromFile(@".\\Resources\unspecified.bmp"));
   this.gAuditL.SetData(i, 0, "Unspecified");
 }

 this.gAuditL.Styles.Normal.ImageAlign = C1.Win.C1FlexGrid.ImageAlignEnum.LeftCenter;
 this.gAuditL.Styles.Normal.TextAlign = C1.Win.C1FlexGrid.TextAlignEnum.RightCenter;
} 

1 个答案:

答案 0 :(得分:1)

请参阅此。( OP发布的答案

namespace SampleProject.Forms.Maintenance
{

public partial class SampleProject: Form
{

    Image img1, img2, img3, img4;// declare member variable

    //Load Event
      private void AuditLogViewer_Load(object sender, EventArgs e)
    {
        object information = Resources.ResourceManager.GetObject("information"); //Return an object from the image chan1.png in the project
        img1 = (Image)information;
        object Warning = Resources.ResourceManager.GetObject("warning"); //Return an object from the image chan1.png in the project
        img2 = (Image)Warning;
        object critical = Resources.ResourceManager.GetObject("critical"); //Return an object from the image chan1.png in the project
        img3 = (Image)critical;
        object unspecified = Resources.ResourceManager.GetObject("unspecified"); //Return an object from the image chan1.png in the project
        img4 = (Image)unspecified;
    }

 //Grid Click Event
    private void grdAuditLogs_OwnerDrawCell(object sender, OwnerDrawCellEventArgs e)
    {
        if (e.Col == 2)
        {
            //let the grid paint the background and border for the cell
            e.DrawCell(C1.Win.C1FlexGrid.DrawCellFlags.Background | C1.Win.C1FlexGrid.DrawCellFlags.Border);

            //find text width
            var width = (int)e.Graphics.MeasureString(e.Text, e.Style.Font).Width;

            //x-coordinate for each image
            var img1_x = e.Bounds.X + width + 10;
            var img2_x = e.Bounds.X + width + 10;
            var img3_x = e.Bounds.X + width + 10;
            var img4_x = e.Bounds.X + width + 10;

            //var img3_x = img2_x + img2.Width + 5;

            //location for each image
            var img1_loc = new Point(img1_x, e.Bounds.Y + img1.Height - 18);
            var img2_loc = new Point(img2_x, e.Bounds.Y + img2.Height - 18);
            var img3_loc = new Point(img3_x, e.Bounds.Y + img3.Height - 18);
            var img4_loc = new Point(img4_x, e.Bounds.Y + img4.Height - 18);


            //draw images at aforementioned points
            if (grdAuditLogs[e.Row, grdAuditLogs.Cols["Severity"].Index].ToString() == "Information")
                e.Graphics.DrawImage(img1, img1_loc);
            if (grdAuditLogs[e.Row, grdAuditLogs.Cols["Severity"].Index].ToString() == "Warning")
                e.Graphics.DrawImage(img2, img2_loc);
            if (grdAuditLogs[e.Row, grdAuditLogs.Cols["Severity"].Index].ToString() == "Critical")
                e.Graphics.DrawImage(img3, img3_loc);
            if (grdAuditLogs[e.Row, grdAuditLogs.Cols["Severity"].Index].ToString() == "Unspecified")
                e.Graphics.DrawImage(img4, img4_loc);

            //e1.Graphics.DrawImage(img3, img3_loc);

            //draw text
            e.Graphics.DrawString(e.Text, e.Style.Font, Brushes.Black, e.Bounds.Location);
            e.Handled = true;
        }
    }