1. 程式人生 > 實用技巧 >DevExpress彈框、右鍵選單、Grid的使用

DevExpress彈框、右鍵選單、Grid的使用

很重要!!!Dev為了區分winform的命名,會把一些新新增的屬性放在Properties物件裡!!找不到想要的屬性,記得到裡面找找哦!

一、下拉框

在這裡假設我們的資料來源是db.List(),在這裡我主要講述關於資料來源繫結的方式。

1、winform的下拉框【comboBox】

!!下拉框的繫結需要三個:資料來源,顯示值,隱藏值

    this.comboBox.DisplayMember="DisplayName";//資料來源欄位名稱【顯示值】
    this.comboBox.ValueMember = "ValueName";//資料來源欄位名稱【隱藏值】
    this.comboBox.DataSource=db.toList();//繫結資料來源

2、Dev的下拉框【comboBoxEdit和lookUpEdit】

!!前者沒辦法繫結資料來源,只能通過遍歷的方式去新增資料,後者可以繫結資料來源,且功能強大!!很重要!!!Dev為了區分winform的命名,會把一些新新增的屬性放在Properties物件裡!!找不到想要的屬性,記得到裡面找找哦!

(1)comboBoxEdit的遍歷繫結資料

    this.comboBoxEdit1.Properties.Items.Add("請選擇");
    this.comboBoxEdit1.Properties.Items.Add("中國");
    this.comboBoxEdit1.Properties.Items.Add("美國");

(2)lookUpEdit的遍歷繫結資料

   this.lookUpEdit1.Properties.DisplayMember ="DisplayName";//資料來源欄位名稱【顯示值】
   this.lookUpEdit1.Properties.ValueMember = "ValueName";//資料來源欄位名稱【隱藏值】
   this.lookUpEdit1.Properties.DataSource = db.toList();//繫結資料來源

那麼這個到底強大在哪裡呢??它可以顯示更多的欄位,而不是在侷限於一個顯示欄位。

二、彈出窗

1、winform的彈出窗【MessageBox】

  MessageBox.Show("內容");

2、Dev的彈出窗【XtraMessageBox和alertControl右下角彈出窗】

(1)XtraMessageBox用法

  XtraMessageBox.Show("內容");

(2)alertControl用法(需要往視窗拖一下控制元件)

   this.alertControl1.Show(this,"提示","您有一封訊息");//this當前窗體,“提示”標題,“您有一封訊息”內容


點選事件是AlertClick

三、右鍵選單

1、winform的右鍵選單【contextMenuStrip】

這個沒啥說的類,在需要的控制元件上繫結一下就好了

2、Dev的右鍵選單【popupMenu和barManager】


!!在這裡提示一下,這是一個組合控制元件,單獨用popupMenu就會出現上面的情況,我們還需要控制元件barManager


當倆個控制元件都拖動後,會自動形成繫結


這樣子才可以使用嘞!

!popupMenu和barManager案例

第一步、新增右鍵選單內容

第二步、頁面實現右鍵功能

給視窗新增一個點選事件

    private void XtraForm3_MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)//如果點選為右鍵
        {
            //啟用右鍵選單
            //new Point(Cursor.Position.X,Cursor.Position.Y)滑鼠所在焦點
            this.popupMenu1.ShowPopup(new Point(Cursor.Position.X, Cursor.Position.Y));
        }
    }

四、Grid控制元件獲取選中的資料

1、winform獲取選中資料

(1)、單個選中

    private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        //獲取當前點中單元格的值
        string content = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
        //獲取當前選中行資料
        =var data = this.dataGridView1.Rows[e.RowIndex].DataBoundItem;
        MessageBox.Show(content);
    }

(2)、整行選中

在窗體載入事件,設定

    //設定為整行選中
    this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
    private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        //獲取當前點中單元格的值
        string content = this.dataGridView1.SelectedRows[0].Cells[e.ColumnIndex].Value.ToString();
            //獲取當前選中行資料
            var data = this.dataGridView1.SelectedRows[0].DataBoundItem;
            MessageBox.Show(content);
    }

2、Dev獲取選中資料【gridControl】

    private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            //焦點所在的資料
            string content = this.gridView1.GetFocusedValue().ToString();
            //焦點所在的資料行
            var  data = this.gridView1.GetFocusedRow();
            MessageBox.Show(content);
        }

結束語

學習要一步一步來,一步吃不成大胖子!