WinForms 列标题字体样式 [英] WinForms Column Header FontStyle

查看:54
本文介绍了WinForms 列标题字体样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用从 MySQL 数据库中获取其项目的 ListView 创建一个对话框.我刚刚开始工作,现在我想让标题有粗体文本.

I am currently trying to make a Dialog with a ListView that takes its Items from a MySQL Database. I just got it working and now I want to get the Header to have bold Text.

这是我当前的代码:

public Form1()
    {
        InitializeComponent();

        lvInstrumente.Columns.Add("ID", 30, HorizontalAlignment.Left);
        lvInstrumente.Columns.Add("Hersteller", 100, HorizontalAlignment.Left);
        lvInstrumente.Columns.Add("Instrument", 100, HorizontalAlignment.Left);
        lvInstrumente.Columns.Add("Preis", 50, HorizontalAlignment.Left);
        lvInstrumente.Columns.Add("Typ", 100, HorizontalAlignment.Left);
        lvInstrumente.Columns.Add("Farbe", 110, HorizontalAlignment.Left);
        lvInstrumente.Columns.Add("Beschreibung", 550, HorizontalAlignment.Left);
        lvInstrumente.FullRowSelect = true;
        lvInstrumente.GridLines = true;
        lvInstrumente.View = View.Details;
    }

    private void btnSelect_Click(object sender, EventArgs e)
    {


        // Verbindung zu DB aufbauen
        var db = new MySqlDB("localhost", "3306", "musicstoreuster");
        var h = db.Connect();
        if (h == null)
            return;// Wenn die Verbindung nicht klappt, steigen wir hier aus

        var list = db.Select("product");


        foreach(var item in list)
        {
            Console.WriteLine(item.ToString());

            var ds = item.Split(new char[] { ';' });
            ListViewItem lvItem = new ListViewItem(ds);
            lvInstrumente.Items.Add(lvItem);   
        }

        db.Close();
    } 

为了使标题加粗,我最终得到了这个解决方案"

To make the Header Bold I ended up with this "solution"

for (int i = 0; i < lvInstrumente.Columns.Count; i++)
        {
            lvInstrumente.Columns[i].ListView.Font = new Font(lvInstrumente.Columns[i].ListView.Font, FontStyle.Bold);
        }

但是现在当我按下选择按钮时,一切都是粗体.我需要添加/更改什么以防止项目也被加粗?

But now when I press the Select Button everything is Bold. What do i need to add/change to Prevent the Items from also being Bold?

推荐答案

在添加项目之前设置字体.类似这样的事情:

Set the Font before you add the item. Some thing like this:

var ds = item.Split(new char[] { ';' });
ListViewItem lvItem = new ListViewItem(ds);

//set the font to the item
lvItem.Font = new Font(lvItem.Font,FontStyle.Regular);

Items.Add(lvItem); 

这有效,我测试过:v

我也一直在测试并使标题加粗,只有这一行可以正常工作

Also i've been testing and to make the headers bold, with only this line works fine

lvInstrumente.Columns[0].ListView.Font = new Font(lvInstrumente.Columns[0].ListView.Font, FontStyle.Bold);

这篇关于WinForms 列标题字体样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆