Search code examples
c#-4.0usingcomponentonec1flexgrid

How to add text and image in a column in Component One FlexGrid?


I have used the below mentioned snippet to show text with image. However I am unable to display image with it.

Is the path to image not accessible from code?

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;
} 

Solution

  • Please refer this.(Answer posted by 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;
            }
        }