2016-12-06 4 views
-2

ich den Verweis aus here bekam und hier ist meine aspx:Verschieben Rekord Up- und Down-Gridview ASP

<asp:GridView ID="gvExecSummary" CssClass="grid th" runat="server" 
       EmptyDataRowStyle-BorderStyle="Solid" EmptyDataRowStyle-HorizontalAlign="Center" EmptyDataRowStyle-Height="100px" 
       EmptyDataRowStyle-BorderWidth="1px" Width="100%" HorizontalAlign="Center" AutoGenerateColumns="False" 
       HeaderStyle-BorderColor="Black" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" 
       CellPadding="3" GridLines="Vertical" ForeColor="Black" Visible="true " DataKeyNames="valUrutan" OnRowCommand="gvExecSummary_RowCommand"> 
       <AlternatingRowStyle BackColor="#efefef" /> 
       <Columns> 
        <asp:TemplateField HeaderText="No" ItemStyle-HorizontalAlign="Center" HeaderStyle-BorderWidth="1" ItemStyle-VerticalAlign="Top"> 
         <HeaderTemplate> 
          <asp:Label runat="server" Text="No" CssClass="search_textbox4"></asp:Label> 
         </HeaderTemplate> 
         <ItemTemplate> 
          <%# Container.DataItemIndex + 1 %> 
         </ItemTemplate> 
         <ItemStyle HorizontalAlign="Center"></ItemStyle> 
        </asp:TemplateField> 



        <asp:TemplateField HeaderText="Pekerjaan/Project" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Top"> 
         <HeaderTemplate> 
          Pekerjaan/Project<br /> 

         </HeaderTemplate> 
         <ItemTemplate> 
          &nbsp&nbsp 
          <asp:LinkButton ID="lb" runat="server" Text=' <%# Eval("lapPekerjaan") %>' CommandArgument='<%# Eval("lapPekerjaan") %>' CommandName="ShowResume"></asp:LinkButton> 
         </ItemTemplate> 

         <ItemStyle VerticalAlign="Top"></ItemStyle> 
        </asp:TemplateField> 

        <asp:TemplateField HeaderText="User/Client" ControlStyle-Width="350px" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Top"> 
         <HeaderTemplate> 
          User/Client<br /> 

         </HeaderTemplate> 
         <ItemTemplate> 
          &nbsp&nbsp<asp:Label ID="lblUserClient" runat="server" Text=' <%# Eval("lapUser") %>'></asp:Label> 
         </ItemTemplate> 
         <ControlStyle Width="350px"></ControlStyle> 
         <ItemStyle HorizontalAlign="Left" VerticalAlign="Top"></ItemStyle> 
        </asp:TemplateField> 


        <asp:TemplateField HeaderText="Permasalahan/Legal Issue" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Top"> 
         <HeaderTemplate> 
          Permasalahan/Legal Issue<br /> 

         </HeaderTemplate> 
         <ItemTemplate> 
          <asp:Label ID="lbllegalIssue" runat="server" Text=' <%# Eval("lapPermsl") %>'></asp:Label> 
         </ItemTemplate> 
         <ItemStyle HorizontalAlign="Center" VerticalAlign="Top"></ItemStyle> 
        </asp:TemplateField> 


        <asp:TemplateField HeaderText="Status Penyelesaian" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Top"> 
         <HeaderTemplate> 
          Status Penyelesaian<br /> 

         </HeaderTemplate> 
         <ItemTemplate> 
          <asp:Label ID="lblstatusPenyelesaian" runat="server" Text=' <%# Eval("lapStat") %>'></asp:Label> 
         </ItemTemplate> 
         <ItemStyle HorizontalAlign="Center" VerticalAlign="Top"></ItemStyle> 
        </asp:TemplateField> 

        <asp:TemplateField HeaderText="Person In Charge" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Top"> 
         <HeaderTemplate> 
          Person In Charge<br /> 

         </HeaderTemplate> 
         <ItemTemplate> 
          <asp:Label ID="lblPersonInCharge" runat="server" Text=' <%# Eval("lapPIC") %>'></asp:Label> 
         </ItemTemplate> 
         <ItemStyle HorizontalAlign="Center" VerticalAlign="Top"></ItemStyle> 
        </asp:TemplateField> 


        <asp:TemplateField ShowHeader="false" Visible="true"> 
         <ItemTemplate> 
          <asp:LinkButton ID="lbUpdate" ClientIDMode="AutoID" runat="server" CommandName="Ubah" CommandArgument='<%# Eval("idLapBulExe") %>'><i class="fa fa-pencil"></i></asp:LinkButton> 
          <asp:LinkButton ID="lbDelete" runat="server" CommandName="Hapus" CommandArgument='<%# Eval("idLapBulExe") %>' ImageUrl="~/App_Theme/Images/delete.png" OnClientClick="return confirm('Are you sure you want to delete this record ?');"><i class="fa fa-trash"></i></asp:LinkButton> 
         </ItemTemplate> 
         <ItemStyle HorizontalAlign="Center"></ItemStyle> 
        </asp:TemplateField>      
        <asp:TemplateField ShowHeader ="false"> 
         <ItemTemplate> 
          <asp:LinkButton ID="lbUp" Text="&#x25B2;" ClientIDMode="AutoID" CssClass="button" OnClick="ChangeUrutan" runat="server" CommandArgument="up"></asp:LinkButton> 
          <asp:LinkButton ID="lbDown" Text="&#x25BC;" runat="server" CssClass="button" OnClick="ChangeUrutan" CommandArgument="down"></asp:LinkButton> 
         </ItemTemplate> 
        </asp:TemplateField> 

       </Columns> 

       <EmptyDataRowStyle HorizontalAlign="Center" BorderWidth="1px" BorderStyle="Solid" Height="100px"></EmptyDataRowStyle> 

       <FooterStyle BackColor="#CCCCCC" /> 
       <HeaderStyle BorderColor="#3a87ad" BackColor="#3a87ad" Font-Bold="True" ForeColor="White"></HeaderStyle> 
       <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" /> 
       <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" /> 
       <SortedAscendingCellStyle BackColor="#F1F1F1" /> 
       <SortedAscendingHeaderStyle BackColor="#808080" /> 
       <SortedDescendingCellStyle BackColor="#CAC9C9" /> 
       <SortedDescendingHeaderStyle BackColor="#383838" /> 
      </asp:GridView> 

Code-Behind:

protected void ChangeUrutan(object sender, EventArgs e) 
    { 
     string commandArgument = (sender as LinkButton).CommandArgument; 
     try 
     { 

      int rowIndex = ((sender as LinkButton).NamingContainer as GridViewRow).RowIndex; 
      int Urutan = Convert.ToInt32(gvExecSummary.DataKeys[rowIndex].Value); 
      int lapbulID = Convert.ToInt32(gvExecSummary.Rows[rowIndex].Cells[0].Text.ToString().Trim()); //this is where i got the error 
      Urutan = commandArgument == "up" ? Urutan - 1 : Urutan + 1; 
      this.UpdateUrutan(lapbulID, Urutan); 

      rowIndex = commandArgument == "up" ? rowIndex - 1 : rowIndex + 1; 
      lapbulID = Convert.ToInt32(gvExecSummary.Rows[rowIndex].Cells[0].Text); 
      Urutan = Convert.ToInt32(gvExecSummary.DataKeys[rowIndex].Value); 
      Urutan = commandArgument == "up" ? Urutan + 1 : Urutan - 1; 
      this.UpdateUrutan(lapbulID, Urutan); 

      Response.Redirect(Request.Url.AbsoluteUri); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("{0} Exception caught.", ex); 
     } 


    } 
    private void UpdateUrutan(int lapbulID, int Urutan) 
    { 

     using (SqlConnection con = new SqlConnection(common.Constr)) 
     { 
      using (SqlCommand cmd = new SqlCommand("UPDATE tblT_LapBulExecutive SET valUrutan = @Urutan WHERE IdFungsiExecutive = @Id")) 
      { 
       using (SqlDataAdapter sda = new SqlDataAdapter()) 
       { 
        cmd.CommandType = CommandType.Text; 
        cmd.Parameters.AddWithValue("@Id", lapbulID); 
        cmd.Parameters.AddWithValue("@Urutan", Urutan); 
        cmd.Connection = con; 
        con.Open(); 
        cmd.ExecuteNonQuery(); 
        con.Close(); 
       } 
      } 
     } 
    } 

und ich bekam Eingabezeichenfolge Fehler war nicht in einem korrekten Format in der int lapbulID Zeile. Fehle ich etwas? Was kann ich tun, um dies zu beheben? Ich habe versucht, TryParse, 1 * Convert.To32int, etc, aber nichts funktioniert. Hilfe

Hinweis: meine Datenbank enthält auch das gleiche wie diese Referenz, nur mit mehr Feld. mit

+0

den Debugger versuchen, den Wert in Urutan und lapbulID Variablen in UpdateUrutan Methode überprüfen, Fügen Sie die Werte hier –

+0

versuchte es, und Urutan Wert ist 4 (was korrekt ist) und lapbulID ist 0 –

+0

Kopieren Sie die Abfrage von Debugging zur Laufzeit und führen Sie die Abfrage an der Eingabeaufforderung, sehen Sie, gibt es einen Fehler in der Abfrage gibt es eine Möglichkeit, dass die Datentypen sind nicht übereinstimmend –

Antwort

0

den Wert von Selected rowIndex Get:

int rowIndex = e.Row.RowIndex; 

Dann wird Ihr Code sein:

 try 
     { 
if (gvExecSummary.SelectedRow.RowType == DataControlRowType.DataRow) 
{ 

      // int rowIndex = ((sender as LinkButton).NamingContainer as GridViewRow).RowIndex; 
      int rowIndex = e.Row.RowIndex; 
      int Urutan = Convert.ToInt32(gvExecSummary.DataKeys[rowIndex].Value); 
      int lapbulID = Convert.ToInt32(gvExecSummary.Rows[rowIndex].Cells[0].Text.ToString().Trim()); //this is where i got the error 
      Urutan = commandArgument == "up" ? Urutan - 1 : Urutan + 1; 
      this.UpdateUrutan(lapbulID, Urutan); 

      rowIndex = commandArgument == "up" ? rowIndex - 1 : rowIndex + 1; 
      lapbulID = Convert.ToInt32(gvExecSummary.Rows[rowIndex].Cells[0].Text); 
      Urutan = Convert.ToInt32(gvExecSummary.DataKeys[rowIndex].Value); 
      Urutan = commandArgument == "up" ? Urutan + 1 : Urutan - 1; 
      this.UpdateUrutan(lapbulID, Urutan); 

      Response.Redirect(Request.Url.AbsoluteUri); 
} 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("{0} Exception caught.", ex); 
     } 
+0

'int rowIndex = e.Row.RowIndex;' bekommt ein Fehler System.EventArgs enthält keine Definition für 'Row' und keine Erweiterungsmethode .... –

+0

try me updated Antwort –

+0

immer noch das gleiche 'fehlt eine using-Anweisung oder eine Assembly-Referenz?' unter 'e.Row.RowIndex; ' –