2

我有一个带有单个文本字符的 DataGridView .. 基本上像字符映射,但希望能够从左到右选择单元格组,就好像它是文本一样。

即,如果我垂直选择 2 个单元格,则默认行为是只选择这 2 个单元格。但是,我希望它选择顶行右侧和第二行左侧的所有单元格。就像您要突出显示此问题文本中的两个垂直字符一样。

是否可以?(我是否设法以一种有意义的方式解释它?!)

谢谢!

编辑:添加了几张图片来帮助更好地解释这一点。

首先,如果我选择“P”并向下拖动到“d”,这就是选择的内容: 默认 DataGridView 行为

在这种情况下,我想发生的是: 在此处输入图像描述

希望这能让它更清楚,谢谢:)

4

2 回答 2

1

这个有效,看看

private void Form1_Load(object sender, EventArgs e)
    {
        dataGridView1.DataSource = li;
        dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
        dataGridView1.SelectionChanged += dataGridView1_SelectionChanged;
    }

    private void dataGridView1_SelectionChanged(object sender, EventArgs e)
    {
        var la = dataGridView1.SelectedCells;
        if (la.Count == 2)
        {
            var rowFirst = ((DataGridViewCell)la[0]).RowIndex;
            var columnFirst = ((DataGridViewCell)la[0]).ColumnIndex;

            var rowSecond = ((DataGridViewCell)la[1]).RowIndex;
            var columnSecond = ((DataGridViewCell)la[1]).ColumnIndex;

            for (int column = 0; column <= columnFirst; column++)
            {
                dataGridView1[column, rowFirst].Selected = true;
            }

            for (int column = columnSecond; column < dataGridView1.ColumnCount; column++)
            {
                dataGridView1[column, rowSecond].Selected = true;
            }
        }
    }
于 2013-02-28T21:28:58.513 回答
0

用更通用的解决方案扩展了 simple-thomas 的答案,我想出了这个。它不能很好地处理缩小的选择,但对于我的目的来说已经足够了。谢谢简单的托马斯!

    private void characterGrid_SelectionChanged( object sender, EventArgs e )
    {
        if ( SelectionChanging == true )
        {
            return;
        }

        SelectionChanging = true;

        var la = characterGrid.SelectedCells;
        if ( la.Count > 1 )
        {
            var sorted = new List<DataGridViewCell>();
            System.Collections.IEnumerator enumerator = la.GetEnumerator();
            while ( enumerator.MoveNext() )
            {
                sorted.Add( (DataGridViewCell)enumerator.Current );
            }
            sorted.Sort( delegate( DataGridViewCell t1, DataGridViewCell t2 )
            {
                if ( t1.RowIndex != t2.RowIndex )
                {
                    return t1.RowIndex.CompareTo( t2.RowIndex );
                }
                else
                {
                    return t1.ColumnIndex.CompareTo( t2.ColumnIndex );
                }
            });

            var rowFirst = ( (DataGridViewCell)sorted[ 0 ] ).RowIndex;
            var columnFirst = ( (DataGridViewCell)sorted[ 0 ] ).ColumnIndex;

            var rowLast = ( (DataGridViewCell)sorted[ sorted.Count - 1 ] ).RowIndex;
            var columnLast = ( (DataGridViewCell)sorted[ sorted.Count - 1 ] ).ColumnIndex;

            if ( rowLast > rowFirst )
            {
                // select first row
                for ( int column = columnFirst; column < characterGrid.ColumnCount; column++ )
                {
                    characterGrid[ column, rowFirst ].Selected = true;
                }

                // in between
                for ( int row = rowFirst + 1; row < rowLast; row++ )
                {
                    for ( int column = 0; column < characterGrid.ColumnCount; column++ )
                    {
                        characterGrid[ column, row ].Selected = true;
                    }
                }

                // last row
                for ( int column = 0; column <= columnLast; column++ )
                {
                    characterGrid[ column, rowLast ].Selected = true;
                }
            }
        }

        SelectionChanging = false;
    }
于 2013-02-28T22:13:30.420 回答