在DataGridView单元格中显示占位符文本

时间:2016-11-03 08:34:25

标签: c# winforms visual-studio datagridview property-placeholder

我需要在Winforms中显示一个占位符,该占位符包含第一行datagrid视图的前两列中的字符串。当datagrid为空时,将显示占位符。

enter image description here

3 个答案:

答案 0 :(得分:3)

您需要处理CellPainting事件并自行绘制占位符:

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

答案 1 :(得分:0)

因此,您可以改善这一点(适用于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..."; }

myTxtbx.Text = "Enter text here...";

注意:if (myTxtbx.Text == "Enter text here...")import UIKit import Foundation class ViewControllerCredits: UIViewController { @IBOutlet weak var version: UILabel! @IBOutlet weak var developers: UILabel! @IBOutlet weak var company: UILabel! @IBOutlet weak var contact: UILabel! @IBOutlet weak var disclaimer: UILabel! override func viewDidLoad() { super.viewDidLoad() getData() // Do any additional setup after loading the view } func getData(){ let url = NSURL(string: "http://192.168.178.1:8888/credits/credits.json") URLSession.shared.dataTask(with: url! as URL){(data, response, error) in if error != nil { print(error) return } do{ let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) for dictionary in json as! [[String: Any]]{ print(dictionary["developers"]) } print(json) }catch let jsonError{ print(jsonError) } }.resume() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 字符串“在此处输入文字...”必须相等。

答案 2 :(得分:0)

只是想分享我对@Reza Aghaei 惊人答案的看法。它将用斜体字“NULL”替换所有空值,类似于 SQL Server Management Studio 的做法。

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