DataGridView
DataGridView上按右键弹出右键菜单前选中当前行
private void DataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
DataGridView1.ClearSelection();
DataGridView1.Rows[e.RowIndex].Selected = true;
DataGridView1.CurrentCell = DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
}
/**//*
2007-05-16 17:31 by HELLO [未注册用户]
用“dataGridView1_CellMouseClick”这个事件
有些时候可以只弹出菜单,没有选择单元格;有时候只选择单元格,没有弹出菜单
如果菜单控件在绑在DATAGRIDVIEW上(dataGridView1.ContextMenuStrip = this.ContextSrtip1),又不能选择单元格
*/
DataGridView 超链接点击事件
//内容点击, 并判断所点击的列名
private void dgvData_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
DataGridView dgv = sender as DataGridView;
string columnName = dgv.Columns[e.ColumnIndex].Name;
if ("operation".Equals(columnName))
{//如果点击的列名符合
dgv.Rows.RemoveAt(e.RowIndex);
}
}
TreeView
c# winform TreeView控件中实现拖拽的功能
节点拖拽事件#region 节点拖拽事件
//当用户开始拖动节点时
private void tvModel_ItemDrag(object sender, ItemDragEventArgs e)
{
TreeNode selectNode = e.Item as TreeNode;
this.tvModel.SelectedNode = selectNode;
this.form.DoDragDrop(e.Item, DragDropEffects.Move);
}
//将对象拖入控件的边界时
private void tvModel_DragEnter(object sender, DragEventArgs e)
{
TreeNode enterNode = (TreeNode)(e.Data.GetData(typeof(TreeNode)));
if (enterNode != null)
e.Effect = DragDropEffects.Move;
else
e.Effect = DragDropEffects.None;
}
//在完成拖放操作时
private void tvModel_DragDrop(object sender, DragEventArgs e)
{
TreeNode selectNode = (TreeNode)(e.Data.GetData(typeof(TreeNode)));
//0.如果选择源为空 或者不是二级节点则退出
if (selectNode == null || selectNode.Level != 1)
return;
//1.根据鼠标坐标获得目标节点
Position.X = e.X;
Position.Y = e.Y;
Position = this.tvModel.PointToClient(Position);
TreeNode targetNode = this.tvModel.GetNodeAt(Position);
foreach (TreeNode node in this.tvModel.Nodes[0].Nodes)
node.NodeFont = new Font(this.form.Font, FontStyle.Regular);
//2.获得要插入的目标节点索引
int index = 0;
if (targetNode == null)
index = selectNode.Parent.Nodes.Count - 1; //目标节点为null, 直接插入到最后
else if (targetNode.Level == 1)
index = targetNode.Index + 1; //目标节点为同级, 直接在之后插入
else if (targetNode.Level == 0)
index = 0; //目标为根节点, 直接插入到最前
else if (targetNode.Level > 1)
{ //目标节点为二级节点以后, 遍历到其二级节点
while (targetNode.Level > 1)
targetNode = targetNode.Parent;
index = index = targetNode.Index + 1;
}
//3.源节点删除, 目标节点在索引出插入, 并高亮选中
selectNode.Remove();
this.tvModel.Nodes[0].Nodes.Insert(index, selectNode);
this.tvModel.SelectedNode = selectNode;
}
//将对象拖过控件边缘时
private void tvModel_DragOver(object sender, DragEventArgs e)
{
Position.X = e.X;
Position.Y = e.Y;
Position = this.tvModel.PointToClient(Position);
//在拖过的控件前后显示划线效果
TreeNode targetNode = this.tvModel.GetNodeAt(Position);
if (targetNode != null && targetNode.Level == 1)
{
if (targetNode.PrevNode != null)
targetNode.PrevNode.NodeFont = new Font(this.form.Font, FontStyle.Regular);
if (targetNode.NextNode != null)
targetNode.NextNode.NodeFont = new Font(this.form.Font, FontStyle.Regular);
targetNode.NodeFont = new Font(this.form.Font, FontStyle.Underline);
}
}
#endregion
posted on 2009-07-10 12:21
黄小二 阅读(633)
评论(0) 编辑 收藏 所属分类:
C#