自定义DataGridViewCheckBoxCell列以显示字符串? [英] Customize a column of DataGridViewCheckBoxCell to display string?

查看:63
本文介绍了自定义DataGridViewCheckBoxCell列以显示字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能不能帮我解决这个问题,我的SQL表中有一列作为bit的类型(例如,性别列)所以当加载表时,DataGridView会显示一列复选框而没有任何文本,检查男性(比特值为1),未检查女性(比特值为0)。我不喜欢那种看起来很难看的方式,我想自定义或做一些事情来格式化DataGridViewCheckBoxCell列中每个单元格的内容,以便一串Male替换选中的复选框和一串字符串女性取代了未经检查的复选框。



如果我将原始列更改为另一个列类型(如varchar)以存储男性字符串,则问题将很容易解决。 和女性直接,但我喜欢使用比特,也许这是节省存储的方式,不是吗?



谢谢你你的帮助!

Could you please help me out with this problem, I have a column in my SQL table as type of bit (for example, a column of gender) so when loading the table, the DataGridView shows me a column of checkboxes without any text, the male gender is checked (the bit value is 1) and female gender is unchecked (the bit value is 0). I don''t like it that way which looks ugly to me, I want to customize or do something to format the content of each cell in that column of DataGridViewCheckBoxCell so that a string of "Male" replaces the checked checkbox and a string of "Female" replaces the unchecked checkbox.

The problem will be sorted out easily if I change my original column to another column type like varchar to store the strings of "Male" and "Female" directly, but I like to use bit instead, maybe it''s a way to save storage, isn''t it?

Thank you for your help!

推荐答案

这样做的一种方法是创建一个自定义DataGridView列,如这里 [ ^ ]。
One way this could be done is to create a custom DataGridView column as described here[^].


谢谢Mike Hankey的良好链接,继承DataGridViewCell和DataGridViewColumn还可以,但它不够简单,我选择了另一个解决方案,它只为datagridView的CellPainting处理程序添加了一些代码行,这是我试过的代码,它运行得很完美:



Thank you Mike Hankey for the good link, inheriting DataGridViewCell and DataGridViewColumn is OK but it is not simple enough and I chose another solution which only adds some lines of code to the CellPainting handler of the datagridView, here is the code I tried, it works perfectly:

private void HS_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (HS.Columns[e.ColumnIndex].CellTemplate.GetType() == typeof(DataGridViewCheckBoxCell)&&e.RowIndex > -1)
            {
                e.Handled = true;                
                string s = (bool)e.Value ? "Male" : "Female";
                e.PaintBackground(e.CellBounds, HS.CurrentCellAddress.Y == e.RowIndex);
                StringFormat sf = new StringFormat();
                sf.LineAlignment = StringAlignment.Center;
                e.Graphics.DrawString(s, HS.Font, new SolidBrush(HS.ForeColor), e.CellBounds,sf);                
            }
        }





谢谢!



Thanks!


这篇关于自定义DataGridViewCheckBoxCell列以显示字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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