Gridview Sorting Problem

I have a problem when I sort my gridview.

The problem is that after sorting it does not get the correct ID after i execute the below code.

    Protected Sub gvCustomres_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles gvCustomres.SelectedIndexChanged
        'Dim row As GridViewRow = gvCustomres.SelectedRow


        'Session("BrokerID") = row.Cells(0).Text

        'Response.Redirect("BrokerDetail.aspx")


    End Sub

Open in new window



eg, after sorting,    the ID id the top row maybe 270 but when I click on the button that executes the above code then it sees the value of  cell 0(id)   as  '1' instead of 270

How can I resolve this.
EdAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

suvmitraCommented:
You need to have gridview row events for that, here take a look on this sample code of how to work with grid view.

<asp:GridView ID="GridView13" align="center" runat="server" AutoGenerateColumns="False" CssClass="style1"
         onrowcancelingedit="GridView13_RowCancelingEdit" Width="100%"
        onrowediting="GridView13_RowEditing" onrowupdating="GridView13_RowUpdating"
        onrowdatabound="GridView13_RowDataBound" CellPadding="5" CellSpacing="5"
         EnableModelValidation="True" ForeColor="#333333" GridLines="None"
         >
        <AlternatingRowStyle BackColor="White" />
        <Columns>
           <asp:TemplateField HeaderText="ID">
           <ItemTemplate >
               <asp:Label ID="Label6" runat="server" Text='<%# Eval("ID") %>' >
            </asp:Label>
           </ItemTemplate>
           </asp:TemplateField>

        <asp:TemplateField HeaderText="Team">
           <ItemTemplate >
               <asp:Label ID="Label6x" runat="server" Text='<%# Eval("Email_Team") %>' >
            </asp:Label>
           </ItemTemplate>
           </asp:TemplateField>

           <asp:TemplateField HeaderText="Email_From">
           <ItemTemplate >
              <asp:Label ID="Label5" runat="server" Text='<%# Eval("Email_From") %>' >
            </asp:Label>
           </ItemTemplate>
           <EditItemTemplate >
               <asp:TextBox ID="TextBox1" BorderStyle="Outset" runat="server" Text='
                  <%# Eval("Email_From")%>'  ></asp:TextBox>
           </EditItemTemplate>
               </asp:TemplateField>
           <asp:TemplateField HeaderText="Email_Cc">
           <ItemTemplate>
               <asp:Label ID="Label1" runat="server" Text='<%# Eval
                        ("Email_Cc") %>'></asp:Label>
           </ItemTemplate>
           <EditItemTemplate >
               <asp:TextBox ID="TextBox2" BorderStyle="Outset" runat="server" Text='<%#
                        Eval("Email_Cc") %>' ></asp:TextBox>
           </EditItemTemplate>
           </asp:TemplateField>
           <asp:TemplateField HeaderText="Email_Bcc">
           <ItemTemplate >
               <asp:Label ID="Label3" runat="server" Text='<%# Eval
                              ("Email_Bcc") %>'></asp:Label>
           </ItemTemplate>
           <EditItemTemplate>
               <asp:TextBox ID="TextBox3" BorderStyle="Outset" runat="server" Text='<%#
                        Eval("Email_Bcc") %>' ></asp:TextBox>
           </EditItemTemplate>
           </asp:TemplateField>

           <asp:TemplateField HeaderText="Edit" ShowHeader="false">
               <ItemTemplate>
                   <asp:LinkButton ID="btnedit" runat="server"
                  CommandName="Edit" Text="Edit" ></asp:LinkButton>
               </ItemTemplate>
               <EditItemTemplate>
                   <asp:LinkButton ID="btnupdate" runat="server"
                  CommandName="Update" Text="Update" ></asp:LinkButton>
                   <asp:LinkButton ID="btncancel" runat="server"
                  CommandName="Cancel" Text="Cancel"></asp:LinkButton>
               </EditItemTemplate>
            </asp:TemplateField>
       </Columns>
        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
    </asp:GridView>

    protected void GridView13_RowDataBound(object sender, GridViewRowEventArgs e)
    {


    }

    protected void GridView13_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView13.EditIndex = -1;
        showgrid();
    }

    protected void GridView13_RowEditing(object sender, GridViewEditEventArgs e)
    {

        GridView13.EditIndex = e.NewEditIndex;
        showgrid();
    }

protected void GridView13_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        Label lb = (Label)GridView13.Rows[e.RowIndex].FindControl("Label6");
        TextBox tx1 = (TextBox)GridView13.Rows[e.RowIndex].FindControl("TextBox1");
        TextBox tx2 = (TextBox)GridView13.Rows[e.RowIndex].FindControl("TextBox2");
        TextBox tx3 = (TextBox)GridView13.Rows[e.RowIndex].FindControl("TextBox3");

        String strConString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        OleDbConnection MyConn;
        OleDbCommand MyComm;

        MyConn = new OleDbConnection(strConString);
        String sqlstr = "select * from tbluser";

        MyComm = new OleDbCommand(sqlstr, MyConn);
        MyComm.CommandType = CommandType.Text;


        try
        {
            if (MyConn.State == 0)
            {
                MyConn.Open();

            }
            else
            {
                MyConn.Close();
                MyConn.Open();
            }

            GridView13.EditIndex = -1;
            string strQuery = "Update tbl_Email Set Email_From='" + tx1.Text.ToString().Trim() + "',Email_Cc='" + tx2.Text.ToString().Trim()
                 + "',Email_Bcc='" + tx3.Text.ToString().Trim() + "' where ID='" + lb.Text.ToString().Trim() + "'";

            OleDbCommand MyComm2;
            OleDbDataAdapter daLevel = new OleDbDataAdapter();

            MyComm2 = new OleDbCommand(strQuery, MyConn);
            MyComm2.CommandType = CommandType.Text;
            MyComm2.ExecuteNonQuery();

        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
            return;
        }
        finally
        {
            MyComm.Dispose();
            MyConn.Close();

        }

        showgrid();
    }

 public void showgrid()
    {

        String strConString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        OleDbConnection MyConn;
        OleDbCommand MyComm;

        MyConn = new OleDbConnection(strConString);
        String sqlstr = "select * from tbluser";

        MyComm = new OleDbCommand(sqlstr, MyConn);
        MyComm.CommandType = CommandType.Text;


        try
        {
            if (MyConn.State == 0)
            {
                MyConn.Open();

            }
            else
            {
                MyConn.Close();
                MyConn.Open();
            }

            DataTable dt = new DataTable();
            string strQuery = "select * from tbl_Email Where Email_Team = '" + Request.Params["isPage"].ToString().Trim() + "'";
            OleDbConnection MyConn2;
            OleDbCommand MyComm2;
            OleDbDataAdapter daLevel = new OleDbDataAdapter();

            MyConn2 = new OleDbConnection(strConString);

            MyComm2 = new OleDbCommand(strQuery, MyConn2);
            MyComm2.CommandType = CommandType.Text;
            daLevel.SelectCommand = MyComm2;
            daLevel.Fill(dt);

            if (dt.Rows.Count >= 1)
            {
                GridView13.DataSource = dt;
                GridView13.DataBind();
                msgEmail.Text = "";
            }
            else
            {
                msgEmail.Text = "Sorry, no records!";
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
            return;
        }
        finally
        {
            MyComm.Dispose();
            MyConn.Close();

        }

    }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dejaanbuCommented:
there should be a problem in your sort code.

check online samples and compare your code...one here

http://www.c-sharpcorner.com/uploadfile/ad665f/paging-and-sorting-in-Asp-Net-gridview/
0
EdAuthor Commented:
Both comments helped to solve the issue.

However, I query string hyperlink definitively solved the issue fully.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.