Hi all,

I have created a custom datagridview cell type (which allows the user to either enter text or add an image). This is working as I had planned, with one exception...

This new cell type, which is set as the CellTemplate, has a button to the right of the cell which, when clicked, allows the user to browse for the image and it is displayed in the cell. However if the row is a new row (isNewRow) it still added the image, but does not create a new "new row" underneath. Its as if the row with the image in is still a clean, new row.

Doing some googling I see that there is a value NotifyCurrentCellDirty, but this seems to only be accessible at the form level, not the cell.

So how do I, from within the custom cell code, mark the row as dirty so that a new row is created underneath? Is marking it as dirty actually the correct thing to do? How does the standard text cell create a new row when you start typing in it?

thanks
Chris

Hi guys,

sorry I really need help on this. I keep trying different things but just cant seem to work.

Does anyone know how the standard text edit cell indicates that it is editing which causes that row to become active and a new row to be added?

If it helps here is most of the code for my custom control:

public class DataGridViewTextImageCell : DataGridViewTextBoxCell
    {
        Image img;
        Rectangle newRect;
        //DataGridView parentDGV;

        protected override void Paint(
            Graphics graphics,
            Rectangle clipBounds,
            Rectangle cellBounds,
            int rowIndex,
            DataGridViewElementStates cellState,
            object value,
            object formattedValue,
            string errorText,
            DataGridViewCellStyle cellStyle,
            DataGridViewAdvancedBorderStyle advancedBorderStyle,
            DataGridViewPaintParts paintParts)
        {
            // Call the base class method to paint the default cell appearance.
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState,
                value, formattedValue, errorText, cellStyle,
                advancedBorderStyle, paintParts);

            /* If there is no text display an image browser button */
            if ( ( value == null ) && ( img == null ) )
            {
                Bitmap insertImg = Properties.Resources.insertImage;
                newRect = new Rectangle(cellBounds.X + cellBounds.Width - 20,
                cellBounds.Y + cellBounds.Height - 20, 16, 16);
                graphics.DrawImage(insertImg, newRect);
            }
            else if ((value == null) && (img != null))
            {
                /* Draw image in cell. First calculate the location and size of image */
                newRect = new Rectangle(cellBounds.X + 5, cellBounds.Y + 5, img.Width, img.Height);
                /* Draw the actual image */
                graphics.DrawImage(img, newRect);
                
                /*resize cell heigth to fit image */
                //this.OwningRow.Height = img.Height + 10; // This is currently done when the image is added!
            }
        }

        protected override void OnClick(DataGridViewCellEventArgs e)
        {
            /* Do base onClick event */
            base.OnClick(e);

            /* If there is no text or image already in the cell... */
            if ((this.Value == null) && (this.img == null))
            {
                /* Get mouse location */
                Point cursorPosition = this.DataGridView.PointToClient(Cursor.Position);

                /* If mouse is within image rectangle... */
                if (newRect.Contains(cursorPosition))
                {
                    OpenFileDialog ofd = new OpenFileDialog();
                    ofd.Filter = "Supported Files (*.bmp, *.jpg, *.gif)|*.bmp;*.jpg;*.gif";
                    ofd.Title = "Select Image to Import";

                    // Show the Dialog.

                    if (ofd.ShowDialog() == DialogResult.OK)
                    {
                        /* If the row doesnt exist (ie its the default last row) add it before populating */
                        if (this.OwningRow.IsNewRow)
                        {
                            // I THINK I NEED TO INDICATE EDIT HERE AND ACTIVATE CURRENT ROW, AND CREATE NEW ONE.
                        }

                        /* Resize the image - this will only happen if it is too big. It will
                         * also create a copy of the resulting image in the projects image folder.*/
                        ResizeImage(ofd.FileName,"c:\\temp\\" + System.IO.Path.GetFileName(ofd.FileName), 500, 1000, true);

                        /*  */
                        img = Image.FromFile("c:\\temp\\" + System.IO.Path.GetFileName(ofd.FileName));

                        this.OwningRow.Height = img.Height + 10;
                    }
                }
            }
        }

        public void ResizeImage(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)
        {
	        //Contents not important for this problem
        }
    }

    public class DataGridViewTextImageCellColumn : DataGridViewColumn
    {
        public DataGridViewTextImageCellColumn()
        {
            this.CellTemplate = new DataGridViewTextImageCell();
        }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.