hi
I used gridview and bind it from database I define preferences for all row in database and order by preferences see below image:
here that I show in image with red arrow when I click on gray arrow in gridview it will change row's Item below is code(up and down):
protected void ChangePreference(object sender, ImageClickEventArgs e)
{
string commandArgument = (sender as ImageButton).CommandArgument;
int rowIndex = ((sender as ImageButton).NamingContainer as GridViewRow).RowIndex;
int locationId = Convert.ToInt32(GridView2.Rows[rowIndex].Cells[0].Text);
int preference = Convert.ToInt32(GridView2.DataKeys[rowIndex].Value);
preference = commandArgument == "up" ? preference - 1 : preference + 1;
this.UpdatePreference(locationId, preference);
rowIndex = commandArgument == "up" ? rowIndex - 1 : rowIndex + 1;
locationId = Convert.ToInt32(GridView2.Rows[rowIndex].Cells[0].Text);
preference = Convert.ToInt32(GridView2.DataKeys[rowIndex].Value);
preference = commandArgument == "up" ? preference + 1 : preference - 1;
this.UpdatePreference(locationId, preference);
this.GetDocumentInfo(1);
}
and grid view:
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" Font-Size="14px"
DataKeyNames="Preference" OnRowDataBound="OnRowDataBound" PageSize="5" GridLines="Both"
Width="991px" BorderColor="#808080" BorderStyle="Solid" BorderWidth="1px" RowStyle-BorderColor="#808080"
RowStyle-BorderStyle="Solid" RowStyle-BorderWidth="1px" AlternatingRowStyle-BackColor="#C0C0C0"
PagerStyle-HorizontalAlign="Center" PagerStyle-VerticalAlign="Middle" ShowHeader="False"
Font-Names="behtop_Yekan" BackColor="#Fcfcfc" CssClass="gridGalary">
<Columns>
<asp:TemplateField ItemStyle-Width="24px" ItemStyle-VerticalAlign="Middle">
<ItemTemplate>
<asp:Label ID="lblCustomerID1" runat="server" Text='<%# Eval("ID")%>' CommandArgument="lblCustomerID1"
Visible="false"></asp:Label>
<div id="divimgup">
<asp:ImageButton ID="lnkUp" runat="server" CssClass="imgupP" ImageUrl="~/Image/admin/imgup.png"
CommandArgument="up" OnClick="ChangePreference" CausesValidation="false" />
<asp:ImageButton ID="lnkDown" runat="server" CssClass="imgup1P" ImageUrl="~/Image/admin/imgup.png"
CommandArgument="down" OnClick="ChangePreference" CausesValidation="false" />
</div>
but here when I click on arrow in gridview to change row's Item it make error:
Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.FormatException: Input string was not in a correct format.
Source Error:
Line 206:
Line 207: int rowIndex = ((sender as ImageButton).NamingContainer as GridViewRow).RowIndex;
Line 208: int locationId = Convert.ToInt32(GridView2.Rows[rowIndex].Cells[0].Text);
Line 209: int preference = Convert.ToInt32(GridView2.DataKeys[rowIndex].Value);
Line 210: preference = commandArgument == "up" ? preference - 1 : preference + 1;
|
why this happen?
Best regards
neda