【问题标题】:Pagination not working properly with DataGridView?分页无法与 DataGridView 一起正常工作?
【发布时间】:2014-03-22 11:34:39
【问题描述】:

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

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

现在加载网格时,它工作正常(所有 3 个按钮在参数中成功发送 TicketID),但是每当我点击分页控件(第一个、上一个、下一个、最后一个)时,我添加的 3 个按钮不起作用适当地。我的意思是,他们不是发送 TicketID(第 0 列)作为参数,而是发送列的“ButtonName(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

【问题讨论】:

  • 将标签应用于问题时请注意标签。 paging 指的是完全不同的东西。
  • 另外,我找不到任何sql代码

标签: c# datagridview pagination


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-21
    • 2014-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    • 2020-08-19
    相关资源
    最近更新 更多