DevExpress_常用控制元件07_HScrollBar
2.0 PopupControlContainer控制元件
PopupControlContainer控制元件可以以面板的形式包含其他控制元件,可以DropDownButton控制元件的下拉框的格式出現,具體形式如圖:
PopupControlContainer控制元件通過DropDownButton控制元件的DropDownControl屬性進行繫結,在繫結後PopupControlContainer控制元件的AllowDrop屬性要設成True;
2.1 HScrollBar控制元件和VScrollBar控制元件
許多控制元件需要滾動條,像ListBoxControl、CheckedListControl控制元件中已經整合啦滾動條,所以 就不需要另加滾動條,但有些控制元件沒有整合,像PictureEdit控制元件,當顯示的圖片過長時,不能在其已有的區域顯示,就需要HScrollbar控制元件 和VScrollBar控制元件;
示例程式碼:
using DevExpress.XtraEditors;
private void Form1_Load(object sender, System.EventArgs e) {
hScrollBar1.Width = pictureBox1.Width;
hScrollBar1.Left = pictureBox1.Left;
hScrollBar1.Top = pictureBox1.Bottom;
hScrollBar1.Maximum = pictureBox1.Image.Width - pictureBox1.Width;
vScrollBar1.Height = pictureBox1.Height;
vScrollBar1.Left = pictureBox1.Left + pictureBox1.Width;
vScrollBar1.Top = pictureBox1.Top;
vScrollBar1.Maximum = pictureBox1.Image.Height - pictureBox1.Height;
}
int x = 0;
private void hScrollBar1_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e) {
x = hScrollBar1.Value;
pictureBox1.Refresh();
}
int y = 0;
private void vScrollBar1_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e) {
y = vScrollBar1.Value;
pictureBox1.Refresh();
}
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) {
e.Graphics.DrawImage(pictureBox1.Image, e.ClipRectangle, x, y, e.ClipRectangle.Width,
e.ClipRectangle.Height, GraphicsUnit.Pixel);
}
顯示效果:
2.2、DocumenManger控制元件
MDI子窗體可以通過DocumentManger控制元件以nativemdiview物件或tabbedview物件的形式展現出來;
通過其ViewCollection屬性新增View檢視(子窗體),可以新增TabbedView、WindowsUIView、WidgetView、NativeMdiView四種檢視;
通過Run Designer中Main下的Document給檢視新增Document文件(顯示的面板);
(NativeMdiView檢視)通過程式碼實現如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars.Docking2010;
using DevExpress.XtraBars.Docking2010.Views.NativeMdi;
using DevExpress.XtraEditors;
namespace DocumentManager_NativeMDI {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
int childCount = 0;
private void Form1_Load(object sender, EventArgs e) {
CreateDocumentManager();
for(int i = 0; i < 3; i++) {
AddChild();
}
}
void CreateDocumentManager() {
DocumentManager dm = new DocumentManager();
dm.MdiParent = this;
dm.View = new NativeMdiView();
}
void AddChild() {
Form childForm = null;
childForm = new Form();
childForm.Text = "Child Form " + (++childCount);
SimpleButton btn = new SimpleButton();
btn.Text = "Button " + childCount;
btn.Parent = childForm;
childForm.MdiParent = this;
childForm.Show();
}
}
}
實現效果如圖:
(TabbedView檢視)通過程式碼實現如下:
using System;
using System.Windows.Forms;
using DevExpress.XtraBars.Docking2010;
using DevExpress.XtraBars.Docking2010.Views.Tabbed;
using DevExpress.XtraEditors;
namespace DocumentManager_TabbedUI {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
void Form1_Load(object sender, EventArgs e) {
AddDocumentManager();
for(int i = 0; i < 3; i++) {
AddChildForm();
}
}
void AddDocumentManager() {
DocumentManager manager = new DocumentManager();
manager.MdiParent = this;
manager.View = new TabbedView();
}
int count;
void AddChildForm() {
Form childForm = new Form();
childForm.Text = "Child Form " + (++count).ToString();
SimpleButton btn = new SimpleButton();
btn.Text = "Button " + count.ToString();
btn.Parent = childForm;
childForm.MdiParent = this;
childForm.Show();
}
}
}
實現效果如圖:
2.Data & Analytics
2.1DataNavigator
2.1.1繫結資料來源:
例:List<int> datasource = new List<int>();
datasource.AddRange(new int[] { 0, 1, 2, 3, 4 });
myDataNavigator1.DataSource = datasource;
2.2GridLookUpEdit
2.1.2示例程式碼:
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;
using System.Data.OleDb;
// A lookup editor created at runtime.
GridLookUpEdit gridLookup;
// A navigator control to navigate the "Order Details" table.
DataNavigator dataNav;
// DataView for the "Order Details" table.
DataView dvMain;
// DataView for the "Products" table.
DataView dvDropDown;
//...
private void Form1_Load(object sender, System.EventArgs e) {
gridLookup = new GridLookUpEdit();
gridLookup.Bounds = new Rectangle(10, 40, 200, 20);
this.Controls.Add(gridLookup);
dataNav = new DataNavigator();
dataNav.Bounds = new Rectangle(10, 10, 250, 20);
this.Controls.Add(dataNav);
InitData();
InitLookUp();
dataNav.DataSource = dvMain;
}
private void InitData() {
// Dataset to provide data from the database
DataSet ds = new DataSet();
string connestionString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\DB\\nwind.mdb";
// Connect to the "Order Details" table
System.Data.OleDb.OleDbDataAdapter dbAdapter =
new OleDbDataAdapter("SELECT * FROM [Order Details]", connestionString);
// Load data from the "Order Details" table to the dataset
dbAdapter.Fill(ds, "Order Details");
// Connect to the "Products" table
dbAdapter = new OleDbDataAdapter("SELECT * FROM Products", connestionString);
// Load data from the "Products" table into the dataset
dbAdapter.Fill(ds, "Products");
DataViewManager dvm = new DataViewManager(ds);
dvMain = dvm.CreateDataView(ds.Tables["Order Details"]);
dvDropDown = dvm.CreateDataView(ds.Tables["Products"]);
}
private void InitLookUp() {
// Bind the edit value to the ProductID field of the "Order Details" table;
// the edit value matches the value of the ValueMember field.
gridLookup.DataBindings.Add("EditValue", dvMain, "ProductID");
// Prevent columns from being automatically created when a data source is assigned.
gridLookup.Properties.View.OptionsBehavior.AutoPopulateColumns = false;
// The data source for the dropdown rows
gridLookup.Properties.DataSource = dvDropDown;
// 下拉框顯示的欄位資料.
gridLookup.Properties.DisplayMember = "ProductName";
// The field matching the edit value.
gridLookup.Properties.ValueMember = "ProductID";
// Add two columns in the dropdown:
// A column to display the values of the ProductID field;
GridColumn col1 = gridLookup.Properties.View.Columns.AddField("ProductID");
col1.VisibleIndex = 0;
col1.Caption = "Product ID";
// A column to display the values of the ProductName field.
GridColumn col2 = gridLookup.Properties.View.Columns.AddField("ProductName");
col2.VisibleIndex = 1;
col2.Caption = "Product Name";
// Set column widths according to their contents.
gridLookup.Properties.View.BestFitColumns();
// Specify the total dropdown width.
gridLookup.Properties.PopupFormWidth = 300;
}
2.3 GridControl
1、設計資料來源並繫結欄位:
DataTable dt = new DataTable();
dt.Columns.Add("name", System.Type.GetType("System.String"));
dt.Columns.Add("sex", System.Type.GetType("System.String"));
dt.Columns.Add("age", System.Type.GetType("System.String"));
DataRow row=dt.NewRow();;
row["name"] = "11";
row["sex"] = "ss";
row["age"] = "age";
dt.Rows.Add(row);
//繫結欄位
gridView1.Columns[1].FieldName = "sex";
gridView1.Columns[2].FieldName = "age";
gridView1.Columns[0].FieldName = "name";
gridControl1.DataSource = dt;
2、 如何解決單擊記錄整行選中的問題
View->OptionsBehavior->EditorShowMode 設定為:Click
3、 如何新增一條記錄
(1)、gridView.AddNewRow()
(2)、實現 gridView_InitNewRow 事件
4、如何解決 GridControl 記錄能獲取而沒有顯示出來的問題
gridView.populateColumns();
5、如何讓行只能選擇而不能編輯(或編輯某一單元格)
(1)、View->OptionsBehavior->EditorShowMode 設定為:Click
(2)、View->OptionsBehavior->Editable 設定為:false
6、如何禁用 GridControl 中單擊列彈出右鍵選單
設定 Run Design->OptionsMenu->EnableColumnMenu 設定為:false
7、如何隱藏 GridControl 的 GroupPanel 表頭
設定 Run Design->OptionsView->ShowGroupPanel 設定為:false
8、如何禁用 GridControl 中列頭的過濾器 過濾器如下圖所示:
設定 Run Design->OptionsCustomization->AllowFilter 設定為:false
9、如何在查詢得到 0 條記錄時顯示自定義的字元提示/顯示 如圖所示:
方法如下:
//When no Records Are Being Displayed
private void gridView1_CustomDrawEmptyForeground(object sender, CustomDrawEventArgs e)
{
//方法一(此方法為GridView設定了資料來源繫結時,可用)
ColumnView columnView = sender as ColumnView;
BindingSource bindingSource = this.gridView1.DataSource as BindingSource;
if(bindingSource.Count == 0)
{
string str = "沒有查詢到你所想要的資料!";
Font f = new Font("宋體", 10, FontStyle.Bold);
Rectangle r = new Rectangle(e.Bounds.Top + 5, e.Bounds.Left + 5, e.Bounds.Right - 5, e.Bounds.Height - 5);
e.Graphics.DrawString(str, f, Brushes.Black, r); }
//方法二(此方法為GridView沒有設定資料來源繫結時,使用,一般使用此種方 法)
if (this._flag)
{
if (this.gridView1.RowCount == 0)
{ string str = "沒有查詢到你所想要的資料!"; Font f = new Font("宋體", 10, FontStyle.Bold);
Rectangle r = new Rectangle(e.Bounds.Left + 5, e.Bounds.Top + 5, e.Bounds.Width - 5, e.Bounds.Height - 5);
e.Graphics.DrawString(str, f, Brushes.Black, r); } } }
10、如何顯示水平滾動條
設定 this.gridView.OptionsView.ColumnAutoWidth = false;
列表寬度自適應內容
gridview1.BestFitColumns();
11、如何定位到第一條資料/記錄?
設定 this.gridView.MoveFirst()
12、如何定位到下一條資料/記錄? 設定 this.gridView.MoveNext()
13、如何定位到最後一條資料/記錄?
設定 this.gridView.MoveLast()
14、設定成一次選擇一行,並且不能被編輯
this.gridView1.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus;
this.gridView1.OptionsBehavior.Editable = false;
this.gridView1.OptionsSelection.EnableAppearanceFocusedCell = false;
15、如何顯示行號? private void gvPayList_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e) { e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far; if (e.Info.IsRowIndicator) { if (e.RowHandle >= 0) { e.Info.DisplayText = (e.RowHandle + 1).ToString(); } else if (e.RowHandle < 0 && e.RowHandle > -1000) { e.Info.Appearance.BackColor = System.Drawing.Color.AntiqueWhite; e.Info.DisplayText = "G" + e.RowHandle.ToString(); } } }
16、如何讓各列頭禁止移動?
設定 gridView1.OptionsCustomization.AllowColumnMoving = false;
17、如何讓各列頭禁止排序?
設定 gridView1.OptionsCustomization.AllowSort = false;
18、如何禁止各列頭改變列寬?
設定 gridView1.OptionsCustomization.AllowColumnResizing = false;
19.拖動滾動條時固定某一列
設定Columns,選擇要固定的列。設定Fixed屬性,可以選擇:固定在左邊、固定在右邊、不固定。 |