How to format a specific item in the ComboBox in a DataGridViewComboBoxColumn?












2















I have code similar to this in a method that (re)creates the columns in the DataGridView:



MyColumn = new DataGridViewComboBoxColumn()
{
Name = "..",
HeaderText = "..",
SortMode = DataGridViewColumnSortMode.NotSortable
};
MyColumn.Items.Clear();
foreach (string s in MyStringList)
{
MyColumn.Items.Add(s);
}
MyColumn.Items.Add("");
// I would like this empty string to be shown as "No group"
// with an italic grayed out font


I think that I probably must create a class for the items of the ComboBox-es in the column, in which I should override the ToString() method, but I want to know how to format the No Group item.



A related question is here which is a about a normal ComboBox not inside a DataGridView, with the answer solving the problem using the DrawMode prperty and DrawItem event of the ComboBox class.










share|improve this question

























  • Why not MyColumn.Items.Add("No Group"); ?

    – Chetan Ranpariya
    Jan 1 at 12:51











  • Thank you for your comment. I updated the question with "No Group" as the text that I want to format.

    – silviubogan
    Jan 1 at 13:00











  • MyColumn.DisplayStyle is DropDownButton so you should be able to use ComboBoxRenderer class for DrawDropDownButton as explained here stackoverflow.com/a/7486187/2516718

    – derloopkat
    Jan 1 at 14:26













  • @derloopkat Thank you. This solves my problem partially, but I am looking for a way to paint the drop-down menus of the ComboBox-es inside a ComboBox column, besides the painting of the closed ComboBox-es.

    – silviubogan
    Jan 1 at 15:02
















2















I have code similar to this in a method that (re)creates the columns in the DataGridView:



MyColumn = new DataGridViewComboBoxColumn()
{
Name = "..",
HeaderText = "..",
SortMode = DataGridViewColumnSortMode.NotSortable
};
MyColumn.Items.Clear();
foreach (string s in MyStringList)
{
MyColumn.Items.Add(s);
}
MyColumn.Items.Add("");
// I would like this empty string to be shown as "No group"
// with an italic grayed out font


I think that I probably must create a class for the items of the ComboBox-es in the column, in which I should override the ToString() method, but I want to know how to format the No Group item.



A related question is here which is a about a normal ComboBox not inside a DataGridView, with the answer solving the problem using the DrawMode prperty and DrawItem event of the ComboBox class.










share|improve this question

























  • Why not MyColumn.Items.Add("No Group"); ?

    – Chetan Ranpariya
    Jan 1 at 12:51











  • Thank you for your comment. I updated the question with "No Group" as the text that I want to format.

    – silviubogan
    Jan 1 at 13:00











  • MyColumn.DisplayStyle is DropDownButton so you should be able to use ComboBoxRenderer class for DrawDropDownButton as explained here stackoverflow.com/a/7486187/2516718

    – derloopkat
    Jan 1 at 14:26













  • @derloopkat Thank you. This solves my problem partially, but I am looking for a way to paint the drop-down menus of the ComboBox-es inside a ComboBox column, besides the painting of the closed ComboBox-es.

    – silviubogan
    Jan 1 at 15:02














2












2








2


0






I have code similar to this in a method that (re)creates the columns in the DataGridView:



MyColumn = new DataGridViewComboBoxColumn()
{
Name = "..",
HeaderText = "..",
SortMode = DataGridViewColumnSortMode.NotSortable
};
MyColumn.Items.Clear();
foreach (string s in MyStringList)
{
MyColumn.Items.Add(s);
}
MyColumn.Items.Add("");
// I would like this empty string to be shown as "No group"
// with an italic grayed out font


I think that I probably must create a class for the items of the ComboBox-es in the column, in which I should override the ToString() method, but I want to know how to format the No Group item.



A related question is here which is a about a normal ComboBox not inside a DataGridView, with the answer solving the problem using the DrawMode prperty and DrawItem event of the ComboBox class.










share|improve this question
















I have code similar to this in a method that (re)creates the columns in the DataGridView:



MyColumn = new DataGridViewComboBoxColumn()
{
Name = "..",
HeaderText = "..",
SortMode = DataGridViewColumnSortMode.NotSortable
};
MyColumn.Items.Clear();
foreach (string s in MyStringList)
{
MyColumn.Items.Add(s);
}
MyColumn.Items.Add("");
// I would like this empty string to be shown as "No group"
// with an italic grayed out font


I think that I probably must create a class for the items of the ComboBox-es in the column, in which I should override the ToString() method, but I want to know how to format the No Group item.



A related question is here which is a about a normal ComboBox not inside a DataGridView, with the answer solving the problem using the DrawMode prperty and DrawItem event of the ComboBox class.







c# .net winforms datagridview datagridviewcolumn






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 25 at 16:23









Community

11




11










asked Jan 1 at 12:43









silviubogansilviubogan

1,08111127




1,08111127













  • Why not MyColumn.Items.Add("No Group"); ?

    – Chetan Ranpariya
    Jan 1 at 12:51











  • Thank you for your comment. I updated the question with "No Group" as the text that I want to format.

    – silviubogan
    Jan 1 at 13:00











  • MyColumn.DisplayStyle is DropDownButton so you should be able to use ComboBoxRenderer class for DrawDropDownButton as explained here stackoverflow.com/a/7486187/2516718

    – derloopkat
    Jan 1 at 14:26













  • @derloopkat Thank you. This solves my problem partially, but I am looking for a way to paint the drop-down menus of the ComboBox-es inside a ComboBox column, besides the painting of the closed ComboBox-es.

    – silviubogan
    Jan 1 at 15:02



















  • Why not MyColumn.Items.Add("No Group"); ?

    – Chetan Ranpariya
    Jan 1 at 12:51











  • Thank you for your comment. I updated the question with "No Group" as the text that I want to format.

    – silviubogan
    Jan 1 at 13:00











  • MyColumn.DisplayStyle is DropDownButton so you should be able to use ComboBoxRenderer class for DrawDropDownButton as explained here stackoverflow.com/a/7486187/2516718

    – derloopkat
    Jan 1 at 14:26













  • @derloopkat Thank you. This solves my problem partially, but I am looking for a way to paint the drop-down menus of the ComboBox-es inside a ComboBox column, besides the painting of the closed ComboBox-es.

    – silviubogan
    Jan 1 at 15:02

















Why not MyColumn.Items.Add("No Group"); ?

– Chetan Ranpariya
Jan 1 at 12:51





Why not MyColumn.Items.Add("No Group"); ?

– Chetan Ranpariya
Jan 1 at 12:51













Thank you for your comment. I updated the question with "No Group" as the text that I want to format.

– silviubogan
Jan 1 at 13:00





Thank you for your comment. I updated the question with "No Group" as the text that I want to format.

– silviubogan
Jan 1 at 13:00













MyColumn.DisplayStyle is DropDownButton so you should be able to use ComboBoxRenderer class for DrawDropDownButton as explained here stackoverflow.com/a/7486187/2516718

– derloopkat
Jan 1 at 14:26







MyColumn.DisplayStyle is DropDownButton so you should be able to use ComboBoxRenderer class for DrawDropDownButton as explained here stackoverflow.com/a/7486187/2516718

– derloopkat
Jan 1 at 14:26















@derloopkat Thank you. This solves my problem partially, but I am looking for a way to paint the drop-down menus of the ComboBox-es inside a ComboBox column, besides the painting of the closed ComboBox-es.

– silviubogan
Jan 1 at 15:02





@derloopkat Thank you. This solves my problem partially, but I am looking for a way to paint the drop-down menus of the ComboBox-es inside a ComboBox column, besides the painting of the closed ComboBox-es.

– silviubogan
Jan 1 at 15:02












1 Answer
1






active

oldest

votes


















0














For custom-painting the ComboBox, you need to handle EditingControlShowing and then get the EditingControl which is DataGridViewComboBoxEditingControl and then set its DrawMode to OwnerDrawFixed and handle its DrawItem event.



For custom-painting the cell, you need to handle CellPainting event and set different font and color for the cell styles and let the paint continue with new values. You can also paint the whole cell if you want.



enter image description here



Example



Load Sample Data:



private DataTable LoadProducts()
{
var dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("CategoryId", typeof(int));
dt.Rows.Add("P1", 1);
dt.Rows.Add("P2", 1);
dt.Rows.Add("P3", DBNull.Value);
return dt;
}
private DataTable LoadCategories()
{
var dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Name");
dt.Rows.Add(DBNull.Value, "No Category");
dt.Rows.Add(1, "C1");
dt.Rows.Add(2, "C2");
dt.Rows.Add(2, "C3");
return dt;
}


Setup DataGridView Columnms:



private void Form1_Load(object sender, EventArgs e)
{
var products = LoadProducts();
var categories = LoadCategories();

dataGridView1.Columns.Add(new DataGridViewTextBoxColumn()
{
Name = "NameColumn",
DataPropertyName = "Name",
HeaderText = "Name"
});
dataGridView1.Columns.Add(new DataGridViewComboBoxColumn()
{
Name = "CategoryIdColumn",
DataPropertyName = "CategoryId",
HeaderText = "Category",
DataSource = categories,
ValueMember = "Id",
DisplayMember = "Name",
DisplayStyle= DataGridViewComboBoxDisplayStyle.Nothing
});
dataGridView1.DataSource = products;
dataGridView1.EditingControlShowing += DataGridView1_EditingControlShowing;
dataGridView1.CellPainting += DataGridView1_CellPainting;
}


Handle EditingControlShowing



private void DataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1?.CurrentCell?.OwningColumn?.Name != "CategoryIdColumn")
return;
var combo = e.Control as DataGridViewComboBoxEditingControl;
if (combo == null)
return;

combo.DrawMode = DrawMode.OwnerDrawFixed;
combo.DrawItem += (obj, args) =>
{
var txt = args.Index >= 0 ? combo.GetItemText(combo.Items[args.Index]) : "";
var textColor = args.Index == 0 ? SystemColors.GrayText : SystemColors.ControlText;
var font = args.Index == 0 ? new Font(combo.Font, FontStyle.Italic) : combo.Font;
if ((args.State & DrawItemState.Selected) == DrawItemState.Selected)
{
textColor = SystemColors.HighlightText;
}
args.DrawBackground();
TextRenderer.DrawText(args.Graphics, txt, font,
args.Bounds, textColor,
TextFormatFlags.VerticalCenter | TextFormatFlags.Left);
};
}


Handle CellPainting



private void DataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex < 0 || e.RowIndex < 0 ||
dataGridView1.Columns[e.ColumnIndex].Name != "CategoryIdColumn")
return;
if (dataGridView1[e.ColumnIndex, e.RowIndex].Value == DBNull.Value)
{
e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Italic);
e.CellStyle.ForeColor = SystemColors.GrayText;
}
else
{
e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Regular);
e.CellStyle.ForeColor = SystemColors.ControlText;
}
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995531%2fhow-to-format-a-specific-item-in-the-combobox-in-a-datagridviewcomboboxcolumn%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    For custom-painting the ComboBox, you need to handle EditingControlShowing and then get the EditingControl which is DataGridViewComboBoxEditingControl and then set its DrawMode to OwnerDrawFixed and handle its DrawItem event.



    For custom-painting the cell, you need to handle CellPainting event and set different font and color for the cell styles and let the paint continue with new values. You can also paint the whole cell if you want.



    enter image description here



    Example



    Load Sample Data:



    private DataTable LoadProducts()
    {
    var dt = new DataTable();
    dt.Columns.Add("Name");
    dt.Columns.Add("CategoryId", typeof(int));
    dt.Rows.Add("P1", 1);
    dt.Rows.Add("P2", 1);
    dt.Rows.Add("P3", DBNull.Value);
    return dt;
    }
    private DataTable LoadCategories()
    {
    var dt = new DataTable();
    dt.Columns.Add("Id", typeof(int));
    dt.Columns.Add("Name");
    dt.Rows.Add(DBNull.Value, "No Category");
    dt.Rows.Add(1, "C1");
    dt.Rows.Add(2, "C2");
    dt.Rows.Add(2, "C3");
    return dt;
    }


    Setup DataGridView Columnms:



    private void Form1_Load(object sender, EventArgs e)
    {
    var products = LoadProducts();
    var categories = LoadCategories();

    dataGridView1.Columns.Add(new DataGridViewTextBoxColumn()
    {
    Name = "NameColumn",
    DataPropertyName = "Name",
    HeaderText = "Name"
    });
    dataGridView1.Columns.Add(new DataGridViewComboBoxColumn()
    {
    Name = "CategoryIdColumn",
    DataPropertyName = "CategoryId",
    HeaderText = "Category",
    DataSource = categories,
    ValueMember = "Id",
    DisplayMember = "Name",
    DisplayStyle= DataGridViewComboBoxDisplayStyle.Nothing
    });
    dataGridView1.DataSource = products;
    dataGridView1.EditingControlShowing += DataGridView1_EditingControlShowing;
    dataGridView1.CellPainting += DataGridView1_CellPainting;
    }


    Handle EditingControlShowing



    private void DataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
    if (dataGridView1?.CurrentCell?.OwningColumn?.Name != "CategoryIdColumn")
    return;
    var combo = e.Control as DataGridViewComboBoxEditingControl;
    if (combo == null)
    return;

    combo.DrawMode = DrawMode.OwnerDrawFixed;
    combo.DrawItem += (obj, args) =>
    {
    var txt = args.Index >= 0 ? combo.GetItemText(combo.Items[args.Index]) : "";
    var textColor = args.Index == 0 ? SystemColors.GrayText : SystemColors.ControlText;
    var font = args.Index == 0 ? new Font(combo.Font, FontStyle.Italic) : combo.Font;
    if ((args.State & DrawItemState.Selected) == DrawItemState.Selected)
    {
    textColor = SystemColors.HighlightText;
    }
    args.DrawBackground();
    TextRenderer.DrawText(args.Graphics, txt, font,
    args.Bounds, textColor,
    TextFormatFlags.VerticalCenter | TextFormatFlags.Left);
    };
    }


    Handle CellPainting



    private void DataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
    if (e.ColumnIndex < 0 || e.RowIndex < 0 ||
    dataGridView1.Columns[e.ColumnIndex].Name != "CategoryIdColumn")
    return;
    if (dataGridView1[e.ColumnIndex, e.RowIndex].Value == DBNull.Value)
    {
    e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Italic);
    e.CellStyle.ForeColor = SystemColors.GrayText;
    }
    else
    {
    e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Regular);
    e.CellStyle.ForeColor = SystemColors.ControlText;
    }
    }





    share|improve this answer




























      0














      For custom-painting the ComboBox, you need to handle EditingControlShowing and then get the EditingControl which is DataGridViewComboBoxEditingControl and then set its DrawMode to OwnerDrawFixed and handle its DrawItem event.



      For custom-painting the cell, you need to handle CellPainting event and set different font and color for the cell styles and let the paint continue with new values. You can also paint the whole cell if you want.



      enter image description here



      Example



      Load Sample Data:



      private DataTable LoadProducts()
      {
      var dt = new DataTable();
      dt.Columns.Add("Name");
      dt.Columns.Add("CategoryId", typeof(int));
      dt.Rows.Add("P1", 1);
      dt.Rows.Add("P2", 1);
      dt.Rows.Add("P3", DBNull.Value);
      return dt;
      }
      private DataTable LoadCategories()
      {
      var dt = new DataTable();
      dt.Columns.Add("Id", typeof(int));
      dt.Columns.Add("Name");
      dt.Rows.Add(DBNull.Value, "No Category");
      dt.Rows.Add(1, "C1");
      dt.Rows.Add(2, "C2");
      dt.Rows.Add(2, "C3");
      return dt;
      }


      Setup DataGridView Columnms:



      private void Form1_Load(object sender, EventArgs e)
      {
      var products = LoadProducts();
      var categories = LoadCategories();

      dataGridView1.Columns.Add(new DataGridViewTextBoxColumn()
      {
      Name = "NameColumn",
      DataPropertyName = "Name",
      HeaderText = "Name"
      });
      dataGridView1.Columns.Add(new DataGridViewComboBoxColumn()
      {
      Name = "CategoryIdColumn",
      DataPropertyName = "CategoryId",
      HeaderText = "Category",
      DataSource = categories,
      ValueMember = "Id",
      DisplayMember = "Name",
      DisplayStyle= DataGridViewComboBoxDisplayStyle.Nothing
      });
      dataGridView1.DataSource = products;
      dataGridView1.EditingControlShowing += DataGridView1_EditingControlShowing;
      dataGridView1.CellPainting += DataGridView1_CellPainting;
      }


      Handle EditingControlShowing



      private void DataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
      {
      if (dataGridView1?.CurrentCell?.OwningColumn?.Name != "CategoryIdColumn")
      return;
      var combo = e.Control as DataGridViewComboBoxEditingControl;
      if (combo == null)
      return;

      combo.DrawMode = DrawMode.OwnerDrawFixed;
      combo.DrawItem += (obj, args) =>
      {
      var txt = args.Index >= 0 ? combo.GetItemText(combo.Items[args.Index]) : "";
      var textColor = args.Index == 0 ? SystemColors.GrayText : SystemColors.ControlText;
      var font = args.Index == 0 ? new Font(combo.Font, FontStyle.Italic) : combo.Font;
      if ((args.State & DrawItemState.Selected) == DrawItemState.Selected)
      {
      textColor = SystemColors.HighlightText;
      }
      args.DrawBackground();
      TextRenderer.DrawText(args.Graphics, txt, font,
      args.Bounds, textColor,
      TextFormatFlags.VerticalCenter | TextFormatFlags.Left);
      };
      }


      Handle CellPainting



      private void DataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
      {
      if (e.ColumnIndex < 0 || e.RowIndex < 0 ||
      dataGridView1.Columns[e.ColumnIndex].Name != "CategoryIdColumn")
      return;
      if (dataGridView1[e.ColumnIndex, e.RowIndex].Value == DBNull.Value)
      {
      e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Italic);
      e.CellStyle.ForeColor = SystemColors.GrayText;
      }
      else
      {
      e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Regular);
      e.CellStyle.ForeColor = SystemColors.ControlText;
      }
      }





      share|improve this answer


























        0












        0








        0







        For custom-painting the ComboBox, you need to handle EditingControlShowing and then get the EditingControl which is DataGridViewComboBoxEditingControl and then set its DrawMode to OwnerDrawFixed and handle its DrawItem event.



        For custom-painting the cell, you need to handle CellPainting event and set different font and color for the cell styles and let the paint continue with new values. You can also paint the whole cell if you want.



        enter image description here



        Example



        Load Sample Data:



        private DataTable LoadProducts()
        {
        var dt = new DataTable();
        dt.Columns.Add("Name");
        dt.Columns.Add("CategoryId", typeof(int));
        dt.Rows.Add("P1", 1);
        dt.Rows.Add("P2", 1);
        dt.Rows.Add("P3", DBNull.Value);
        return dt;
        }
        private DataTable LoadCategories()
        {
        var dt = new DataTable();
        dt.Columns.Add("Id", typeof(int));
        dt.Columns.Add("Name");
        dt.Rows.Add(DBNull.Value, "No Category");
        dt.Rows.Add(1, "C1");
        dt.Rows.Add(2, "C2");
        dt.Rows.Add(2, "C3");
        return dt;
        }


        Setup DataGridView Columnms:



        private void Form1_Load(object sender, EventArgs e)
        {
        var products = LoadProducts();
        var categories = LoadCategories();

        dataGridView1.Columns.Add(new DataGridViewTextBoxColumn()
        {
        Name = "NameColumn",
        DataPropertyName = "Name",
        HeaderText = "Name"
        });
        dataGridView1.Columns.Add(new DataGridViewComboBoxColumn()
        {
        Name = "CategoryIdColumn",
        DataPropertyName = "CategoryId",
        HeaderText = "Category",
        DataSource = categories,
        ValueMember = "Id",
        DisplayMember = "Name",
        DisplayStyle= DataGridViewComboBoxDisplayStyle.Nothing
        });
        dataGridView1.DataSource = products;
        dataGridView1.EditingControlShowing += DataGridView1_EditingControlShowing;
        dataGridView1.CellPainting += DataGridView1_CellPainting;
        }


        Handle EditingControlShowing



        private void DataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
        if (dataGridView1?.CurrentCell?.OwningColumn?.Name != "CategoryIdColumn")
        return;
        var combo = e.Control as DataGridViewComboBoxEditingControl;
        if (combo == null)
        return;

        combo.DrawMode = DrawMode.OwnerDrawFixed;
        combo.DrawItem += (obj, args) =>
        {
        var txt = args.Index >= 0 ? combo.GetItemText(combo.Items[args.Index]) : "";
        var textColor = args.Index == 0 ? SystemColors.GrayText : SystemColors.ControlText;
        var font = args.Index == 0 ? new Font(combo.Font, FontStyle.Italic) : combo.Font;
        if ((args.State & DrawItemState.Selected) == DrawItemState.Selected)
        {
        textColor = SystemColors.HighlightText;
        }
        args.DrawBackground();
        TextRenderer.DrawText(args.Graphics, txt, font,
        args.Bounds, textColor,
        TextFormatFlags.VerticalCenter | TextFormatFlags.Left);
        };
        }


        Handle CellPainting



        private void DataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
        if (e.ColumnIndex < 0 || e.RowIndex < 0 ||
        dataGridView1.Columns[e.ColumnIndex].Name != "CategoryIdColumn")
        return;
        if (dataGridView1[e.ColumnIndex, e.RowIndex].Value == DBNull.Value)
        {
        e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Italic);
        e.CellStyle.ForeColor = SystemColors.GrayText;
        }
        else
        {
        e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Regular);
        e.CellStyle.ForeColor = SystemColors.ControlText;
        }
        }





        share|improve this answer













        For custom-painting the ComboBox, you need to handle EditingControlShowing and then get the EditingControl which is DataGridViewComboBoxEditingControl and then set its DrawMode to OwnerDrawFixed and handle its DrawItem event.



        For custom-painting the cell, you need to handle CellPainting event and set different font and color for the cell styles and let the paint continue with new values. You can also paint the whole cell if you want.



        enter image description here



        Example



        Load Sample Data:



        private DataTable LoadProducts()
        {
        var dt = new DataTable();
        dt.Columns.Add("Name");
        dt.Columns.Add("CategoryId", typeof(int));
        dt.Rows.Add("P1", 1);
        dt.Rows.Add("P2", 1);
        dt.Rows.Add("P3", DBNull.Value);
        return dt;
        }
        private DataTable LoadCategories()
        {
        var dt = new DataTable();
        dt.Columns.Add("Id", typeof(int));
        dt.Columns.Add("Name");
        dt.Rows.Add(DBNull.Value, "No Category");
        dt.Rows.Add(1, "C1");
        dt.Rows.Add(2, "C2");
        dt.Rows.Add(2, "C3");
        return dt;
        }


        Setup DataGridView Columnms:



        private void Form1_Load(object sender, EventArgs e)
        {
        var products = LoadProducts();
        var categories = LoadCategories();

        dataGridView1.Columns.Add(new DataGridViewTextBoxColumn()
        {
        Name = "NameColumn",
        DataPropertyName = "Name",
        HeaderText = "Name"
        });
        dataGridView1.Columns.Add(new DataGridViewComboBoxColumn()
        {
        Name = "CategoryIdColumn",
        DataPropertyName = "CategoryId",
        HeaderText = "Category",
        DataSource = categories,
        ValueMember = "Id",
        DisplayMember = "Name",
        DisplayStyle= DataGridViewComboBoxDisplayStyle.Nothing
        });
        dataGridView1.DataSource = products;
        dataGridView1.EditingControlShowing += DataGridView1_EditingControlShowing;
        dataGridView1.CellPainting += DataGridView1_CellPainting;
        }


        Handle EditingControlShowing



        private void DataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
        if (dataGridView1?.CurrentCell?.OwningColumn?.Name != "CategoryIdColumn")
        return;
        var combo = e.Control as DataGridViewComboBoxEditingControl;
        if (combo == null)
        return;

        combo.DrawMode = DrawMode.OwnerDrawFixed;
        combo.DrawItem += (obj, args) =>
        {
        var txt = args.Index >= 0 ? combo.GetItemText(combo.Items[args.Index]) : "";
        var textColor = args.Index == 0 ? SystemColors.GrayText : SystemColors.ControlText;
        var font = args.Index == 0 ? new Font(combo.Font, FontStyle.Italic) : combo.Font;
        if ((args.State & DrawItemState.Selected) == DrawItemState.Selected)
        {
        textColor = SystemColors.HighlightText;
        }
        args.DrawBackground();
        TextRenderer.DrawText(args.Graphics, txt, font,
        args.Bounds, textColor,
        TextFormatFlags.VerticalCenter | TextFormatFlags.Left);
        };
        }


        Handle CellPainting



        private void DataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
        if (e.ColumnIndex < 0 || e.RowIndex < 0 ||
        dataGridView1.Columns[e.ColumnIndex].Name != "CategoryIdColumn")
        return;
        if (dataGridView1[e.ColumnIndex, e.RowIndex].Value == DBNull.Value)
        {
        e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Italic);
        e.CellStyle.ForeColor = SystemColors.GrayText;
        }
        else
        {
        e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Regular);
        e.CellStyle.ForeColor = SystemColors.ControlText;
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 1 at 15:13









        Reza AghaeiReza Aghaei

        68.1k857171




        68.1k857171
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995531%2fhow-to-format-a-specific-item-in-the-combobox-in-a-datagridviewcomboboxcolumn%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

            Npm cannot find a required file even through it is in the searched directory