2013-05-22 20 views
0

我需要建立在datagridview的傳呼,我是找到了一個演示中programCall.com成功,這就是鏈接ProgramCall.com如何維護分頁鏈接點擊複選框檢查狀態中的datagridview在C#

下面是下載的演示屏幕截圖形式: - enter image description here

現在我更新此下載的代碼和零添加一個複選框列(0)指數的datagridview的: -

下面的屏幕截圖是更新。 enter image description here

這裏是我的問題;當我檢查第一頁的複選框,然後移動到第二頁以在第一頁返回後執行任何任務時,我的選中複選框未選中,我想維護其檢查狀態,因此任何屬性或方法都可以維護datagridview複選框檢查狀態或其他。

下面我給你我的更新的代碼: -

Form1.cs中: -

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Globalization; 

namespace DatagridviewPagination 
{ 
    public partial class Form1 : Form 
    { 

     private int CurrentPage = 1; 
     int PagesCount = 1; 
     int pageRows = 10; 

     BindingList<Employee> Baselist = null; 
     BindingList<Employee> Templist = null; 


     public Form1() 
     { 
      InitializeComponent(); 
      DataGridViewCheckBoxColumn CheckboxColumn = new DataGridViewCheckBoxColumn(); 
      CheckboxColumn.TrueValue = true; 
      dataGridView1.Columns.Add(CheckboxColumn); 
      dataGridView1.Rows.Add(1); 
      dataGridView1.Columns[0].HeaderText = "Select"; 
      dataGridView1.Columns[0].Width = 50; 
     } 


     private void Form1_Load(object sender, EventArgs e) 
     { 
      Baselist = FillDataforGrid(); 
      PagesCount = Convert.ToInt32(Math.Ceiling(Baselist.Count * 1.0/pageRows)); 

      CurrentPage = 1; 
      RefreshPagination(); 
      RebindGridForPageChange(); 
     } 

     //Method to generate temporary data 
     private BindingList<Employee> FillDataforGrid() 
     { 
      BindingList<Employee> list = new BindingList<Employee>(); 
      for (int i = 1; i <= 20; i++) 
      { 
       Employee obj = new Employee(i, "ProgramCall.com" + i.ToString(), "Finance" + i.ToString()); 
       list.Add(obj); 
      } 

      return list; 
     } 


     private void RebindGridForPageChange() 
     { 
      //Rebinding the Datagridview with data 
      int datasourcestartIndex = (CurrentPage - 1) * pageRows; 
      Templist = new BindingList<Employee>(); 
      for (int i = datasourcestartIndex; i < datasourcestartIndex + pageRows; i++) 
      { 
       if (i >= Baselist.Count) 
        break; 

       Templist.Add(Baselist[i]); 
      } 

      dataGridView1.DataSource = Templist; 
      dataGridView1.Refresh(); 
     } 

     //Method that handles the pagination button clicks 
     private void ToolStripButtonClick(object sender, EventArgs e) 
     { 
      try 
      { 
       ToolStripButton ToolStripButton = ((ToolStripButton)sender); 

       //Determining the current page 
       if (ToolStripButton == btnBackward) 
        CurrentPage--; 
       else if (ToolStripButton == btnForward) 
        CurrentPage++; 
       else if (ToolStripButton == btnLast) 
        CurrentPage = PagesCount; 
       else if (ToolStripButton == btnFirst) 
        CurrentPage = 1; 
       else 
        CurrentPage = Convert.ToInt32(ToolStripButton.Text, CultureInfo.InvariantCulture); 

       if (CurrentPage < 1) 
        CurrentPage = 1; 
       else if (CurrentPage > PagesCount) 
        CurrentPage = PagesCount; 

       //Rebind the Datagridview with the data. 
       RebindGridForPageChange(); 

       //Change the pagiantions buttons according to page number 
       RefreshPagination(); 
      } 
      catch (Exception) { } 
     } 


     private void RefreshPagination() 
     { 
      ToolStripButton[] items = new ToolStripButton[] { toolStripButton1, toolStripButton2, toolStripButton3, toolStripButton4, toolStripButton5 }; 

      //pageStartIndex contains the first button number of pagination. 
      int pageStartIndex = 1; 

      if (PagesCount > 5 && CurrentPage > 2) 
       pageStartIndex = CurrentPage - 2; 

      if (PagesCount > 5 && CurrentPage > PagesCount - 2) 
       pageStartIndex = PagesCount - 4; 

      for (int i = pageStartIndex; i < pageStartIndex + 5; i++) 
      { 
       if (i > PagesCount) 
       { 
        items[i - pageStartIndex].Visible = false; 
       } 
       else 
       { 
        //Changing the page numbers 
        items[i - pageStartIndex].Text = i.ToString(CultureInfo.InvariantCulture); 

        //Setting the Appearance of the page number buttons 
        if (i == CurrentPage) 
        { 
         items[i - pageStartIndex].BackColor = Color.Black; 
         items[i - pageStartIndex].ForeColor = Color.White; 
        } 
        else 
        { 
         items[i - pageStartIndex].BackColor = Color.White; 
         items[i - pageStartIndex].ForeColor = Color.Black; 
        } 
       } 
      } 

      //Enabling or Disalbing pagination first, last, previous , next buttons 
      if (CurrentPage == 1) 
       btnBackward.Enabled = btnFirst.Enabled = false; 
      else 
       btnBackward.Enabled = btnFirst.Enabled = true; 

      if (CurrentPage == PagesCount) 
       btnForward.Enabled = btnLast.Enabled = false; 

      else 
       btnForward.Enabled = btnLast.Enabled = true; 
     } 
    } 
} 

Employee.cs: -

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace DatagridviewPagination 
{ 
    class Employee 
    { 

     public Employee(int empid, string empname, string empdept) 
     { 
      this.empId = empid; 
      this.empName = empname; 
      this.empDept = empdept; 

     } 
     private int empId; 

     public int Empid 
     { 
      get { return empId; } 
      set { empId = value; } 
     } 

     private string empName; 

     public string EmpName 
     { 
      get { return empName; } 
      set { empName = value; } 
     } 

     private string empDept; 

     public string EmpDept 
     { 
      get { return empDept; } 
      set { empDept = value; } 
     } 

    } 
} 

謝謝高級!

回答

1

它的發生是因爲複選框列是動態的並且未鏈接到模型中的屬性。所以更改不會在頁面中持續存在。

Employee類必須有一個屬性,說bool Checked。我認爲datagrid會自動爲此創建一個複選框列。如果不是,則複選框列應該綁定到此屬性。

使用DataGridViewColumn.DataPropertyName物業列鏈接到一個模型屬性。

相關問題