我从here获得了参考资料 这是我的aspx:
<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>
  
<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>
  <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="▲" ClientIDMode="AutoID" CssClass="button" OnClick="ChangeUrutan" runat="server" CommandArgument="up"></asp:LinkButton>
<asp:LinkButton ID="lbDown" Text="▼" 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>
codebehind:
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 :(得分:0)
使用以下方法获取Selected 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);
}