2016-12-06 146 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; ' –