GridControl的常用操作

1.GridView的回车跳转单元格和换行

        private void gridView1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)//按下回车
            {
                string name = gridView1.FocusedColumn.FieldName;//获得字段名
                if (name == "INPUT_CODE")//回车跳转到这个字段的时候,添加新行
                {
                    DataTable dt = this.gridControl1.DataSource as DataTable;//获得gridControl的数据源
                    dt.Rows.Add(dt.NewRow());//添加新行
                }
                SendKeys.Send("{Tab}");// 按Enter键时调用按Tab键时候的默认方法。
            }
        }

2.GridView添加行号

先设置一下IndicatorWinth属性,调一下行号的合适宽度

  private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator)
            {
                e.Info.DisplayText = Convert.ToString(e.RowHandle + 1);
            }
        }

3.GridView添加右键菜单

   从工具箱里添加contextMenuStrip控件,设置右键菜单的选项

 

双击右键选项,在后台写相应的删除操作

  private void MenuItemDel_Click(object sender, EventArgs e)
        {
             int rowHandle = this.gridView1.FocusedRowHandle;//获得焦点的行
             DataTable dtPrice = this.gridControl1.DataSource as DataTable;//数据源
             dtPrice.Rows.RemoveAt(rowHandle);//移除获得焦点的行
             if (dtPrice.Rows.Count <= 0)
                  dtPrice.Rows.Add(dtPrice.NewRow());//添加新的行

      }

4.在GridView中添加控件(如图)

设置DisplayMember和ValueMember的值

给SearchLookUpSupper绑定数据源

 if (drugServer.DrugInfoService.InputFirmDict(ref outFirm))//得到查询出的DataTable类型的数据源的值
                {
                    outFirm.Columns["SUPPLIER_ID"].Caption = "厂家标识";//设置给我用户看的字段名称
                    outFirm.Columns["SUPPLIER"].Caption = "厂家";
                    outFirm.Columns["INPUT_CODE"].Caption = "输入码";
                    this.SearchLookUpSupper.DataSource = outFirm;//绑定数据源
                }

 

posted @ 2016-01-22 20:59  高空燕子飞过  阅读(864)  评论(0编辑  收藏  举报