DataGridView删除、修改方法封装
private int curRow = 0;//鼠标右键得到的当前行号
#region 激发鼠标事件,显示右键菜单,得到被选中的行的行号,注意第一列被隐藏的情况考虑
private int showRightMenu(DataGridView dataGridView, ContextMenuStrip contextMenuStrip, DataGridViewCellMouseEventArgs e)
{
int curRowTemp = 0;//当前鼠标在DataGridView中的行的位置
if (e.Button == MouseButtons.Right)
{
private int showRightMenu(DataGridView dataGridView, ContextMenuStrip contextMenuStrip, DataGridViewCellMouseEventArgs e)
{
int curRowTemp = 0;//当前鼠标在DataGridView中的行的位置
if (e.Button == MouseButtons.Right)
{
curRowTemp = e.RowIndex;
if (curRowTemp < 0 || curRowTemp > dataGridView.Rows.Count)
{
curRowTemp = 0;
}
else
{
dataGridView.CurrentCell = dataGridView[1, curRowTemp];
dataGridView.ContextMenuStrip = contextMenuStrip;
{
curRowTemp = 0;
}
else
{
dataGridView.CurrentCell = dataGridView[1, curRowTemp];
dataGridView.ContextMenuStrip = contextMenuStrip;
}
}
return curRowTemp;//返回当前鼠标所在的行的行号
}
#endregion
}
return curRowTemp;//返回当前鼠标所在的行的行号
}
#endregion
#region 根据行号,得到DataGridView中该行的某一列的信息
private Object GetColumeInfo(DataGridView dataGridView, int curRow, int colnum)
{
Object info = null;
info = dataGridView.Rows[curRow].Cells[colnum].Value;
return info;
}
#endregion
private Object GetColumeInfo(DataGridView dataGridView, int curRow, int colnum)
{
Object info = null;
info = dataGridView.Rows[curRow].Cells[colnum].Value;
return info;
}
#endregion
#region 根据行号,设置DataGridView中该行的某一列的信息
private int SetColumeInfo(DataGridView dataGridView, int curRow, int column, Object info)
{
dataGridView.Rows[curRow].Cells[column].Value = info;
return (int)this.GetColumeInfo(dataGridView, curRow, 0);
}
#endregion
private int SetColumeInfo(DataGridView dataGridView, int curRow, int column, Object info)
{
dataGridView.Rows[curRow].Cells[column].Value = info;
return (int)this.GetColumeInfo(dataGridView, curRow, 0);
}
#endregion
#region 根据行号,删除该行
private int RemoveRow(DataGridView dataGridView, int curRow)
{
int rowID = (int)this.GetColumeInfo(dataGridView, curRow, 0);
dataGridView.Rows.RemoveAt(curRow);
private int RemoveRow(DataGridView dataGridView, int curRow)
{
int rowID = (int)this.GetColumeInfo(dataGridView, curRow, 0);
dataGridView.Rows.RemoveAt(curRow);
System.Console.WriteLine("删除的索引号" + rowID + "删除的行号" + curRow);
return rowID;
return rowID;
}
#endregion
#endregion