Developer Express XtraGrid使用技巧
1.基本grid的用法
1.1 gridControl-->view-->gridView
绑定到grid上的字段可以拖动删除,在任意字段上右键点column chooser 可以将删除的字段添加进来
字段上右键选择实现选定需要的信息和排序
字段上右键选择分组(group by column),也可以直接将字段拖到上面实现分组
字段上右键选择group by box后可以取消分组栏,(不能实现拖动分组,但可以选择分组)
字段上右键选择best fit可以实现该字段最佳宽度(便于查看)
1.2 gridControl-->view-->cardView
以卡片形式存放数据
可以自己设定过滤选项
1.3 gridControl-->view-->banderGridView
将字段分段显示,前提是给gridControl先绑定一个dataset
动态绑定的时候不能分段,也不能分行
1.3 gridControl-->view-->advBanderGridView
高级分段显示,动态绑定的字段可以在前台向下拖动后分段,但效果不好,相当于分行显示的样子
1.4 else
可以通过create ner view增加一个view ,但程序只显示你选定的view
zoom的使用 快速上升,也就是选择子项后单独显示(上到最高显示)
1.5 feature browser
-->priview 可以指定在每行中预览的字段
-->filtering 可以自动过滤(optingview.showAutoFiterRow)
1.7 Master-Detail 绑定的问题
首先在gridControl1中指定dataSource 的绑定是主表
然后在level设计器中添加层(retrieve details)
最后用代码绑定:
using (SqlConnection con = new SqlConnection(SqlHepler.CONN_STRING))
{
//SqlDataAdapter sda1 = new SqlDataAdapter("select * from Orders",con);
//sda1.Fill(this.northwindDataSet.Orders);
SqlDataAdapter sda2 = new SqlDataAdapter("select * from [Order Details]",con);
sda2.Fill(this.northwindDataSet.Order_Details);
}
1.8 summary的用法
//If the GridOptionsView.ShowFooter option is set,
//the summary(of course some else can ) can be displayed within the footer cell.
gridView1.OptionsView.ShowFooter = true;
//初始化,也可以不用指定
colOrderID.SummaryItem.FieldName = "OrderID";
colOrderID.SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count;
2.XtraGrid的打印
借用别人写的一个类
class DevPrint
{
//*********************字段*********************
private string pageHeaderName="";
private string pageFooterName="";
private bool isPrintPage=true;
private bool isPrintDate=true;
private int headerLocation=1;
private int footerLocation=1;
private int pageLocation=3;
private int dateLocation=5;
private bool enableEditPage = true;
private System.Drawing.Font pageHeaderFont = null;
private System.Drawing.Font pageFooterFont = null;
private System.Drawing.Printing.Margins devMargins = null;
private System.Drawing.Printing.PaperKind devPaperKind = System.Drawing.Printing.PaperKind.A4;//可以自定义修改纸张
private DevExpress.XtraPrinting.PageHeaderArea PHA = new DevExpress.XtraPrinting.PageHeaderArea();
private DevExpress.XtraPrinting.PageFooterArea PFA = new DevExpress.XtraPrinting.PageFooterArea();
//*********************字段*********************
//*********************属性*********************
//标题
public string PageHeaderName
{
get
{
return pageHeaderName;
}
set
{
pageHeaderName=value;
}
}
//页脚
public string PageFooterName
{
get
{
return pageFooterName;
}
set
{
pageFooterName = value;
}
}
//是否打印页数
public bool IsPrintPage
{
get
{
return isPrintPage;
}
set
{
isPrintPage = value;
}
}
//是否打印时间
public bool IsPrintDate
{
get
{
return isPrintDate;
}
set
{
isPrintDate = value;
}
}
//header的位置(左,中,右)
public int HeaderLocation
{
get
{
return headerLocation;
}
set
{
headerLocation=value;
}
}
//footer的位置(左,中,右)
public int FooterLocation
{
get
{
return footerLocation;
}
set
{
footerLocation=value;
}
}
//页数的位置
public int PageLocation
{
get
{
return pageLocation;
}
set
{
pageLocation=value;
}
}
//时间的位置
public int DateLocation
{
get
{
return dateLocation;
}
set
{
dateLocation=value;
}
}
//是否允许编辑页面
public bool EnableEditPage
{
get
{
return enableEditPage;
}
set
{
enableEditPage = value;
}
}
//标题字体
public System.Drawing.Font PageHeaderFont
{
get
{
return pageHeaderFont;
}
set
{
pageHeaderFont = value;
}
}
//页脚字体
public System.Drawing.Font PageFooterFont
{
get
{
return pageFooterFont;
}
set
{
pageFooterFont = value;
}
}
//页边距
public System.Drawing.Printing.Margins DevMargins
{
get
{
return devMargins;
}
set
{
devMargins = value;
}
}
//纸张类型
public System.Drawing.Printing.PaperKind DevPaperKind
{
get
{
return devPaperKind;
}
set
{
devPaperKind = value;
}
}
//*********************字段*********************
private void PageHeaderFooterSettings()
{
PHA.Content.Clear();
PFA.Content.Clear();
string[] stringsPHA = new string[] {"","",""};
string[] stringsPFA = new string[] {"","",""};
switch(headerLocation)
{
case 0:stringsPHA[0]=pageHeaderName;
break;
case 1:stringsPHA[1]=pageHeaderName;
break;
case 2:stringsPHA[2]=pageHeaderName;
break;
default:stringsPHA[1]=pageHeaderName;
break;
};
switch(footerLocation)
{
case 0:stringsPFA[0]=pageFooterName;
break;
case 1:stringsPFA[1]=pageFooterName;
break;
case 2:stringsPFA[2]=pageFooterName;
break;
default:stringsPFA[1]=pageFooterName;
break;
};
if (isPrintPage)
{
switch(pageLocation)
{
case 0:stringsPHA[0]=stringsPHA[0]+"[Page # of Pages #]";
break;
case 1:stringsPHA[1]=stringsPHA[1]+"[Page # of Pages #]";
break;
case 2:stringsPHA[2]=stringsPHA[2]+"[Page # of Pages #]";
break;
case 3:stringsPFA[2]=stringsPFA[2]+"[Page # of Pages #]";
break;
case 4:stringsPFA[1]=stringsPFA[1]+"[Page # of Pages #]";
break;
case 5:stringsPFA[0]=stringsPFA[0]+"[Page # of Pages #]";
break;
default:stringsPFA[2]=stringsPFA[2]+"[Page # of Pages #]";
break;
};
}
if (isPrintDate)
{
switch(dateLocation)
{
case 0:stringsPHA[0]=stringsPHA[0]+"[Date Printed]";
break;
case 1:stringsPHA[1]=stringsPHA[1]+"[Date Printed]";
break;
case 2:stringsPHA[2]=stringsPHA[0]+"[Date Printed]";
break;
case 3:stringsPFA[2]=stringsPFA[2]+"[Date Printed]";
break;
case 4:stringsPFA[1]=stringsPFA[1]+"[Date Printed]";
break;
case 5:stringsPFA[0]=stringsPFA[0]+"[Date Printed]";
break;
default:stringsPFA[0]=stringsPFA[0]+"[Date Printed]";
break;
};
}
PHA.Content.AddRange(stringsPHA);
PFA.Content.AddRange(stringsPFA);
if (pageHeaderFont != null)
{
try
{
PHA.Font = pageHeaderFont;
}
catch
{
PHA.Font = new Font(new System.Drawing.FontFamily("楷体_GB2312"),20,System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point);
}
}
else
{
PHA.Font = new Font(new System.Drawing.FontFamily("楷体_GB2312"),20,System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point);
}
if (pageFooterFont != null)
{
try
{
PFA.Font = pageFooterFont;
}
catch
{
PFA.Font = new Font(new System.Drawing.FontFamily("楷体_GB2312"),9,System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point);
}
}
}
public void ShowDevPreview(DevExpress.XtraPrinting.IPrintable printComponent)
{
PageHeaderFooterSettings();
DevExpress.XtraPrinting.PrintingSystem ps = new DevExpress.XtraPrinting.PrintingSystem();
DevExpress.XtraPrinting.PrintableComponentLink pc =new DevExpress.XtraPrinting.PrintableComponentLink();
pc.Component = printComponent;
pc.Landscape = true;
//是否指定页边距尺寸
if (devMargins != null)
pc.Margins = devMargins;
//是否定义纸张
if (devPaperKind != System.Drawing.Printing.PaperKind.A4)
pc.PaperKind = devPaperKind;
//标题和页脚的显示
pc.PageHeaderFooter = new DevExpress.XtraPrinting.PageHeaderFooter(PHA,PFA);
pc.CreateDocument(ps);
pc.EnablePageDialog = enableEditPage;
pc.ShowPreview();
}
}