ContextMenuStrip
鼠标右键
1、添加控件,将ContextMenuStrip拖到视图设计器的下方或在工具箱里双击ContextMenuStrip添加
此时在左下角会显示ContextMenuStrip,点击之后
新增一个【新增,修改,删除】的右键菜单
然后双击新增的【新增,修改,删除】菜单,进入到点击事件中,编写提示框代码。
private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("新增成功");
}
private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
{
BllOpDatabaseManager.dbName = User.aliTmpDBName;
//获得当前选中的行
//int rowindex = e.RowIndex;
string strid = Convert.ToString(dataGridView1.CurrentCell.RowIndex);
string strUVPC = dataGridView1.Rows[rowindex].Cells["商品访客数"].Value.ToString();
string strUV = dataGridView1.Rows[rowindex].Cells["访客数"].Value.ToString();
//string cmdstr = "update Users set UserName='" + txtUname.Text + "',UserPass='" + txtUpass.Text + "' where UserID=" + Convert.ToInt32(strid) + "";
//string cmdstr = "update 店铺整体日指标 set 商品访客数=" + strUVPC + ",访客数=" + strUV + " where 统计日期=" + dataGridView1.Rows[rowindex].Cells[0].Value.ToString() + "";
string cmdstr = "update 店铺整体日指标 set 商品访客数=" + Convert.ToInt32(strUVPC) + ",访客数=" + Convert.ToInt32(strUV) + " where 统计日期='" + dataGridView1.Rows[rowindex].Cells[0].Value.ToString() + "'";
MessageBox.Show(cmdstr, "友情提示");
IsExecuteNon = BllOpDatabaseManager.ExecuteNonQuery(cmdstr);
if (IsExecuteNon)
{
//MessageBox.Show("记录已从数据库删除,请按刷新按钮刷新显示列表", "友情提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
Display(); //显示数据库内容
MessageBox.Show("修改成功!");
}
else
{
MessageBox.Show("修改失败!");
}
}
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
bool IsExecuteNon = false;
BllOpDatabaseManager.dbName = User.aliTmpDBName;
//获得当前选中的行
//int rowindex = e.RowIndex;
//string cmdstr = string.Format("delete from 店铺整体日指标 where 统计日期 = '{0}'", dataGridView1.SelectedRows[0].Cells[0].Value);
string cmdstr = string.Format("delete from 店铺整体日指标 where 统计日期 = '{0}'", dataGridView1.Rows[rowindex].Cells[0].Value.ToString() );
MessageBox.Show(cmdstr, "友情提示");
IsExecuteNon = BllOpDatabaseManager.DelBtn_Click(cmdstr);
if (IsExecuteNon)
{
//MessageBox.Show("记录已从数据库删除,请按刷新按钮刷新显示列表", "友情提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
Display(); //显示数据库内容
MessageBox.Show("删除成功!");
}
else
{
MessageBox.Show("删除失败!");
}
}
private void 移除ToolStripMenuItem_Click(object sender, EventArgs e)
{
//dataGridviewView中删除功能就实现
dataGridView1.AllowUserToAddRows = false;//删除最后一行的空白行
dataGridView1.Rows.Remove(dataGridView1.CurrentRow);//删除当前光标所在行
MessageBox.Show("移除成功!");
//dataGridView1.Rows.Clear();//删除所有行
}
//更改当前单元格时,返回数据库,事件
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
BllOpDatabaseManager.dbName = User.aliTmpDBName;
int myint = dataGridView1.CurrentCell.ColumnIndex;//当前列索引
int myint2 = dataGridView1.CurrentCell.RowIndex;//当前行索引
string str1 = dataGridView1.Columns[myint].HeaderText + "=" + "'" + dataGridView1.CurrentCell.Value.ToString() + "'";//列名+当前格内容
string str2 = dataGridView1.Rows[myint2].Cells[0].Value.ToString();//当前行,0列内容。
string cmdstr = "update 店铺整体日指标 set " + str1 + " where 统计日期 ='" + str2 + "'";//sql更改语句
MessageBox.Show(cmdstr, "友情提示");
IsExecuteNon = BllOpDatabaseManager.ExecuteNonQuery(cmdstr);
if (IsExecuteNon)
{
//MessageBox.Show("记录已从数据库删除,请按刷新按钮刷新显示列表", "友情提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
Display(); //显示数据库内容
MessageBox.Show("修改成功!");
}
else
{
MessageBox.Show("修改失败!");
}
}
dataGridviewView中删除功能就实现
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
dataGridView1.AllowUserToAddRows = false;//删除最后一行的空白行
dataGridView1.Rows.Remove(dataGridView1.CurrentRow);//删除当前光标所在行
MessageBox.Show("删除成功!");
//dataGridView1.Rows.Clear();//删除所有行
}
建立关联
找到DataGridView的属性中的ContextMenuStrip,将其设置为上面拖拽的右键菜单控件。