public partial class Main : DevExpress.XtraEditors.XtraForm
{
public Main()
{
InitializeComponent();
gc1.DataSource = BindData().Tables["MainView"];
//初始化默认所有折叠
(gc1.MainView as DevExpress.XtraGrid.Views.Grid.GridView).CollapseAllDetails();
gv1.OptionsDetail.ShowDetailTabs = false;//隐藏Tab
gv2.OptionsDetail.ShowDetailTabs = false;//隐藏Tab:son
gv3.OptionsDetail.ShowDetailTabs = false;//隐藏Tab:GrandSon
gv1.OptionsView.ShowGroupPanel = false;//隐藏panel
gv2.OptionsView.ShowGroupPanel = false;
gv3.OptionsView.ShowGroupPanel = false;
}
private void Main_Load(object sender, EventArgs e)
{
gv1.RowClick += gv1_RowClick;
}
DataSet BindData()
{
DataSet ds = new DataSet("TEST");
DataTable dtsource1 = new DataTable("测试");
dtsource1.Columns.Add("ID", typeof(int));//主键ID
dtsource1.Columns.Add("LEVEL", typeof(int));//层级
dtsource1.Columns.Add("BOM_PID", typeof(int));//父级主键ID
dtsource1.Columns.Add("NAME", typeof(string));//名称
dtsource1.Rows.Add(new object[] { 1, 0, 1, "NAME1" });
dtsource1.Rows.Add(new object[] { 2, 0, 1, "NAME1" });
dtsource1.Rows.Add(new object[] { 3, 1, 1, "NAME1" });
dtsource1.Rows.Add(new object[] { 4, 1, 1, "NAME1" });
dtsource1.Rows.Add(new object[] { 5, 2, 3, "NAME1" });
dtsource1.Rows.Add(new object[] { 6, 2, 3, "NAME1" });
DataTable dtMain = GetChild(dtsource1, "MainView", 0);
DataTable dtSon = GetChild(dtsource1, "son", 1);
DataTable dtGrandSon = GetChild(dtsource1, "GrandSon", 2);
ds.Tables.Add(dtMain);
ds.Tables.Add(dtSon);
ds.Tables.Add(dtGrandSon);
ds.Relations.Add("son", dtMain.Columns["ID"], dtSon.Columns["BOM_PID"], false);//建立关系
ds.Relations.Add("GrandSon", dtSon.Columns["ID"], dtGrandSon.Columns["BOM_PID"], false);
return ds;
}
/// <summary>
/// 获取指定等级子表的信息
/// </summary>
/// <param name="dtSource">数据源</param>
/// <param name="childName">子表名称</param>
/// <param name="level">子表等级</param>
/// <returns></returns>
DataTable GetChild(DataTable dtSource, string childName, int level)
{
DataTable dtReturn = dtSource.Copy();
DataTable dtNew = new DataTable();
DataRow[] dtRowSelect = dtSource.Select("LEVEL=" + level);
for (int i = 0; i < dtReturn.Columns.Count; i++)
{
dtNew.Columns.Add(dtReturn.Columns[i].ColumnName);
}
if (dtRowSelect.Length > 0)
{
for (int i = 0; i < dtRowSelect.Length; i++)
{
DataRow dtnew = dtRowSelect[i];
dtNew.Rows.Add(dtRowSelect[i].ItemArray);
}
}
dtNew.TableName = childName;
return dtNew;
}
private void gv1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
{
//GridView using DevExpress.XtraGrid.Views.Grid
string gv1FocusedColumns = gv1.FocusedColumn.Caption;//当前一级点击行
if (true)//条件逻辑
{
MessageBox.Show(string.Format("当前一级点击行:{0}", gv1.FocusedRowHandle));
}
CloseAllExceptCurExpand(e.RowHandle);//除当前一级行,其他一级行全部关闭
gv2.RowCellClick -= gv2_RowCellClick;//一级点击附加二级点击事件
gv2.RowCellClick += gv2_RowCellClick;//一级点击附加二级点击事件
}
private void gv2_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
{
GridView detailView = gv1.GetDetailView(gv1.FocusedRowHandle, 0) as GridView;//获取一级点击下的子Gridview点击行
if (detailView != null)
{
MessageBox.Show(string.Format("当前二级点击行:{0}", detailView.FocusedRowHandle));
gv2.RowCellClick -= gv2_RowCellClick;
gv3.RowCellClick -= gv3_RowCellClick;
gv3.RowCellClick += gv3_RowCellClick;
}
}
private void gv3_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
{
GridView detailView = gv1.GetDetailView(gv1.FocusedRowHandle, 0) as GridView;
if (detailView != null)
{
GridView gv3View = detailView.GetDetailView(detailView.FocusedRowHandle, 0) as GridView;
if (gv3View != null)
{
MessageBox.Show(string.Format("当前三级点击行:{0}", gv3View.FocusedRowHandle));
gv3.RowCellClick -= gv3_RowCellClick;
string focusedColumn = gv3View.FocusedColumn.Caption;
}
}
}
//展开
public void ExpandAllRows(DevExpress.XtraGrid.Views.Grid.GridView view)
{
view.BeginUpdate();
try
{
int dataRowCount = view.DataRowCount;
for (int rHandle = 0; rHandle < dataRowCount; rHandle++)
{
view.SetMasterRowExpanded(rHandle, true);
}
}
finally
{
view.EndUpdate();
}
}
//一级:折叠除当前展开外的所有其他展开项
public void CloseAllExceptCurExpand(int parentRowHandle)
{
int rowsCount = (gc1.DataSource as DataTable).Rows.Count;
for (int i = 0; i < rowsCount; i++)
{
if (i != parentRowHandle)
{
gv1.SetMasterRowExpanded(i, false);
}
}
}
}
双击展开和折叠:
//设置当前点击行detail展开与折叠
public void CloseExpandCur(GridView gv)
{
int curRowHandle = gv.FocusedRowHandle;//获取当前视图点击的行
bool expend1 = gv.GetMasterRowExpanded(curRowHandle);//获取当前点击的行是否折叠
gv.SetMasterRowExpanded(curRowHandle, !expend1);//设置展开折叠
}
实例使用方法1
//private void gv1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
//{
// if (e.Button == MouseButtons.Left && e.Clicks == 2)//双击展开和双击折叠
// {
// GridView gvBindCloseExpand = gv1 as GridView;
// CloseExpandCur(gvBindCloseExpand);
// }
//}
//背景颜色绘制
private void gv1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
{
e.Cache.FillRectangle(System.Drawing.Brushes.Red, e.Bounds);
}