基本用法
1.新建一个winformAPP 放一个gridControl 为gridC 再放一个button 用法与dataGrid一样
代码如下:
/// <summary> /// 显示数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { using (SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=123;database=test")) { using (SqlDataAdapter ada = new SqlDataAdapter("select * from users ", con)) { DataTable dt = new DataTable(); ada.Fill(dt); grdC.DataSource = dt; } } }
2.取选中行标
GridView.FocusedRowHandle
3.不可编辑
gridview -->OptionsBehavior -->Editable=false
4.主从表(重点是 dataset 里有两个可以关联的表 建立两表的联系 再给dataset)
string str = @"SELECT VPG.CustomerCode AS 客户编码, cus.CustomerName AS 客户名称, VPG.ProtectionGroupName AS 保护机组, VPG.ConfiguredRpoSenconds RPO秒, VPG.Status AS 状态, VPG.VmsCount AS 虚拟机数量, VPG.Priority AS 优先级, VPG.ProvisionedStorageInMB AS 总空间, VPG.UsedStorageInMB AS 已用空间,ProtectionGroupId FROM dbo.Tbl_UDC_ProtectionGroupStatus AS VPG LEFT OUTER JOIN dbo.Tbl_UDC_CustomerList AS cus ON VPG.CustomerCode = cus.CustomerCode where 1=1 "; //VPG.ActualRPO, VPG.ActualRTO, VPG.ProtectionGroupId, VPG.SiteId 这几个可能没有用 if (this.CusCode != "") str = str + string.Format(" and VPG.customerCode = '{0}'", this.CusCode); str = str + @" select VmName VM名称,Status 状态,Priority 优先级,ProvisionedStorageInMB 总容量 ,JournalUsedStorageMb 已用空间,VpgId,CustomerCode from Tbl_UDC_VmResourceInfo"; DataSet dsMD = dh.GetDataSet(str); DataRelation relation = new DataRelation("VPG", dsMD.Tables[0].Columns["ProtectionGroupId"], dsMD.Tables[1].Columns["VpgId"]); dsMD.Relations.Add(relation); gcVPG.DataSource = dsMD.Tables[0];
5.样式
gridControl-->LookAndFeel-->UsedDefaultLookAndFree=false-->skinName(改下下就可以了)
6.动态创建 GridControl 读取数据 并第一列设定记录行数
GridControl grid = new GridControl(); GridView gv = new GridView(); gv.OptionsView.ShowFooter = true; gv.OptionsView.ShowGroupPanel = false; gv.OptionsView.ColumnAutoWidth = false; grid.MainView = gv; grid.Dock = DockStyle.Fill; this.Controls.Add(grid); using (SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=123;database=test")) { using (SqlDataAdapter adp = new SqlDataAdapter("select * from tbl_test ", con)) { DataTable dt = new DataTable(); adp.Fill(dt); grid.DataSource = dt; } } gv.Columns[0].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count;