2016-05-14 8 views
0

チェックボックス(生年月日チェックボックス)がチェックされているときに、MS Accessに接続されているdataGridViewに特定の列(例として生年月日)を表示したいとします。複数のチェックボックスを追加したいので、それが配列の場合はもっと役立ちます。チェックボックスがチェックされているときに特定の列を表示する

namespace emp_db1 
{ 
    public partial class Print : Form 
    { 

     private OleDbConnection connection = new OleDbConnection(); 
     public Print() 
     { 
      InitializeComponent(); 
      connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source = emp_0.mdb"; 
     } 
     DataTable ds; 
     private void Print_Load(object sender, EventArgs e) 
     { 
      try 
      { 
       connection.Open(); 
       OleDbCommand command = new OleDbCommand(); 
       command.Connection = connection; 
       command.CommandText = "SELECT ID, Name, DOB FROM emp_per"; 
       OleDbDataAdapter da = new OleDbDataAdapter(command); 
       ds = new DataTable(); 
       da.Fill(ds); 
       dataGridView1.DataSource = ds; 
       da.Update(ds); 
       connection.Close(); 
       dataGridView1.AutoResizeColumns(); 
       dataGridView1.AutoResizeColumnHeadersHeight(); 
       foreach (DataGridViewColumn col in dataGridView1.Columns) 
       { 
        col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter; 
        col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; 
       } 
       this.dataGridView1.Sort(this.dataGridView1.Columns[0], ListSortDirection.Ascending); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show("Error " + ex); 
      } 
     } 
     private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) 
     { 
     } 
     private void dob_check_CheckedChanged(object sender, EventArgs e) 
     { 
     } 
    } 
} 

kyokou。

+1

なぜあなたは以前、同じ質問を削除したのですか? – Crowcoder

答えて

0

ソリューション:

namespace emp_db1 
{ 
    public partial class Print : Form 
{ 
    List<CheckBox> chkboxes = new List<CheckBox>(); 


    private OleDbConnection connection = new OleDbConnection(); 
    public Print() 
    { 
     InitializeComponent(); 
     connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source = emp_0.mdb"; 

    } 
    DataTable ds; 

    private void Print_Load(object sender, EventArgs e) 
    { 

    chkboxes.Add(dob_check); //0 
    chkboxes.Add(mother_check); //1 

     try 
     { 
      connection.Open(); 
      OleDbCommand command = new OleDbCommand(); 
      command.Connection = connection; 
      command.CommandText = "SELECT ID, Name, DOB, Mother FROM emp_per"; 
      OleDbDataAdapter da = new OleDbDataAdapter(command); 
      ds = new DataTable(); 
      da.Fill(ds); 
      dataGridView1.DataSource = ds; 
      da.Update(ds); 
      connection.Close(); 
      dataGridView1.AutoResizeColumns(); 
      dataGridView1.AutoResizeColumnHeadersHeight(); 

      foreach (DataGridViewColumn col in dataGridView1.Columns) 
      { 
       col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter; 
       col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; 
      } 
      this.dataGridView1.Sort(this.dataGridView1.Columns[0], ListSortDirection.Ascending); 

     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Error " + ex); 
     } 
    } 


    private void refreshCheckBoxes(int id) 
    { 
     for(int x = 0; x < 2; x++) 
     { 
      if (!chkboxes[x].Checked) dataGridView1.Columns[x+2].Visible = false; else dataGridView1.Columns[x+2].Visible = true; 

     } 
    } 

    private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e) 
    { 
    } 
    private void dob_check_CheckedChanged(object sender, EventArgs e) 
    { 
     refreshCheckBoxes(0); 
    } 

    private void mother_check_CheckedChanged(object sender, EventArgs e) 
    { 
     refreshCheckBoxes(1); 
    } 
    } 
} 
関連する問題