给列表页面单元格添加右键菜单contextMenuStrip 原

  • 2019 年 12 月 19 日
  • 笔记

 

控制哪几列能右键出菜单:

 private void gdvDetail_RowCellClick(object sender, RowCellClickEventArgs e)          {              DataTable dt = gdcDetail.DataSource as DataTable;                if (((e.Column.VisibleIndex == 2 )                            || (e.Column.VisibleIndex == 3 )                                    || (e.Column.VisibleIndex == 4 )                                        || (e.Column.VisibleIndex == 5) )                                           && e.Button  == MouseButtons.Right&&gdvDetail.GetFocusedRowCellValue(e.Column)!=null) {                  iIndex = e.Column.AbsoluteIndex;                  contextMenuStrip1.Show(this.gdcDetail, e.Location);              }          }            private int iIndex=0;

右键菜单的按钮事件:

  private void finishToolStripMenuItem_Click(object sender, EventArgs e)          {              int[] i = gdvDetail.GetSelectedRows();              //DevExpress.XtraGrid.Columns.GridColumn[] sss = gdvDetail.GetSelectedCells(i[0]);              if (iIndex == 2) {                  gdvDetail.SetFocusedRowCellValue("EB_PICKING_STATUS", "Y");                  iIndex = 0;                  gdvDetail.RefreshData();              }              if (iIndex == 6)              {                  gdvDetail.SetFocusedRowCellValue("PRA_PICKING_STATUS", "Y");                  iIndex = 0;                  gdvDetail.RefreshData();              }              if (iIndex == 10)              {                  gdvDetail.SetFocusedRowCellValue("SUB_PICKING_STATUS", "Y");                  iIndex = 0;                  gdvDetail.RefreshData();              }              if (iIndex == 14)              {                  gdvDetail.SetFocusedRowCellValue("MAA_PICKING_STATUS", "Y");                  iIndex = 0;                  gdvDetail.RefreshData();              }                //object s = gdvDetail.GetFocusedRow();              //int[] i = gdvDetail.GetSelectedRows();              //DevExpress.XtraGrid.Columns.GridColumn[] sss = gdvDetail.GetSelectedCells(i[0]);              //string d = gdvDetail.GetRowCellValue(i[0], "A").ToString();              //string ss = gdvDetail.GetFocusedValue().ToString();              //gdvDetail.SetFocusedRowCellValue("A", "OK");          }

设置单元格改变背景色:

   private void gdvDetail_RowCellStyle(object sender, RowCellStyleEventArgs e)          {              if (e.RowHandle>-1)              {                  string s = gdvDetail.GetRowCellValue(e.RowHandle, "EB_PICKING_STATUS").ToString();                  int i = gdvDetail.GetFocusedDataSourceRowIndex();                  if (e.Column.VisibleIndex == 2)                  {                      if (gdvDetail.GetDataRow(i)["EB_ABNORMAL_FLAG"].ToString() == "Y")                      {                          e.Appearance.BackColor = Color.Red;                      }                      else if (gdvDetail.GetDataRow(i)["EB_PICKING_STATUS"].ToString() == "Y")                      {                          e.Appearance.BackColor = Color.LightGreen;                      }                      else if (gdvDetail.GetDataRow(i)["EB_CHANGE_FLAG"].ToString() == "Y")                      {                          e.Appearance.BackColor = Color.Yellow;                      }                  }                  else if (e.Column.VisibleIndex == 3)                  {                      if (gdvDetail.GetDataRow(i)["PRA_ABNORMAL_FLAG"].ToString() == "Y")                      {                          e.Appearance.BackColor = Color.Red;                      }                      else if (gdvDetail.GetDataRow(i)["PRA_PICKING_STATUS"].ToString() == "Y")                      {                          e.Appearance.BackColor = Color.LightGreen;                      }                      else if (gdvDetail.GetDataRow(i)["PRA_CHANGE_FLAG"].ToString() == "Y")                      {                          e.Appearance.BackColor = Color.Yellow;                      }                  }                  else if (e.Column.VisibleIndex == 4)                  {                      if (gdvDetail.GetDataRow(i)["SUB_ABNORMAL_FLAG"].ToString() == "Y")                      {                          e.Appearance.BackColor = Color.Red;                      }                      else if (gdvDetail.GetDataRow(i)["SUB_PICKING_STATUS"].ToString() == "Y")                      {                          e.Appearance.BackColor = Color.LightGreen;                      }                      else if (gdvDetail.GetDataRow(i)["SUB_CHANGE_FLAG"].ToString() == "Y")                      {                          e.Appearance.BackColor = Color.Yellow;                      }                  }                  else if (e.Column.VisibleIndex == 5)                  {                      if (gdvDetail.GetDataRow(i)["MAA_ABNORMAL_FLAG"].ToString() == "Y")                      {                          e.Appearance.BackColor = Color.Red;                      }                      else if (gdvDetail.GetDataRow(i)["MAA_PICKING_STATUS"].ToString() == "Y")                      {                          e.Appearance.BackColor = Color.LightGreen;                      }                      else if (gdvDetail.GetDataRow(i)["MAA_CHANGE_FLAG"].ToString() == "Y")                      {                          e.Appearance.BackColor = Color.Yellow;                      }                  }                }          }  

VisibleIndex:可见列的索引;AbsoluteIndex:绝对索引,包括隐藏列。