NewbieProgrammer
NewbieProgrammer

Reputation: 864

Pagination not working properly with DataGridView?

I am trying to implement pagination in DataGridView in C# windows application. Basically I have a simple DataGridViewwhich gets populated by a stored procedure in database and get totalrecords from another stored procedure as well.

I have also added three buttons to the grid, which refer to three other forms, and they send TicketID (Column 0 in grid) with them as a parameter.

Now when the grid is loaded, it is working perfectly (all 3 buttons send TicketID successfully in parameters), but whenever I click on pagination control (first,previous,next,last), the 3 buttons I added do not function properly. What I mean is, instead of sending TicketID (column 0) as a parameter they send "ButtonName (.Text of DataGridView button)" of the column.

I cant seem to figure out what the problem is, I would really appreciate if someone could help me out.

Code of the page :

         public partial class Form1 : Form
            {
    private int totalRecords = 0;
    private int mintTotalRecords = 0;
    private int mintPageSize = 0;
    private int mintPageCount = 0;
    private int mintCurrentPage = 1;

    public Form1()
    {
        InitializeComponent();
    }

    private void fillGrid()
    {
        try
        {
            this.mintPageSize = 10; 
            this.mintTotalRecords = getCount();
            this.mintPageCount = this.mintTotalRecords / this.mintPageSize;
            if (this.mintTotalRecords % this.mintPageSize > 0)
                this.mintPageCount++;
            this.mintCurrentPage = 0;
            loadPage();
        }
        catch (Exception ex)
        {

        }
    }
    private int getCount()
    {
        SqlConnection con = new SqlConnection();
        try
        {

            con.ConnectionString = "//connectionstring";
            SqlCommand com = new SqlCommand();
            com.Connection = con;
            com.CommandText = "getTotalNo";
            com.CommandType = CommandType.StoredProcedure;
            com.Parameters.Clear();
            con.Open();
            SqlDataReader dr = com.ExecuteReader();
            while (dr.Read())
            {
                totalRecords = Convert.ToInt32(dr["total"].ToString());
            }
        }
        catch (Exception ex)
        {
            totalRecords = 0;
        }

        return totalRecords;
    }


    private void loadPage()
    {
        SqlConnection con = new SqlConnection();
        try
        {

            int intSkip = 0;
            intSkip = (this.mintCurrentPage * this.mintPageSize);
            con.ConnectionString = "//connectionstring";
            SqlCommand com = new SqlCommand();
            com.Connection = con;
            com.CommandText = "showRecord";
            com.CommandType = CommandType.StoredProcedure;
            com.Parameters.Clear();
            com.Parameters.AddWithValue("@pagesize", mintPageSize.ToString());
            com.Parameters.AddWithValue("@skip", intSkip.ToString());
            con.Open();
            SqlDataReader dr = com.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(dr);
            dgRecords.DataSource = dt;
            label1.Text = (this.mintCurrentPage + 1).ToString() + " / " + this.mintPageCount.ToString();


        }
        catch (Exception ex)
        {

        }
    }

    private void loadbtns()
    {
        DataGridViewButtonColumn cell = new DataGridViewButtonColumn();
        cell.HeaderText = "View Details";
        cell.Name = "View";
        cell.Visible = true;
        cell.Width = 100;
        cell.Text = "View Details";
        cell.UseColumnTextForButtonValue = true;

        DataGridViewButtonColumn cell2 = new DataGridViewButtonColumn();
        cell2.HeaderText = "Add Details";
        cell2.Name = "Add";
        cell2.Visible = true;
        cell2.Width = 120;
        cell2.Text = "Add Technical Detail";
        cell2.UseColumnTextForButtonValue = true;

        DataGridViewButtonColumn cell3 = new DataGridViewButtonColumn();
        cell3.HeaderText = "Close Ticket";
        cell3.Name = "Close";
        cell3.Visible = true;
        cell3.Width = 100;
        cell3.Text = "Close Ticket";
        cell3.UseColumnTextForButtonValue = true;

        dgRecords.Columns.Add(cell);
        dgRecords.Columns.Add(cell2);
        dgRecords.Columns.Add(cell3);
    }



    private void lnkFirst_Click(object sender, EventArgs e)
    {
        try
        {
            this.mintCurrentPage = this.mintPageCount - 1;

            loadPage();
        }
        catch
        {
        }

    }

    private void lnkNext_Click(object sender, EventArgs e)
    {
        try
        {
            this.mintCurrentPage++;

            if (this.mintCurrentPage > (this.mintPageCount - 1))
                this.mintCurrentPage = this.mintPageCount - 1;

            loadPage();
        }
        catch
        {
        }

    }

    private void lnkPrevious_Click(object sender, EventArgs e)
    {
        try
        {
            if (this.mintCurrentPage == this.mintPageCount)
                this.mintCurrentPage = this.mintPageCount - 1;
            this.mintCurrentPage--;
            if (this.mintCurrentPage < 1)
                this.mintCurrentPage = 0;

            loadPage();
        }
        catch
        {
        }
    }

    private void lnkLast_Click(object sender, EventArgs e)
    {
        try
        {
            this.mintCurrentPage = 0;

            loadPage();
        }
        catch
        {
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        fillGrid();
        loadbtns();
    }

    void childForm_FormClosed(object sender, FormClosedEventArgs e)
    {
        this.Visible = true;
    }

    private void dgRecords_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == dgRecords.Columns["Add"].Index)
        {
            Form5 frm2 = new Form5(dgRecords.Rows[dgRecords.CurrentRow.Index].Cells[0].Value.ToString());
            frm2.FormClosed += new FormClosedEventHandler(childForm_FormClosed);
            frm2.Show();
            this.Hide();
        }

        else if (e.ColumnIndex == dgRecords.Columns["Close"].Index)
        {
            Form6 frm = new Form6(dgRecords.Rows[dgRecords.CurrentRow.Index].Cells[0].Value.ToString());
            frm.FormClosed += new FormClosedEventHandler(childForm_FormClosed);
            frm.Show();
            this.Hide();
        }

        else if (e.ColumnIndex == dgRecords.Columns["View"].Index)
        {
            Form4 frm3 = new Form4(dgRecords.Rows[dgRecords.CurrentRow.Index].Cells[0].Value.ToString());
            frm3.FormClosed += new FormClosedEventHandler(childForm_FormClosed);
            frm3.Show();
            this.Hide();
            }

        }
     }
  }

Stored Procedure of TotalRecords:

          ALTER PROCEDURE [dbo].[getTotalNo]
          AS
          BEGIN

          select count(*) total from tblTicketDetail where status = 1

          END

Stored Procedure of ShowRecords:

    ALTER PROCEDURE [dbo].[showRecord]
    @pagesize int,
    @skip int

    AS
    BEGIN

    SELECT TOP (@pagesize) * FROM tblTicketDetail WHERE TicketID NOT IN (SELECT TOP (@Skip) TicketID FROM tblTicketDetail)

    END

Upvotes: 1

Views: 242

Answers (1)

NewbieProgrammer
NewbieProgrammer

Reputation: 864

I have just resolved my own problem, instead of mentioning cell number ("Cell[0]") I mentioned cell HeaderText (Cell["TicketID"])

Upvotes: 1

Related Questions