效果圖
網上查了很多例子自己結合和修改了一下。最下方的分頁跳轉是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; }
}
}
先寫這么多有問題和建議或者哪里不明白的地方可以直接私聊我,如果覺得有幫助幫我點個贊我讓我有動力繼續發博客謝謝了。