2010-01-05 37 views
4

嗨,大家好顺序,使用箭头更改,一切事都显示在一个GridView的ASP.NET

我试图用箭头表示的图像,以允许用户更改顺序的项目出现在ASP.NET的网格视图中的列表中。

我有一个名为“position”的类,该类显示在GridView内部并按位置排序。在gridview的每一行都有一个向上和向下的箭头,我想改变gridview的行所表示的对象的“位置”的值。最简单的方法是什么?

ASP -

<br /><strong>Previous Employment: </strong> <br /> 
       Pick one of your previous employers listed below to update/delete or <asp:HyperLink ID="PreviousEmploymentLink" runat="server" />. 
       <asp:GridView runat="server" ID="EmploymentDataGrid" AutoGenerateColumns="false" OnRowDeleting="EmploymentDataGrid_onDeleting" 
       DataKeyNames="EmployerId" SkinID="FullWidthGrid" > 
       <EmptyDataTemplate> 
       <p>No Previous Employment added yet</p> 
       </EmptyDataTemplate> 
        <Columns> 
          <asp:BoundField HeaderText="Dates" DataField="Dates" /> 
          <asp:BoundField HeaderText="Employer's Name" DataField="EmployerName" /> 
          <asp:BoundField HeaderText="Job Description" DataField="JobDescription" /> 
          <asp:BoundField HeaderText="Job Title" DataField="JobTitle" /> 
          <asp:HyperLinkField HeaderStyle-Width="30px" DataNavigateUrlFields="EmployerId" 
          HeaderText="Edit" Text="<img src='../../../Images/edit.gif' alt='Edit Employment' border='0'/>" 
          DataNavigateUrlFormatString="UpdatePreviousEmployment.aspx?PreviousEmploymentId={0}" /> 
          <asp:TemplateField HeaderText="Delete" ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="45px"> 
           <ItemTemplate> 
            <asp:ImageButton AlternateText="Delete User" ID="DeleteButton" runat="server" CommandName="Delete" ImageUrl="~/Images/delete.gif" /> 
           </ItemTemplate> 
          </asp:TemplateField> 
          <asp:TemplateField HeaderText="Position" ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="45px"> 
           <ItemTemplate> 
            <asp:ImageButton AlternateText="Move Up" ID="UpPositionButton" runat="server" CommandName="MoveEmploymentUp" OnClick="MoveEmploymentUp" ImageUrl="~/Images/arrow_up_green.gif" /> 
            <asp:ImageButton AlternateText="Move Down" ID="DownPositionButton" runat="server" CommandName="MoveEmploymentDown" ImageUrl="~/Images/arrow_down_green.gif" /> 
           </ItemTemplate> 
          </asp:TemplateField> 
        </Columns> 
       </asp:GridView> 

谢谢!

回答

1
protected void MoveEmploymentUp(object sender, ImageClickEventArgs e) 
{ 
    ICVDao cvdao = DaoFactory.GetCVDao();   

    CV currentCv = cvdao.GetById(Int32.Parse(Request.Params["CVID"]), false); 

    IEmployerDao eDao = DaoFactory.GetEmployerDao(); 


    ImageButton i = (ImageButton)sender; 

    /* 
    * if chap above 
    * Swap numbers with chap above 
    * if not 
    * dont swap 
    */ 

    GridViewRow g = (GridViewRow)i.Parent.Parent; 

    Employer emp = (Employer)g.DataItem; 

    EmploymentDataGrid.SelectedIndex = g.RowIndex; 
    Employer movingEmployment = eDao.GetById(int.Parse(EmploymentDataGrid.SelectedDataKey.Value.ToString()), false); 

    Employer otherEmployment = new Employer(); 

    if (movingEmployment.Position != 1 && currentCv.Employment.Count > 1) 
    { 
     foreach (Employer em in currentCv.Employment) 
     { 
      if (em.Position == movingEmployment.Position - 1) 
       otherEmployment = em; 
     } 

     movingEmployment.Position -= 1; 
     otherEmployment.Position += 1; 

     eDao.SaveOrUpdate(movingEmployment); 
     eDao.CommitChanges(); 
     eDao.SaveOrUpdate(otherEmployment); 
     eDao.CommitChanges(); 

     EmploymentGridBind(); 

     //InstructionsLabel.Text = "Mover Dates:" +movingEmployment.Dates+ " ID:" + movingEmployment.EmployerId + " Position:" + movingEmployment.Position + 
     // "<br />Other Dates:" + otherEmployment.Dates + " ID:" + otherEmployment.EmployerId + " Position:" + otherEmployment.Position; 
    } 

} 

必须得到通过gridview的对象datakey值相对于gridviewrow,使用,其余的全是容易

1

我有几个小的话:

吹毛求疵: movingEmployment对子级正在移动员工

//the following assignement is not used. 
Employer emp = (Employer)g.DataItem;  

//This object should not be constructed by default. 
Employer otherEmployment = new Employer(); 

eDao.SaveOrUpdate(movingEmployment); 
//eDao.CommitChanges(); 
//I did not commit because if something went wrong right now, 
//then the data for one would be updated but the other not. 
eDao.SaveOrUpdate(otherEmployment); 
eDao.CommitChanges(); 

修改后的代码(稍微调整了性能)可能是。

protected void MoveEmploymentUp(object sender, ImageClickEventArgs e) 
{ 
    ICVDao cvdao = DaoFactory.GetCVDao(); 
    CV currentCv = cvdao.GetById(Int32.Parse(Request.Params["CVID"]), false); 

    //Can only work with at least two items in the list. 
    if(currentCv.Employment.Count > 1) 
    { 
    #region Get to the selected Employee reference 
    IEmployerDao eDao = DaoFactory.GetEmployerDao(); 
    ImageButton i = (ImageButton)sender; 
    GridViewRow g = (GridViewRow)i.Parent.Parent;   
    EmploymentDataGrid.SelectedIndex = g.RowIndex;   
    Employer selectedEmployee = eDao.GetById(int.ParseEmploymentDataGrid.SelectedDataKey.Value.ToString()), false);  
    #endregion 

    int selectedPosition = selectedEmployee.Position; 
    if (selectedPosition != 1)   
    {   
     #region find employee to swap with 
     Employer swapWithEmployee = null; 
     //I assume currentCv.Employment is only an IEnumerable. 
     //If it was an Collection we would not have to loop. 
     foreach (Employer findSwapEmployee in currentCv.Employment)   
     {   
      if (findSwapEmployee.Position == selectedPosition- 1) 
      {   
       swapWithEmployee= findSwapEmployee;   
       break; 
      }   
     } 
     #endregion 

     #region perform and commit swap 
     if(swapWithEmployee != null) 
     { 
     selectedEmployee.Position -= 1;   
     swapWithEmployee.Position = selectedPosition;   

     eDao.SaveOrUpdate(selectedEmployee);   
     eDao.SaveOrUpdate(swapWithEmployee);   
     eDao.CommitChanges();   
     EmploymentGridBind();   
     }   
     #endregion 
    }  
}