首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

DataGridView两列开展互换的源码

2013-10-23 
DataGridView两列进行互换的源码private void toolLeft2_Click(object sender, EventArgs e){int selcol

DataGridView两列进行互换的源码

private void toolLeft2_Click(object sender, EventArgs e)
        {
            int selcol = DBGrid2.CurrentCell.ColumnIndex;
            int selrow = DBGrid2.CurrentCell.RowIndex;
            DataGridViewColumn col = (DataGridViewColumn)DBGrid2.Columns[selcol].Clone();
            DBGrid2.Columns.Insert(DBGrid2.CurrentCell.ColumnIndex - 1, col);
            DBGrid2.Columns.RemoveAt(selcol + 1);
            DBGrid2.CurrentCell = DBGrid2.Rows[selrow].Cells[selcol - 1];
        }

        private void toolRight2_Click(object sender, EventArgs e)
        {
            int selcol = DBGrid2.CurrentCell.ColumnIndex;
            int selrow = DBGrid2.CurrentCell.RowIndex;
            DataGridViewColumn col = (DataGridViewColumn)DBGrid2.Columns[selcol].Clone();
            DBGrid2.Columns.Insert(DBGrid2.CurrentCell.ColumnIndex + 2, col);
            DBGrid2.Columns.RemoveAt(selcol);
            DBGrid2.CurrentCell = DBGrid2.Rows[selrow].Cells[selcol + 1];
        }

        private void DBGrid2_SelectionChanged(object sender, EventArgs e)
        {
            if (DBGrid2.CurrentCell == null) return;
            if (DBGrid2.CurrentCell.ColumnIndex <= 1 || DBGrid2.CurrentCell.ColumnIndex >= DBGrid2.ColumnCount - 1)
            {
                toolLeft2.Enabled = false;
                toolRight2.Enabled = false;
            }
            else if (DBGrid2.CurrentCell.ColumnIndex == 2)
            {
                toolLeft2.Enabled = false;
                if (DBGrid2.CurrentCell.ColumnIndex == DBGrid2.ColumnCount - 2)
                    toolRight2.Enabled = false;
                else
                    toolRight2.Enabled = true;
            }
            else
            {
                toolLeft2.Enabled = true;
                if (DBGrid2.CurrentCell.ColumnIndex == DBGrid2.ColumnCount - 2)
                    toolRight2.Enabled = false;
                else
                    toolRight2.Enabled = true;
            }
        }

     

热点排行