GridView Sorting problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aj123
    New Member
    • Mar 2007
    • 8

    GridView Sorting problem

    I have Table News
    NewsID, int
    NewsHeadLint, varcahr(200)
    OrderNews, int

    I gridView Display as following query
    Select ID,NewsHeadLine from News order By OrderNews DESC
    ----
    <asp:GridView ID="GridView1" runat="server" AutoGenerateCol umns="False" DataKeyNames="N ewsID" EmptyDataText=" There are no data records to display."
    DataSourceID="S qlDataSource1" Style="position : relative; left: 4px; top: 44px;" OnRowCommand="G rid1_Command" AllowPaging="Tr ue" PageSize="8" Width="571px" >
    I have Add in gridView 2 buttonField
    <asp:ButtonFiel d ButtonType="Ima ge" CommandName="Cm dUp"..
    <asp:ButtonFiel d ButtonType="Ima ge" CommandName="Cm dDown"
    ..
    CmdUP(command up) get the value of current order row and row the above in gridView and
    swtich between them
    In CS code
    protected void Grid1_Command(o bject sender, System.Web.UI.W ebControls.Grid ViewCommandEven tArgs e)
    {

    // Object o = sender;


    String z = (String)e.Comma ndArgument;

    int indexGrid = Convert.ToInt16 (z);
    String CommandNamew = e.CommandName.T oString();
    switch (CommandNamew)
    {
    //-------------------------------------------


    case "CmdUp":
    {

    //I have write query and correctly excute in Database




    String NewsIDcurrent = GridView1.DataK eys[indexGrid].Value.ToString ().Trim(); //IDnews
    //Response.Write( GridView1.DataK eys[Convert.ToInt16 (z)].Values[0].ToString());

    SqlCommand Cmd = new SqlCommand();
    Cmd.Connection = conn;




    if ((indexGrid + 1) != GridView1.DataK eys.Count)
    {

    //NewsIDvalue this the current Id
    String NewsIDunder = GridView1.DataK eys[indexGrid + 1].Value.ToString ().Trim();
    try
    {
    conn.Open();
    //---Current
    Cmd.CommandText = "Select OrderNews from News where NewsID=" + NewsIDcurrent;
    int orderVal = (int)Cmd.Execut eScalar();


    Cmd.Parameters. Clear();



    Cmd.CommandText = "Select OrderNews from News where NewsID=" + NewsIDunder;

    int orderUnder = (int)Cmd.Execut eScalar();
    Cmd.Parameters. Clear();

    Cmd.CommandText = "update News Set OrderNews=" + Convert.ToStrin g(orderUnder) + " where NewsID=" + NewsIDcurrent;
    Cmd.ExecuteNonQ uery();

    //--Under
    Cmd.Parameters. Clear();


    Cmd.CommandText = "update News Set OrderNews=" + Convert.ToStrin g(orderVal) + " where NewsID=" + NewsIDunder;
    Cmd.ExecuteNonQ uery();



    Cmd.Parameters. Clear();


    // GridView1.DataB ind();
    }

    catch (Exception ex)
    {

    }

    finally
    {
    conn.Close();
    // Response.Redire ct("NewsCollect ion.aspx"); with it or without it
    }






    }

    break;
    }//end cmddown


    case "CmdDown":
    {


    //I have write query and correctly excute in Database same idea as above

    break;


    }
    default: { break; }

    ............




    I have problem with this command(excute twice,or not display modefection unless u refresh the browser...)
    what excatly best way to write and what event I must to write in the webpage(_Load _preRender..)
Working...