-2

Here is my code, but its not working

protected void Button2_Click(object sender, EventArgs e)
    {
        int i = 1;
        if (GridView1.Rows.Count > 0)
        {
            TextBox1.Text = GridView1.Rows[i].Cells[1].Text;
            TextBox2.Text = GridView1.Rows[i].Cells[2].Text;
            TextBox3.Text = GridView1.Rows[i].Cells[31].Text;
            TextBox4.Text = GridView1.Rows[i].Cells[5].Text;
            i++; 
        }
    }

it didn't how next item.

Rahul Singh
  • 21,585
  • 6
  • 41
  • 56

1 Answers1

0

Use this code

protected void Button2_Click(object sender, EventArgs e)
{
            int i = ViewState["cnt"]!=null? (int) ViewState["cnt"]:1;
            i = i + 1;
            TextBox1.Text = GridView1.Rows[i].Cells[1].Text;
            TextBox2.Text = GridView1.Rows[i].Cells[2].Text;
            TextBox3.Text = GridView1.Rows[i].Cells[31].Text;
            TextBox4.Text = GridView1.Rows[i].Cells[5].Text;
            ViewState["cnt"] = i;
}
Kumar Manish
  • 3,746
  • 3
  • 37
  • 42
  • Sir, in this pgm how can I stop when it reaches the last ite(the last row or nth row). now in the pgm wen I click nxt it will goes to next item and so on, but after the last item it shows error. – vishnu suresh Dec 22 '14 at 08:35
  • Please check the number of records in the database with help of count property and if i and count is similar , stop the incremention in the I variable – Kumar Manish Dec 22 '14 at 11:23
  • DataSet ds = (DataSet)GridView1.DataSource; int count = ds.Tables[0].Rows.Count; int i = ViewState["cnt"] !=null ? (int)ViewState["cnt"] : 1; if (count < i) { i = i + 1; TextBox1.Text = GridView1.Rows[i].Cells[1].Text; TextBox2.Text = GridView1.Rows[i].Cells[2].Text; TextBox3.Text = GridView1.Rows[i].Cells[31].Text; TextBox4.Text = GridView1.Rows[i].Cells[5].Text; ViewState["cnt"] = i; } else return; } – vishnu suresh Dec 22 '14 at 15:03
  • change it if (count != i) instead of if (count < i) – Kumar Manish Dec 22 '14 at 17:24
  • sir the pgm shows error in int count = ds.Tables[0].Rows.Count; and also if ( count!=i) – vishnu suresh Dec 23 '14 at 07:51
  • int count = ds.Tables[0].Rows.Count; error shows here as NullrefrenceException was unhandled by user code object refrence not set to an instance of an object – vishnu suresh Dec 23 '14 at 08:29
  • instead of DataSet ds = (DataSet)GridView1.DataSource use this code :BindingSource bs = (BindingSource)GridView1.DataSource; DataSet ds = (DataSet) bs.DataSource; – Kumar Manish Dec 23 '14 at 08:36
  • look this link :http://stackoverflow.com/questions/785799/how-can-i-export-a-gridview-datasource-to-a-datatable-or-dataset – Kumar Manish Dec 23 '14 at 08:38