简体   繁体   中英

how to go back to previous row in gridview on click previous_button click event

How to go back to previous row in gridview on click previous_button click event?

Sometimes in gridview , there might be a requirement that I need to select the previous or next row of the grid on button click. for example i have selected the 2nd row and now I want to go 3rd row. So I can do that by just clicking the “Next” button and I can keep on going to all the next rows by clicking it again. Similarly, let's say that i have selected the 10th row and now i want to go 9th row. So I can do that by just clicking the “Previous” button and so on. Here is the C # code for that.

Please help with me the c# code for previous_Click.

protected void Nextbutton_Click(object sender, EventArgs e)
{
    count++;
    if (count==10)
    {
            Div1.Attributes.CssStyle["display"] = "show";          
    }
    else
    {
        GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
        clickedRow.Visible = false;
    }
}

protected void previous_Click(object sender, EventArgs e)
{
    GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
}

You can select of previous row as following:

protected void previous_Click(object sender, EventArgs e)
{
    GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
    int currentRowIndex = clickedRow.RowIndex;
    if (currentRowIndex != 0){
        int previousRowIndex = GridView1.Rows[currentRowIndex - 1].RowIndex;
        GridView1.SelectedIndex = previousRowIndex;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM