2016-12-06 20 views
-2

我從here 在這裏得到了引用是我的aspx:將記錄向上和向下的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> 

代碼隱藏:

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(); 
       } 
      } 
     } 
    } 

我得到了錯誤輸入字符串的不正確格式在int lapbulID行。我錯過了什麼嗎?我能做些什麼來解決這個問題? 我試過TryParse,1 * Convert.To32int等,但沒有任何工作。幫助

注:我的數據庫也包含相同的引用,只有更多的字段。

+0

嘗試調試和檢查Urutan和UpdateUrutan方法lapbulID變量的值,添加值這裏 –

+0

試過了,Urutan的值是4(這是正確的)並且lapbulID是0 –

+0

在運行時從調試中複製查詢並在命令提示符下運行查詢,查看查詢中是否有任何錯誤,數據類型是不匹配 –

回答

0

使用Get選擇rowIndex位置的值:

int rowIndex = e.Row.RowIndex; 

那麼你的代碼將是:

 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;'gets一個錯誤System.EventArgs不包含'Row'的定義,也沒有擴展方法.... –

+0

嘗試我更新的答案 –

+0

仍然相同'缺少使用指令或程序集引用?'在'e.Row.RowIndex; ' –