首页 > 解决方案 > 在 datagridview 组合框列中设置嵌入字体(Winform)

问题描述

一直在寻找这个,但找不到任何东西。是否可以为 datagridview 组合框列设置嵌入字体?我嵌入了一个字体(工程符号),我可以把它分配给一个常规的组合框就好了。这是代码:

private void AllocFont(Font f, Control c, float size)
    {
        FontStyle fontStyle = FontStyle.Regular;
        c.Font = new Font(ff, size, fontStyle);
    }

我像这样使用它:

private void Inspection_report_builder_Load(object sender, EventArgs e)
    {
        loadFont();
        AllocFont(font, this.comboBox1,10);
    }

但我似乎无法将字体分配给 datagridview 组合框。组合框已经创建,所以我不需要以编程方式创建它。我只需要分配字体。有任何想法吗?到目前为止,我可以填充组合框项目(工作得很好)我是新手,所以如果你能提供一个代码示例以便我理解它,那就太好了。这是我用来填充列表的代码,以防您想查看它:

private void FillSymbolCombo()
    {
        ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["shopmanagerConnectionString1"];
        MySqlConnection con = new MySqlConnection(conSettings.ToString());
        MySqlCommand cmd = new MySqlCommand(@"select symbols from shopmanager.engineering_symbols;", con);
        MySqlDataReader myReader;
        try
        {
            con.Open();
            myReader = cmd.ExecuteReader();

            while (myReader.Read())
            {
                string sList = myReader.GetString("symbols");
                Column5.Items.Add(sList);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        con.Close();
    }

标签: c#datagridviewdatagridcomboboxcolumn

解决方案


我找到了答案。这是我的代码,以防其他人遇到同样的问题:

private void loadFont()
    {
        byte[] fontArray = ERP1.Properties.Resources.engineering2;
        int dataLenght = ERP1.Properties.Resources.engineering2.Length;

        IntPtr ptrData = Marshal.AllocCoTaskMem(dataLenght);
        Marshal.Copy(fontArray, 0, ptrData, dataLenght);

        uint cFonts = 0;
        AddFontMemResourceEx(ptrData, (uint)fontArray.Length, IntPtr.Zero, ref cFonts);

        PrivateFontCollection pfc = new PrivateFontCollection();
        pfc.AddMemoryFont(ptrData, dataLenght);

        Marshal.FreeCoTaskMem(ptrData);

        ff = pfc.Families[0];
        font = new Font(ff, 9f, FontStyle.Regular);

        // This is where I assign the font to the combobox in the datagridview
        Column5.DefaultCellStyle.Font = font;
    }

推荐阅读