如何在C#.net中实现Excel的群集功能 [英] How to achive Group funcinality of the Excel in C#.net

查看:59
本文介绍了如何在C#.net中实现Excel的群集功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一个文件有30-33列。我需要做的是我必须在一些列上应用组,以便当我打开将显示那些列的文件时隐藏和上面的一个加号登录点击我可以手动取消隐藏列

解决方案

你将遇到的第一个问题是加号如何放在一个隐藏的列上,你将无法看到它取消隐藏列!

所以我在下面的示例中标记了隐藏的列,方法是在隐藏列左侧的列标题文本上加上一个加号(注意这意味着你无法隐藏第一个专栏[0])。



我选择使用上下文菜单来控制隐藏/取消隐藏,因为我不希望列出现和消失只是因为用户点击了DGV。

这个问题是确定用户右键单击哪一列显示菜单 - 这些信息在 EventArgs 。我通过在表格上有一个变量来解决这个问题

 private int dgvCol = -1; 

我在用户点击DGV时设置< pre lang =cs> private void dataGridView1_CellMouseDown( object sender,DataGridViewCellMouseEventArgs e)
{
dgvCol = e.ColumnIndex;
}



然后我将contextMenuStrip放入表单中,其中包含2个项目Hide Column和Unhide Columns(注意复数)并设置 ContextMenuStrip 该菜单条控件的DGV属性。

然后你需要连接一些事件来处理这些菜单项..

  private   void  hideColumnToolStripMenuItem_Click(对象发​​件人,EventArgs e)
{
...
}
private < span class =code-keyword> void unhideColumnsToolStripMenuItem_Click( object sender,EventArgs e)
{
...
}

我还决定向用户提供一个隐藏列的列表,作为取消隐藏列的子菜单,我每次隐藏或取消隐藏列时都会动态构建这些列。



这是我的完整代码,包括一个用于设置测试d的按钮ATA。这不是生产力量(即它仍然需要一些工作),但它应该让你去。请注意中提供的替代品(注释掉)unhideColumnsToolStripMenuItem_Click



 namespace DemoCollapseColumns 
{
公共部分类DGVCollapseColumns:表格
{
private int dgvCol = -1;
private void dataGridView1_CellMouseDown(object sender,DataGridViewCellMouseEventArgs e)
{
dgvCol = e.ColumnIndex;
}
private void hideColumnToolStripMenuItem_Click(object sender,EventArgs e)
{
if(dgvCol> 0)
hideColumn(dgvCol);
else
MessageBox.Show(无法隐藏第一列);
}
private void hideColumn(int colIndex)
{
dataGridView1.Columns [colIndex] .Visible = false;
if(!dataGridView1.Columns [colIndex - 1] .HeaderText.EndsWith(+))
dataGridView1.Columns [colIndex - 1] .HeaderText + =+;
SetUpUnhideSubMenu();
}
private void SetUpUnhideSubMenu()
{
//有更好的方法可以将这个
ToolStripMenuItem ti =(contextMenuStrip1.Items [unhideColumnsToolStripMenuItem]作为ToolStripMenuItem );

ti.DropDownItems.Clear();
foreach(DataGridViewColumn c in dataGridView1.Columns)
{
if(!c.Visible)
{
ToolStripItem ti1 = ti.DropDownItems.Add(c.HeaderText ,null,new EventHandler(UnHideMenu_ItemClicked));
ti1.Tag = c.Name;
}
}
}
private void unhideColumnsToolStripMenuItem_Click(object sender,EventArgs e)
{
//如果要取消隐藏所有内容,请取消注释
// foreach(dataGridView1.Columns中的DataGridViewColumn c)
// c.Visible = true;

//使用以下内容取消隐藏右边的列
//if(dataGridView1.Columns[dgvCol].HeaderText.EndsWith(\"+))
/ / UnhideColumn(dgvCol + 1);

//这将隐藏隐藏列的组中的所有内容
if(dataGridView1.Columns [dgvCol] .HeaderText.EndsWith(+))
{
while(dataGridView1.Columns [dgvCol] .HeaderText.EndsWith(+))
{
UnhideColumn(dgvCol + 1);
dgvCol + = 1;
}
}
}
private void UnHideMenu_ItemClicked(对象发送者,EventArgs e)
{
var nam =((ToolStripMenuItem)sender).Tag。的ToString();
if(nam.EndsWith(+))
nam = nam.Remove(nam.Length - 1,1);
Console.WriteLine(子菜单点击{0}:{1},((ToolStripMenuItem)sender).Text,nam);
UnhideColumn(dataGridView1.Columns [nam] .Index);
}
private void UnhideColumn(int colIndex)
{
dataGridView1.Columns [colIndex] .Visible = true;
dataGridView1.Columns [colIndex -1] .HeaderText = dataGridView1.Columns [colIndex -1] .HeaderText.Remove(dataGridView1.Columns [colIndex-1] .HeaderText.Length - 1,1);
SetUpUnhideSubMenu();
}
private void dataGridView1_ColumnHeaderMouseClick(object sender,DataGridViewCellMouseEventArgs e)
{
if(dataGridView1.Columns [e.ColumnIndex] .HeaderText.EndsWith(+))
unhideColumnsToolStripMenuItem_Click(sender,(EventArgs)e);
}

public DGVCollapseColumns()
{
InitializeComponent();
}

private void SetupTest_Click(object sender,EventArgs e)
{
//只需将一些虚拟值放入DGV,这样我们就可以使用$
DataTable dt;
dt = new DataTable();
for(var i = 0; i< 11; i ++)
{
dt.Columns.Add(Column+ i.ToString(),typeof(char));
}
for(var j = 0; j< 10; j ++)
{
var dr = dt.NewRow();
dr.ItemArray = new object [] {'A','B','C','D','E','F','G','H','I',' J','K'};
dt.Rows.Add(dr);
}
dataGridView1.DataSource = dt;

//决定在初始加载时隐藏哪些列
//我只是隐藏任意列
for(var k = 3; k< 6; k ++)
hideColumn(k);
for(var k = 8; k <10; k ++)
hideColumn(k);

}
}
}


Hi I have one file which are having some 30-33 column. what I need to do is I have to apply group on some of the columns so that when I open the file that will show those columns As hided and Above one Plus Sign on click of which I can unhide the columns manually

解决方案

The first problem you will have is where to put the plus-sign as if it is on a hidden column you're not going to be able to see it to unhide the column!
So I've marked the hidden columns in my example below by putting a plus-sign on the header text of the column immediately to the left of a hidden column (note this means you cannot hide the first column [0]).

I've chosen to use a Context menu to control the Hide/Unhide as I wouldn't want columns appearing and disappearing just because the User clicked on the DGV.
The problem with this is determining which column the user right-clicked on to show the menu - this information is not available in EventArgs. I got around this by having a variable on the form

private int dgvCol = -1;

which I set whenever the user clicks within the DGV

private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
    dgvCol = e.ColumnIndex;
}


I then put a contextMenuStrip onto the form with 2 items "Hide Column" and "Unhide Columns" (note the plural) and set the ContextMenuStrip property of the DGV to that menu strip control.
You then need to wire up some events to handle those menu items..

private void hideColumnToolStripMenuItem_Click(object sender, EventArgs e)
{
...
}
private void unhideColumnsToolStripMenuItem_Click(object sender, EventArgs e)
{
...
}

I also decided to present the user with a list of hidden columns as a sub-menu to "Unhide Columns" which I build dynamically each time a column is hidden or unhidden.

Here's my full code, including a button just to set up some test data. It's not production strength (i.e. it still needs some work) but it should get you going. Note the alternatives offered (commented out) in unhideColumnsToolStripMenuItem_Click

namespace DemoCollapseColumns
{
    public partial class DGVCollapseColumns : Form
    {
        private int dgvCol = -1;
        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            dgvCol = e.ColumnIndex;
        }
        private void hideColumnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgvCol > 0)
                hideColumn(dgvCol);
            else
                MessageBox.Show("Cannot hide the first column");
        }
        private void hideColumn(int colIndex)
        {
            dataGridView1.Columns[colIndex].Visible = false;
            if (!dataGridView1.Columns[colIndex - 1].HeaderText.EndsWith("+"))
                dataGridView1.Columns[colIndex - 1].HeaderText += "+";
            SetUpUnhideSubMenu();
        }
        private void SetUpUnhideSubMenu()
        {
            //There are better ways of doing this
            ToolStripMenuItem ti = (contextMenuStrip1.Items["unhideColumnsToolStripMenuItem"] as ToolStripMenuItem);

            ti.DropDownItems.Clear();
            foreach (DataGridViewColumn c in dataGridView1.Columns)
            {
                if (!c.Visible)
                {
                    ToolStripItem ti1 = ti.DropDownItems.Add(c.HeaderText, null, new EventHandler(UnHideMenu_ItemClicked));
                    ti1.Tag = c.Name;
                }
            }
        }
        private void unhideColumnsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //uncomment this if you want to unhide everything
            //foreach (DataGridViewColumn c in dataGridView1.Columns)
            //    c.Visible = true;

            //Use the following to unhide the column to the immediate right
            //if(dataGridView1.Columns[dgvCol].HeaderText.EndsWith("+"))
            //    UnhideColumn(dgvCol + 1);

            //This will unhide everything in a "group" of hidden columns
            if (dataGridView1.Columns[dgvCol].HeaderText.EndsWith("+"))
            {
                while (dataGridView1.Columns[dgvCol].HeaderText.EndsWith("+"))
                {
                    UnhideColumn(dgvCol + 1);
                    dgvCol += 1;
                }
            }
        }
        private void UnHideMenu_ItemClicked(object sender, EventArgs e)
        {
            var nam = ((ToolStripMenuItem)sender).Tag.ToString();
            if (nam.EndsWith("+"))
                nam = nam.Remove(nam.Length - 1, 1);
            Console.WriteLine("sub menu clicked {0} : {1}", ((ToolStripMenuItem)sender).Text, nam);
            UnhideColumn(dataGridView1.Columns[nam].Index);
        }
        private void UnhideColumn(int colIndex)
        {
            dataGridView1.Columns[colIndex].Visible = true;
            dataGridView1.Columns[colIndex -1].HeaderText = dataGridView1.Columns[colIndex -1].HeaderText.Remove(dataGridView1.Columns[colIndex-1].HeaderText.Length - 1, 1);
            SetUpUnhideSubMenu();
        }
        private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (dataGridView1.Columns[e.ColumnIndex].HeaderText.EndsWith("+"))
                unhideColumnsToolStripMenuItem_Click(sender, (EventArgs)e);
        }

        public DGVCollapseColumns()
        {
            InitializeComponent();
        }

        private void SetupTest_Click(object sender, EventArgs e)
        {
            // Just puts some dummy values into the DGV so we can play with the columns
            DataTable dt;
            dt = new DataTable();
            for (var i = 0; i < 11; i++)
            {
                dt.Columns.Add("Column " + i.ToString(), typeof(char));
            }
            for (var j = 0; j < 10; j++)
            {
                var dr = dt.NewRow();
                dr.ItemArray = new object[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K' };
                dt.Rows.Add(dr);
            }
            dataGridView1.DataSource = dt;

            //Decide which columns to hide on initial load
            //I'm just hiding arbitrary columns
            for (var k = 3; k < 6; k++)
                hideColumn(k);
            for (var k = 8; k < 10; k++)
                hideColumn(k);
            
        }
    }
}


这篇关于如何在C#.net中实现Excel的群集功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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