如何从数据库中显示NavBar Control Devexpress

时间:2017-02-17 01:46:13

标签: c# winforms oracle11g devexpress navbar

我有这样的表格菜单

enter image description here

GROUPMENU是导航栏控件中的一个组,JUDULMENU是一个项目。我试图使用此代码,但它不起作用

if (koneksidb.con.State == ConnectionState.Open)
    koneksidb.con.Close();
koneksidb.con.Open();
OracleCommand cmd = new OracleCommand();
cmd.CommandText = @"SELECT * FROM MENU ORDER BY GROUPMENU ASC";
cmd.Connection = koneksidb.con;
OracleDataReader dr = cmd.ExecuteReader();

// Create a NavBarControl.
NavBarControl navBar = new NavBarControl();
this.Controls.Add(navBar);
navBar.Dock = DockStyle.Left;
// Apply the "SkinExplorerBarView" style.
navBar.PaintStyleName = "SkinExplorerBarView";

while (dr.Read())
{
    NavBarGroup groupLocal = new NavBarGroup(dr["GROUPMENU"].ToString());
    NavBarItem itemInbox = new NavBarItem(dr["JUDULMENU"].ToString());

    navBar.BeginUpdate();
    navBar.Groups.Add(groupLocal);
    groupLocal.ItemLinks.Add(itemInbox);
    groupLocal.Expanded = true;
    navBar.EndUpdate();
}

这是我的代码工作的细节:

enter image description here

请有人建议我如何修复此代码? 感谢

1 个答案:

答案 0 :(得分:1)

在MENU表中只使用查询来分隔组和项目..它可以使用不同的方法或单个方法。这里是使用单一方法解决上述问题的示例代码:

try
{
    navBarControl1.BeginUpdate();

    //GET GROUP
    if (koneksidb.con.State == ConnectionState.Open)
        koneksidb.con.Close();
    koneksidb.con.Open();
    OracleCommand cmd = new OracleCommand();
    cmd.CommandText = @"SELECT DISTINCT GROUPMENU FROM MENU ";
    cmd.Connection = koneksidb.con;
    OracleDataReader dr = cmd.ExecuteReader();
    while (dr.Read())
    {
        List<string> header = new List<string>();
        header.Add(dr["GROUPMENU"].ToString());

        foreach (string hdr in header)
        {
            NavBarGroup group = new NavBarGroup(hdr);
            //group.Caption = fi.Name;
            group.LargeImageIndex = 0;
            group.Expanded = true;

            //GET ITEM
            OracleCommand cmd1 = new OracleCommand();
            cmd1.CommandText = @"SELECT * FROM MENU WHERE GROUPMENU='" + hdr + "' ORDER BY GROUPMENU ASC";
            cmd1.Connection = koneksidb.con;
            OracleDataReader dr1 = cmd1.ExecuteReader();
            while (dr1.Read())
            {
                List<string> JudulLists = new List<string>();
                JudulLists.Add(dr1["JUDULMENU"].ToString());

                foreach (var Judul in JudulLists)
                {
                    NavBarItem item = new NavBarItem(Judul.ToString());
                    navBarControl1.Items.Add(item);
                    group.ItemLinks.Add(item);
                }
            }

            if (group.ItemLinks.Count > 0)
            {
                navBarControl1.Groups.Add(group);
            }
        }
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
finally
{
    navBarControl1.EndUpdate();
}

结果:

enter image description here

现在为我解决..谢谢