一、在设置gridview中定义事件和打开开关
1、定义事件:
OnFillContextMenuItems=“ASPxGridView1_FillContextMenuItems”
OnAddSummaryItemViaContextMenu=“ASPxGridView1_AddSummaryItemViaContextMenu”
OnContextMenuItemVisibility=“ASPxGridView1_ContextMenuItemVisibility”
OnContextMenuItemClick=“ASPxGridView1_ContextMenuItemClick”
2、打开开关:
二、后台事件代码:
protected void ASPxGridView1_FillContextMenuItems(object sender, ASPxGridViewContextMenuEventArgs e)
{
if (e.MenuType == GridViewContextMenuType.Rows)
{
//关于上下文菜单项的研究,暂留
//e.Items.Clear();//清除所有默认的菜单,默认菜单包括新建、编辑、刷新,如果不加这句会显示默认菜单,可根据情况设置,需要显示默认菜单就不用清除,直接在默认菜单的某个位置插入自定义菜单即可
//创建一个菜单组并增加子菜单的方法:
//var item = e.CreateItem("导出", "Export");
//item.BeginGroup = true;
//e.Items.Insert(e.Items.IndexOfCommand(GridViewContextMenuCommand.Refresh), item);
//e.Items.Add(item);
//AddMenuSubItem(item, "PDF", "ExportToPDF", @"~\Images\16\2012092109942.png", true);
//AddMenuSubItem(item, "XLS", "ExportToXLS", @"~\Images\16\2012092109942.png", true);
//创建一个菜单组并增加子菜单的方法:
//var item2 = e.CreateItem("审核", "shenhe");
//item2.BeginGroup = true;
e.Items.Insert(e.Items.IndexOfCommand(GridViewContextMenuCommand.Refresh), item2);
//e.Items.Add(item2);
//AddMenuSubItem(item2, "审核本行", "ShenHe_Current_Row", @"~\Images\16\bell.png", false);
//AddMenuSubItem(item2, "审核本页", "ShenHe_Current_Page", @"~\Images\16\bin_empty.png", false);
//直接创建菜单项的方法:可以使用图标
e.Items.Clear();//清除默认菜单项,可根据自己情况不清除,如果不清除就会默认有:新建、编辑、刷新三个菜单项
e.Items.Add("审核本行", "ShenHe_Current_Row", @"~\Images\16\bell.png");
e.Items.Add("审核本页", "ShenHe_Current_Page", @"~\Images\16\bin_empty.png");
}
}
protected void ASPxGridView1_ContextMenuItemClick(object sender, ASPxGridViewContextMenuItemClickEventArgs e)
{
switch (e.Item.Name)
{
case "ShenHe_Current_Row":
DataRowView current_row = ASPxGridView1.GetRow(ASPxGridView1.FocusedRowIndex) as DataRowView;
string bh = current_row.Row["编号"].ToString().Trim();
string shr =Session["xx_user"] == null ? "" : Session["xx_user"].ToString().Trim();
if (!string.IsNullOrEmpty(shr))
{
DateTime current_dateTime = SqlHelper.fwq_datetime();
object o_shcs = SqlHelper.ExecuteScalar("select count(*) as 次数 from 加油业务_审核记录 where rtrim(表名)='加油业务' and rtrim(索引编号)='" + bh + "'", new SqlParameter[] { });
int shcs = Convert.IsDBNull(o_shcs) ? 1 : Convert.ToInt32(o_shcs) + 1;
SqlHelper.ExecuteNoneQuery("insert into 加油业务_审核记录(表名,索引编号,审核人,审核状态,审核时间,审核次数) values('加油业务','" + bh + "','" + shr + "','已审核','" + current_dateTime + "'," + shcs + ")", new SqlParameter[] { });
current_row.Row["审核人"] = shr;
current_row.Row["审核状态"] = "已审核";
current_row.Row["审核时间"] = current_dateTime;
current_row.Row["审核次数"] = shcs;
}
break;
case "ShenHe_Current_Page":
DataRowView current_row2 = ASPxGridView1.GetRow(ASPxGridView1.FocusedRowIndex) as DataRowView;
string bh2 = current_row2.Row["编号"].ToString().Trim();
string shr2 =Session["xx_user"] == null ? "" : Session["xx_user"].ToString().Trim();
if (!string.IsNullOrEmpty(shr2))
{
DateTime current_dateTime = SqlHelper.fwq_datetime();
object o_shcs = SqlHelper.ExecuteScalar("select count(*) as 次数 from 加油业务_审核记录 where rtrim(表名)='加油业务' and rtrim(索引编号)='" + bh2+ "'", new SqlParameter[] { });
int shcs = Convert.IsDBNull(o_shcs) ? 1 : Convert.ToInt32(o_shcs) + 1;
SqlHelper.ExecuteNoneQuery("insert into 加油业务_审核记录(表名,索引编号,审核人,审核状态,审核时间,审核次数) values('加油业务','" + bh2 + "','" + shr2 + "','已审核','" + current_dateTime + "'," + shcs + ")", new SqlParameter[] { });
current_row2.Row["审核人"] = shr2;
current_row2.Row["审核状态"] = "已审核";
current_row2.Row["审核时间"] = current_dateTime;
current_row2.Row["审核次数"] = shcs;
}
break;
}
}
protected void ASPxGridView1_AddSummaryItemViaContextMenu(object sender, ASPxGridViewAddSummaryItemViaContextMenuEventArgs e)
{
if (e.SummaryItem.FieldName == "业务实际" && e.SummaryItem.SummaryType == SummaryItemType.Sum)
e.SummaryItem.ValueDisplayFormat = "{0:0.00}";
}
protected void ASPxGridView1_ContextMenuItemVisibility(object sender, ASPxGridViewContextMenuItemVisibilityEventArgs e)
{
if (e.MenuType == GridViewContextMenuType.Rows)
{
GridViewContextMenuItem menuItem1 = e.Items.Find(item => item.Name == "ShenHe_Current_Row") as GridViewContextMenuItem;
GridViewContextMenuItem menuItem2 = e.Items.Find(item => item.Name == "ShenHe_Current_Page") as GridViewContextMenuItem;
//如果审核人与登录用户相同则可以审核,否则不能审核
string current_user_str = Session["xx_user"] == null ? "" : Session["xx_user"].ToString().Trim();
if (current_user_str != "")
{
if (shr_cmb.Text.Trim() == current_user_str)
{
//分别为每一行设置菜单显示项
for (int i = 0; i < ASPxGridView1.VisibleRowCount; i++)
{
var value = ASPxGridView1.GetRowValues(i, "审核状态");
if (value.ToString().Trim() == "已审核")
{
e.SetVisible(menuItem1, i, false);
e.SetVisible(menuItem2, i, false);
}
else
{
e.SetVisible(menuItem1, i, true);
e.SetVisible(menuItem2, i, true);
}
}
}
else//如果审核人与登录用户不同不能审核
{
for (int i = 0; i < ASPxGridView1.VisibleRowCount; i++)
{
var value = ASPxGridView1.GetRowValues(i, "审核状态");
e.SetVisible(menuItem1, i, false);
e.SetVisible(menuItem2, i, false);
}
}
}
else
{
for (int i = 0; i < ASPxGridView1.VisibleRowCount; i++)
{
var value = ASPxGridView1.GetRowValues(i, "审核状态");
e.SetVisible(menuItem1, i, false);
e.SetVisible(menuItem2, i, false);
}
}
}
}
秋风写于淄博,业务联系及技术交流QQ:375172665