嘴强王者

导航

Devexpress treelist两张表父子节点设置、筛选、分页、排序、页面跳转demo

效果图

 

 

 

 

网上查了很多例子自己结合和修改了一下。最下方的分页跳转是dev的datapager控件。控件的属性事件自己研究一下。

代码如下

public partial class MMDefinitionQueryForm : Form
{
private IMMwDefinitionsCoBO immwDefinitionsCoBO_0;
private IMMwDefVersCoBO immwDefVersCoBO_0;
public MMDefinitionQueryForm()
{
this.InitializeComponent();
this.immwDefinitionsCoBO_0 = ObjectContainer.BuildUp<IMMwDefinitionsCoBO>();
this.immwDefVersCoBO_0 = ObjectContainer.BuildUp<IMMwDefVersCoBO>();
Resultlist.OptionsBehavior.ReadOnly = true; //只读
Resultlist.OptionsBehavior.Editable = false; //不可编辑
}
//页行数
//public int pagesize = 1;
//当前页
public int pageIndex = 1;
//总页数
public int pageCount;
//排序规则
public string ordername = "DefID";
public string order = "ASC";
public int orderint = 0;
public string DefID = null;//设备编号
public string version = null;//物料版本
public string DefPK = null;//设备识别码
//public string EqpDefName;
//分页及数据绑定
public void BindPageGridList()
{
try
{
nvgtDataPager.Buttons.CustomButtons[0].Enabled = true;
nvgtDataPager.Buttons.CustomButtons[1].Enabled = true;
nvgtDataPager.Buttons.CustomButtons[2].Enabled = true;
nvgtDataPager.Buttons.CustomButtons[3].Enabled = true;
//分页获取数据列表
DataTable dt = Getdata();
if (pageIndex == 1 || pageIndex == 0)
{
nvgtDataPager.Buttons.CustomButtons[0].Enabled = false;
nvgtDataPager.Buttons.CustomButtons[1].Enabled = false;
}
if (dt.Rows.Count == 0)
{
pageIndex = 0;
}
//最后页时获取真实记录数
if (pageCount == pageIndex)
{
nvgtDataPager.Buttons.CustomButtons[2].Enabled = false;
nvgtDataPager.Buttons.CustomButtons[3].Enabled = false;
}

Bind(dt);
// nvgtDataPager.DataSource = dt;
nvgtDataPager.TextStringFormat = string.Format("第 {0}页, 共 {1}页", pageIndex, pageCount);
}
catch (Exception ex)
{
ExceptionPolicy.HandleException(ex, ExceptionPolicy.BusinessLogicDefaultPolicy);
}

}
// 按钮点击事件
private void nvgtDataPager_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
{
ShowEvent(e.Button);
}
// 分页事件处理
void ShowEvent(NavigatorButtonBase button)
{
//string type = button.ButtonType.ToString();
NavigatorCustomButton btn = (NavigatorCustomButton)button;
string type = btn.Tag.ToString();
if (type == "首页")
{
pageIndex = 1;
}
if (type == "下一页")
{
pageIndex++;
}
if (type == "末页")
{
pageIndex = pageCount;
}

if (type == "上一页")
{
pageIndex--;
}
//绑定分页控件和GridControl数据
BindPageGridList();
}
//数据查询方法
public DataTable Getdata()
{
string where = " DefName Like '%" + queryname.Text.Trim() + "%' and DefID Like '%" + querycode.Text.Trim() + "%'";
int pagecount;
DataTable dt = immwDefinitionsCoBO_0.ExecPaging(ordername, order, where, pageIndex, out pagecount, 5);//参数顺序:排序字段,排序方式,条件,页索引,总页数
pageCount = pagecount;
return dt;
}
//给treelist绑定数据,
public void Bind(DataTable dt)
{
dt.DefaultView.Sort = ordername + " " + order;//可能无用
dt = dt.DefaultView.ToTable();
this.Resultlist.ClearNodes();
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
TreeListNode node = Resultlist.AppendNode("DefID", null);
node.SetValue(物料识别号, dt.Rows[i]["DefPK"]);
node.SetValue(物料编码, dt.Rows[i]["DefID"]);
node.SetValue(物料名称, dt.Rows[i]["DefName"]);
node.SetValue(物料大类, dt.Rows[i]["TypeCD"]);
node.SetValue(物料小类, dt.Rows[i]["ClassName"]);
node.SetValue(物料描述, dt.Rows[i]["Descript"]);
GetChildNode(node, dt.Rows[i]["DefID"].ToString());
}
}
}
//子节点绑定
public void GetChildNode(TreeListNode node, string parent)
{
IList<MMwDefVersCo> ChildNode = immwDefVersCoBO_0.GetEntitiesByDef(parent);
if (ChildNode.Count > 0)
{
ChildNode.ToList().ForEach(item =>
{
TreeListNode nodelist = node.TreeList.AppendNode(null, node);
nodelist.SetValue(物料编码, item.VLabel);
nodelist.SetValue(物料名称, item.IsCurrent.Value ? "CURRENT" : string.Empty);
}
);
}
}
private void querybutton_Click(object sender, EventArgs e)
{
pageIndex = 1;
BindPageGridList();
}
private void pageindextxt_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != '\b')//这是允许输入退格键 
            {
int len = pageindextxt.Text.Length;
if (len < 1 && e.KeyChar == '0')
{
e.Handled = true;
}
else if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字 
                {
e.Handled = true;
}
}
}
private void gotoindex_Click(object sender, EventArgs e)
{
if (pageCount > 0 && Convert.ToInt32(pageindextxt.Text) <= pageCount)
{
pageIndex = Convert.ToInt32(pageindextxt.Text);
BindPageGridList();
}
else { SaMessageBox.ShowWarning("务必保证输入页码有效"); pageindextxt.Text = ""; }
}
// 排序与选中
private void Resultlist_MouseDown(object sender, MouseEventArgs e)
{
TreeListHitInfo hitInfo = Resultlist.CalcHitInfo(e.Location);
//鼠标左键点击
if (e.Button == MouseButtons.Left && hitInfo.HitInfoType == HitInfoType.Column && pageCount > 0)
{
if (hitInfo.Column.FieldName == "物料编码")
{
ordername = "DefID";
if (orderint == 1)//判断排序规则
{ orderint = 2; }
else { orderint = 1; }
}
else if (hitInfo.Column.FieldName == "物料名称")
{
ordername = "DefName";
if (orderint == 3)
{ orderint = 4; }
else { orderint = 3; }
}
else if (hitInfo.Column.FieldName == "物料大类")
{
ordername = "TypeCD";
if (orderint == 5)
{ orderint = 6; }
else { orderint = 5; }
}
else if (hitInfo.Column.FieldName == "设备小类")
{
ordername = "ClassName";
if (orderint == 7)
{ orderint = 8; }
else { orderint = 7; }
}
else
{
return;
}
switch (orderint)
{
case 1:
case 3:
case 5:
case 7: order = "ASC"; break;
default: order = "DESC"; break;
}
BindPageGridList();
}
else { return; }
}
private void Resultlist_MouseDoubleClick(object sender, MouseEventArgs e)
{
TreeListHitInfo hitInfo = Resultlist.CalcHitInfo(e.Location);
if (e.Button == MouseButtons.Left && hitInfo.HitInfoType == HitInfoType.Cell)
{

if (Resultlist.FocusedNode.Selected && !Resultlist.FocusedNode.HasChildren)
{
DefPK = Resultlist.FocusedNode.ParentNode.GetValue("物料识别号").ToString();
DefID = Resultlist.FocusedNode.ParentNode.GetValue("物料编码").ToString();
version = Resultlist.FocusedNode.GetValue("物料编码").ToString();
base.DialogResult = DialogResult.OK;
base.Close();
}
else if (Resultlist.FocusedNode.Selected && Resultlist.FocusedNode.HasChildren)
{
DefPK = Resultlist.FocusedNode.GetValue("物料识别号").ToString();
DefID = Resultlist.FocusedNode.GetValue("物料编码").ToString();
base.DialogResult = DialogResult.OK;
base.Close();
}
else
{
return;
}
}
else { return; }
}
}

 

先写这么多有问题和建议或者哪里不明白的地方可以直接私聊我,如果觉得有帮助帮我点个赞我让我有动力继续发博客谢谢了。

 

posted on 2019-09-06 10:07  嘴强王者  阅读(1047)  评论(0编辑  收藏  举报