c# - 在 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();
}
解决方案
我找到了答案。这是我的代码,以防其他人遇到同样的问题:
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;
}
推荐阅读
- sharepoint - Graph API 403 Forbidden - 令牌中需要存在 scp 或角色声明
- apache-flink - Flink - 具有高并行度的网络缓冲区数量不足
- c# - 文本的重叠部分变得透明 GDI+
- rabbitmq - rabbitMq中的事件交换规则
- angular - Angular Reactive Forms:如何显示错误+红色边框?
- pandas - 如何删除列表中的那些列元素的行?
- python - 在 Python 中重新格式化 DataFrame
- javascript - Twitter引导模式在底部滚动条时侦听
- arrays - 将循环中的答案添加到列表中
- php - 带有连接的 PostgreSQL 查询仅通过 AJAX 给出“内部服务器错误”(不在 pgAdmin 上)