1.得到数据集
public DataSet GetAllLvlMenu()
{
StringBuilder strSql = new StringBuilder();
strSql.Append(" declare @lvl int ");
strSql.Append(" declare @i int ");
// strSql.Append(" select @lvl=max(lvl)+5 from Member_menu ");
strSql.Append(" set @lvl=19 ");
strSql.Append(" set @i=0 ");
strSql.Append(" while @i<=@lvl ");
strSql.Append(" begin ");
strSql.Append(" select *,SUBSTRING(Code,1,len(Code)-3) as PCode from Member_Menu where lvl=@i order by ShowOrder ");
strSql.Append(" set @i=@i+1 ");
strSql.Append(" end ");
return DbHelperSQL.Query(strSql.ToString());
}
2. 得到集合并建立主子表关系
DataSet ds = Member.GetAllLvlMenu();
//表之间建立主子表关系
for (int i = 0; i <ds.Tables.Count-1; i++)
{
ds.Relations.Add(ds.Tables[i].Columns["Code"], ds.Tables[i + 1].Columns["PCode"]);
}
3.绑定
UltraWebGrid1.DataSource = ds;
UltraWebGrid1.DataBind();
4.前台添加功能
function UltraWebGrid_Add(objID)
{
var grid=igtbl_getGridById(objID);
var row=grid.getActiveRow();
if(row!=null)
{
if(row.Rows!=null) row.Rows.addNew();
else
{
//由于我数据库设计code的长度是60,每层是3位,所以这里只能到20层
if(row.Band.Index<19) igtbl_addNew(objID,row.Band.Index+1);
else igtbl_addNew(objID,row.Band.Index);
}
}
else igtbl_addNew(objID,0);
return false;
}
5.后台保存
protected void SaveButton_Click(object sender, EventArgs e)
{
try
{
SaveMenu(this.UltraWebGrid1.Rows);
MessageBox.Show(this, "sucess!");
}
catch(Exception ex)
{
MessageBox.Show(this, "fail!");
}
}
private void SaveMenu(RowsCollection Rows)
{
for (int i = 0; i < Rows.Count; i++)
{
if (Rows[i].DataChanged == DataChanged.Modified || Rows[i].DataChanged == DataChanged.Added)
{
Member_MenuInfo menu = new Member_MenuInfo();
string ParentCode = "";
menu.Lvl = Rows.Band.Index;
if (Rows[i].Cells.FromKey("Chinese").Value != null)
menu.Chinese = Rows[i].Cells.FromKey("Chinese").Value.ToString();
if (Rows[i].Cells.FromKey("Russian").Value != null)
menu.Russian = Rows[i].Cells.FromKey("Russian").Value.ToString();
if (Rows[i].Cells.FromKey("English").Value != null)
menu.English = Rows[i].Cells.FromKey("English").Value.ToString();
if (Rows[i].Cells.FromKey("URL").Value != null)
menu.URL = Rows[i].Cells.FromKey("URL").Value.ToString();
if (Rows[i].Cells.FromKey("ImagePath").Value != null)
menu.ImagePath = Rows[i].Cells.FromKey("ImagePath").Value.ToString();
menu.IsShow = (bool)Rows[i].Cells.FromKey("IsShow").Value;
if (Rows[i].Cells.FromKey("ShowOrder").Value != null)
menu.ShowOrder = (int)Rows[i].Cells.FromKey("ShowOrder").Value;
if (Rows[i].DataChanged == DataChanged.Added)
{
menu.MenuId = System.Guid.NewGuid();
if (Rows[i].ParentRow != null)
ParentCode = Rows[i].ParentRow.Cells.FromKey("Code").Value.ToString();
Member.Member_MenuAdd(menu, ParentCode);
menu = Member.GetMember_MenuInfo(menu.MenuId);
Rows[i].Cells.FromKey("MenuId").Value = menu.MenuId;
Rows[i].Cells.FromKey("Code").Value = menu.Code;
Rows[i].Cells.FromKey("PCode").Value = ParentCode;
}
else if (Rows[i].DataChanged == DataChanged.Modified)
{
menu.MenuId = (Guid)Rows[i].Cells.FromKey("MenuId").Value;
menu.Code = Rows[i].Cells.FromKey("Code").Value.ToString();
Member.Member_MenuUpdate(menu);
}
}
if (Rows[i].Rows != null) SaveMenu(Rows[i].Rows);
}
}