GridControl有很多型態,這邊用的是它的LayoutView型態

拉GridControl進來再變成LayouView型態,要讓LayoutView有

資料的方式就是先用它的Run Designer去Columns的地方新增Column

,每個Column要設定對應的資料庫欄位(FieldName這個),再去Layout

的地方排版,最後再把一個DataTable丟給GridControl就會自動繫結啦,

進入光棒正題

先宣告個變數

private int hotTrackRow = DevExpress.XtraGrid.GridControl.InvalidRowHandle;

然後宣告屬性,(注意我取的屬性名是有底線的hotTrackRow)


private int _hotTrackRow
 {
            get
            {
                return hotTrackRow;
            }

            set
            {
                if (hotTrackRow != value)
                {
                    int PreHot = hotTrackRow;
                    hotTrackRow = value;

                    layoutView1.RefreshRow(PreHot);
                    layoutView1.RefreshRow(hotTrackRow);

                    if (hotTrackRow >= 0)
                    {
                        gridControl1.Cursor = Cursors.Hand;
                    }
                    else
                    {
                        gridControl1.Cursor = Cursors.Default;
                    }
                }
            }
}

 

 

//再寫個函數判斷滑鼠有沒有在某個區域

 private bool IsMouseOverCardFieldValue(Point p)
{
            DevExpress.XtraGrid.Views.Layout.ViewInfo.LayoutViewHitInfo hInfo = layoutView1.CalcHitInfo(p);

            //FieldValue是LayoutView的一塊放值的區域,這裡用Intellisence可以去選各種不同的區域
            if (hInfo.HitTest == DevExpress.XtraGrid.Views.Layout.ViewInfo.LayoutViewHitTest.FieldValue)
                _hotTrackRow = hInfo.RowHandle;
            else
                _hotTrackRow = DevExpress.XtraGrid.GridControl.InvalidRowHandle;

         
            return hInfo.HitTest == DevExpress.XtraGrid.Views.Layout.ViewInfo.LayoutViewHitTest.FieldValue;
}

 

 

//然後寫個滑鼠移動的事件(從控制項去選事件點兩下)

 private void layoutView1_MouseMove(object sender, MouseEventArgs e)
{
            if (IsMouseOverCardFieldValue(new Point(e.X, e.Y)))
            {
                //Cursor.Current = new Cursor("door.cur");(這行是自訂滑鼠指標)
                gridControl1.Cursor = Cursors.Hand;
            }
            else
                gridControl1.Cursor = Cursors.Default;

}

 

//最後再寫個Style的事件(從控制項去選事件點兩下)

//如果是GridControl的GridView型態的話,是RowCellStyle這個事件

private void layoutView1_CustomFieldValueStyle(object sender, DevExpress.XtraGrid.Views.Layout.Events.LayoutViewFieldValueStyleEventArgs e)
{
            if (e.RowHandle == _hotTrackRow)
            {
                e.Appearance.BackColor = Color.Blue;

            }
}

 

//如此一來就有藍色的光棒啦!!

 

//在寫個Click事件,因為用LayoutViewCard的Click事件沒有用

//所以只好用LayoutView的事件,然後判斷滑鼠有沒有點到你想要點的區域

//有的話才執行按鈕的事件,沒有的話就沒事

  private void layoutView1_Click(object sender, EventArgs e)
{
            DataRowView dr = (DataRowView)(sender as LayoutView).GetFocusedRow();
           
            Point p = MousePosition;

            p = gridControl1.PointToClient(p);

            if (IsMouseOverCardFieldValue(p))
            {
                //這裡寫按下去按鈕後要做的事情
            }
}

 

//最後補上有可能用的到的NameSpace(因為太多了我也不知道哪個是哪個)

using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Layout;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraLayout;
using DevExpress.XtraLayout.Customization;
using DevExpress.XtraLayout.Utils;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.Controls;

welkingunther 發表在 痞客邦 PIXNET 留言(0) 人氣()