分页与DataGridView无法正常工作?

时间:2014-02-21 05:43:50

标签: c# datagridview pagination

我正在尝试在C#windows应用程序中的DataGridView中实现分页。基本上我有一个简单的DataGridView,它由数据库中的存储过程填充,并从另一个存储过程中获取总记录。

我还在网格中添加了三个按钮,它们引用了其他三种形式,并将它们作为参数发送给TicketID(网格中的第0列)。

现在加载网格时,它运行正常(所有3个按钮在参数中成功发送TicketID),但每当我点击分页控件(第一个,上一个,下一个,最后一个)时,我添加的3个按钮不起作用正常。我的意思是,不是将TicketID(第0列)作为参数发送,而是发送列的“ButtonName(.Text of DataGridView按钮)”。

我似乎无法弄清问题是什么,如果有人能帮助我,我真的很感激。

页面代码:

         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();
            }

        }
     }
  }

TotalRecords的存储过程:

          ALTER PROCEDURE [dbo].[getTotalNo]
          AS
          BEGIN

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

          END

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

1 个答案:

答案 0 :(得分:1)

我刚解决了自己的问题,而不是提及单元格编号(“Cell [0]”)我提到了单元格HeaderText(Cell [“TicketID”])