【问题标题】:Can a DataGridView allow more than one "object" in a cell?DataGridView 可以在一个单元格中允许多个“对象”吗?
【发布时间】:2011-11-15 23:10:57
【问题描述】:

是否可以在DataGridView 的单元格中允许多个对象?

我想在第一列中显示一个组,然后在它旁边的列中显示该组中的所有用户,但在一个单元格中(组名称旁边的单元格)。这不能是字符串操作,因为我需要能够单击每个用户并将它们拖放到周围。我正在使用链接到 SQL 查询数据库的DataGridview

我正在使用 Visual Basic 和 Visual Studio 2010。

【问题讨论】:

    标签: vb.net winforms datagridview


    【解决方案1】:

    很遗憾,您不能在 DataGridView 中执行此操作。但是,您可以创建一个如下所示的表:

    GROUP   USER
    ------  -----
    
    Group1  User1
    Group1  User2
    Group1  User3
    Group2  UserA
    Group2  UserB
    

    完成此操作后,您可以处理 DataGridView 的 CellFormatting 事件,如果上面单元格的值匹配,则将 e.FormattedValue 设置为 String.Empty。然后你的桌子会“看起来”像:

    GROUP   USER
    ------  -----
    
    Group1  User1
            User2
            User3
    Group2  UserA
            UserB
    

    这几乎就是您想要的。如果您希望网格线消失,您可以改为处理 DataGridView 的 CellPainting 事件以绘制正确的边框、居中文本等。

    更新:这是使用 CellFormatting 事件隐藏单元格内容的示例。在这个例子中,我只是使用一个 List 作为我的数据源,但是你可以用它来代替你的 SQL 数据源、DataTable 等。

    public partial class Form1 : Form
    {
        private List<GroupUserRow> _rows = new List<GroupUserRow>();
    
        public Form1()
        {
            InitializeComponent();
    
            _rows.Add(new GroupUserRow { Group = "Group1", User = "User1" });
            _rows.Add(new GroupUserRow { Group = "Group1", User = "User2" });
            _rows.Add(new GroupUserRow { Group = "Group1", User = "User3" });
            _rows.Add(new GroupUserRow { Group = "Group2", User = "UserA" });
            _rows.Add(new GroupUserRow { Group = "Group2", User = "UserB" });
    
            dataGridView1.DataSource = _rows;
        }
    
        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex == 0)
            {
                int prevRowIndex = e.RowIndex - 1;
                if (prevRowIndex >= 0 && dataGridView1[0, prevRowIndex].Value == e.Value)
                {
                    //this just "hides" the value from the screen, the value is not
                    //removed from the cell
                    e.Value = String.Empty;
                    e.FormattingApplied = true;
                }
            }
        }
    
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                MessageBox.Show("This cell's value is: " + dataGridView1[e.ColumnIndex, e.RowIndex].Value);
            }
        }
    
    }
    
    public class GroupUserRow
    {
        public string Group { get; set; }
        public string User { get; set; }
    }
    

    【讨论】:

    • 感谢您的回复!但是,我之前已经这样做了,我所做的是将每个组放入一个数组中,如果该数组已经包含该组,则 String = "" 但是每次单击单元格时 DGV 都会重新绘制自己。因此,如果我单击具有组名的单元格,它将检查它是否在数组中。当它看到组在数组中时,它将其设置为空。因此,每当我单击单元格时,字符串都会设置为空。有任何想法吗?任何关于 CellPainting 的进一步解释都会有所帮助!谢谢! =)
    • 嗨詹姆斯!我添加了一个使用 CellFormatting 事件隐藏单元格的示例。让我知道这是怎么回事! :-)
    • 嘿凯文!感谢您的快速回复!!这当然有很大帮助!现在我担心的一个问题是我已经知道答案是,如果我有一整行设置为 String.Empty 的单元格,有没有办法删除该行?我一直遇到数据库问题,抱怨删除行后 DGV 中没有足够的空间用于 SQL 查询。我问主要是因为它在美学上并不吸引人,尤其是当我有超过 100 个用户时。此外,当它可能妨碍我添加拖放功能时。谢谢! =)
    猜你喜欢
    • 1970-01-01
    • 2012-12-18
    • 2016-10-07
    • 2015-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    相关资源
    最近更新 更多