dataGridView右键菜单并选中该行 http://blog.csdn.net/lanmao100/archive/2009/06/25/4298529.aspx

dataGridView右键菜单并选中该行

程序代码:

private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (e.RowIndex >= 0)
{
dataGridView1.ClearSelection();
dataGridView1.Rows[e.RowIndex].Selected
= true;
dataGridView1.CurrentCell
= dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
}
}
}

去掉带
*好的左边的那一列:

把DataGridView 的RowHeadervisible设置成false


private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
}
}

//上移
private void btnMoveUp_Click(object sender, EventArgs e)
{
for (int i = 0; i < listView1.SelectedItems.Count; i++)
{
System.Windows.Forms.ListViewItem listViewItem
= this.listView1.SelectedItems[i];
int index = this.listView1.SelectedItems[i].Index - 1;
if (index < 0) return;
this.listView1.Items.Remove(this.listView1.SelectedItems[i]);
this.listView1.Items.Insert(index, listViewItem);
listViewItem.Selected
= true;
}
}
//下移
private void btnMoveDown_Click(object sender, EventArgs e)
{
for (int i = this.listView1.SelectedItems.Count - 1; i > -1; i--)
{
System.Windows.Forms.ListViewItem listViewItem
= this.listView1.SelectedItems[i];
int index = this.listView1.SelectedItems[i].Index + 1;
if (index > this.listView1.Items.Count - 1) return;
this.listView1.Items.Remove(this.listView1.SelectedItems[i]);
this.listView1.Items.Insert(index, listViewItem);
listViewItem.Selected
= true;
}
}

http://blog.csdn.net/lanmao100/archive/2009/06/25/4298529.aspx
posted @ 2011-06-22 18:05  许明吉博客  阅读(2087)  评论(0编辑  收藏  举报