2

I need to show a placeholder which contains a string in first two columns of the first row of datagrid view in Winforms. The placeholder is to be displayed when the datagrid is empty.

enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
biff
  • 79
  • 1
  • 2
  • 13
  • Do you really mean a Label Control? Or do you simply want to set the text value of the two Cells?? – TaW Nov 03 '16 at 08:55
  • Actually i'm trying to place the label as a placeholder in those cells.so that if the datagridview is empty i can display it on those cells. – biff Nov 03 '16 at 09:01
  • Please change the question title and descriptions based on your new comment. You want to draw place holder text for first 2 columns. Also it seems you don't need a click on button, you just want to show them on cells when the cell is empty. – Reza Aghaei Nov 03 '16 at 09:10
  • Include a clear condition for the DGV being 'empty' ! – TaW Nov 03 '16 at 09:18
  • if there is no data in DGV then the placeholder text(eg: HH:MM:SS) must be displayed in the 2 cells of first row,when the user enters value there placeholder text must be gone. – biff Nov 03 '16 at 09:23

3 Answers3

5

You need to handle CellPainting event and draw place holder yourself:

void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.RowIndex < 0 || e.ColumnIndex < 0)        /*If a header cell*/
        return;
    if (!(e.ColumnIndex == 0 || e.ColumnIndex == 1) /*If not our desired columns*/
        return;

    if(e.Value == null || e.Value == DBNull.Value)  /*If value is null*/
    {
        e.Paint(e.CellBounds, DataGridViewPaintParts.All
            & ~(DataGridViewPaintParts.ContentForeground));

        TextRenderer.DrawText(e.Graphics, "Enter a value", e.CellStyle.Font,
            e.CellBounds, SystemColors.GrayText, TextFormatFlags.Left);

        e.Handled = true;
    }
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
0

So, you can improve this (work for Textbox) and change for dataGrid.Text:

Textbox myTxtbx = new Textbox();
myTxtbx.Text = "Enter text here...";

myTxtbx.GotFocus += GotFocus.EventHandle(RemoveText);
myTxtbx.LostFocus += LostFocus.EventHandle(AddText);

public void RemoveText(object sender, EventArgs e)
{
     if (myTxtbx.Text == "Enter text here...") {
        myTxtbx.Text = "";
     }
}

public void AddText(object sender, EventArgs e)
{
     if(String.IsNullOrWhiteSpace(myTxtbx.Text))
        myTxtbx.Text = "Enter text here...";
}

Note: in myTxtbx.Text = "Enter text here..."; and if (myTxtbx.Text == "Enter text here...") the string "Enter text here..." must be equal.

Marco Bagiacchi
  • 334
  • 2
  • 20
0

Just wanted to share my flavor of @Reza Aghaei's amazing answer. It will replace all nulls with the word "NULL" in italics, similar to how SQL Server Management Studio does it.

private void MainGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.RowIndex < 0 || e.ColumnIndex < 0)
        return;

    if (e.Value == null || e.Value == DBNull.Value)
    {
        e.Paint(e.CellBounds, DataGridViewPaintParts.All
                & ~(DataGridViewPaintParts.ContentForeground));

        var font = new Font(e.CellStyle.Font, FontStyle.Italic);
        var color = SystemColors.ActiveCaptionText;
        if (((DataGridView)sender).SelectedCells.Contains(((DataGridView)sender)[e.ColumnIndex, e.RowIndex]))
                color = Color.White;

        TextRenderer.DrawText(e.Graphics, "NULL", font, e.CellBounds, color, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);

        e.Handled = true;
    }
}
Sal
  • 5,129
  • 5
  • 27
  • 53