views:

16

answers:

1

I have a DataGridView with a column of text box cells that need to have watermark text. I also have a static class that uses DLLImport to add a watermark to a TextBox using SendMessage with a IntPtr hWnd as one of parameters, like this:

public static void SetWatermark(TextBox textBox, string watermarkText)
    {
        SendMessage(textBox.Handle, EM_SETCUEBANNER, 0, watermarkText);
    }

Problem is, while the TextBox inherits the Control.Handle property, DataGridViewTextBoxCell does not, so I can't get that IntPtr parameter. It is my understanding that the cell uses a TextBox control to edit the value, so shouldn't there be some way to get to that Handle?

I am using .NET 2.0 and C#

+1  A: 

If you want a reference to the TextBox for the cell currently being edited, you can use the DataGridView.EditingControl property on the DataGridView itself. Put your code in a handler for the EditingControlShowing event so that it is called whenever a new TextBox is shown.

Of course, you actually want the watermark on cells that aren't being edited, since the cell that is being edited has keyboard focus so the watermark will be hidden anyway. Cells that are not being edited do not have window handles and are painted entirely in managed code, so you cannot do this with SendMessage. You will need to create a subclass of DataGridViewCell and override Paint or handle the CellPainting event on the DataGridView.

Quartermeister