2014-02-21 91 views
1

我試圖在C#windows應用程序中實現DataGridView的分頁。基本上我有一個簡單的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(); 
      } 

     } 
    } 
    } 

總記錄的存儲過程:

  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 
+0

在將問題應用於問題時請注意標籤。 ['paging'](http://stackoverflow.com/tags/paging/info)指的是*完全*不同的東西。 –

+0

另外,我找不到任何sql代碼 – Alexander

回答

1

我剛剛解決我自己的問題,而不是提到的單元格號(「Cell [0]」)我提到的單元格HeaderText(Cell [「TicketID」])

相關問題