2014-07-25 35 views
0

我有一個數據表與每行中的複選框。我想要檢查複選框的行的每個列值。請告訴我如何實現這一點。 這是我的代碼。當複選框被選中時,我想要特定的行細節。如何檢查複選框時選擇數據錶行?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using System.Data; 

namespace dataTable_test 
{ 
    /// <summary> 
    /// Interaction logic for Page1.xaml 
    /// </summary> 
    public partial class Page1 : Page 
    { 
     DataTable table = new DataTable(); 
     public Page1() 
     { 
      InitializeComponent(); 

      table = GetTable(); 
      enter_data(table); 


     } 
     static DataTable GetTable() 
     { 
      // 
      // Here we create a DataTable with four columns. 
      // 
      DataTable table = new DataTable(); 
      table.Columns.Add("Dosage", typeof(int)); 
      table.Columns.Add("Drug", typeof(string)); 
      table.Columns.Add("Patient", typeof(string)); 
      table.Columns.Add("Date", typeof(DateTime)); 
      table.Columns.Add("isSelected", typeof(bool)); 

      // 
      // Here we add five DataRows. 
      // 
      table.Rows.Add(25, "Indocin", "David", DateTime.Now,false); 
      table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now, false); 
      table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now,false); 
      table.Rows.Add(21, "Combivent", "Janet", DateTime.Now, false); 
      table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now, false); 
      return table; 
     } 
     private void enter_data(DataTable dataTable) 
     { 
      grid1.Children.Clear(); 
      grid1.RowDefinitions.Clear(); 
      grid1.ColumnDefinitions.Clear(); 

      int row = 0,col=0; 
      foreach (DataRow dr1 in dataTable.Rows) 
      { 
       RowDefinition row1 = new RowDefinition(); 
       row1.Height = new GridLength(50); 
       //ColumnDefinition column1 = new ColumnDefinition(); 
       //column1.Width = new GridLength(120); 

       if (row == 0) 
       { 
        col++; 
        row = 0; 
        ColumnDefinition column1 = new ColumnDefinition(); 
        column1.Width = new GridLength(120); 

        ColumnDefinition column2 = new ColumnDefinition(); 
        column2.Width = new GridLength(120); 

        ColumnDefinition column3 = new ColumnDefinition(); 
        column3.Width = new GridLength(120); 

        ColumnDefinition column4 = new ColumnDefinition(); 
        column4.Width = new GridLength(120); 

        ColumnDefinition column5 = new ColumnDefinition(); 
        column5.Width = new GridLength(120); 

        grid1.ColumnDefinitions.Add(column1); 
        grid1.ColumnDefinitions.Add(column2); 
        grid1.ColumnDefinitions.Add(column3); 
        grid1.ColumnDefinitions.Add(column4); 
        grid1.ColumnDefinitions.Add(column5); 
        } 

       grid1.RowDefinitions.Add(row1); 

       //grid1.ColumnDefinitions.Add(column2); 

       Button B1 = new Button(); 
       B1.Content = dr1["Dosage"].ToString(); 
       B1.Margin = new Thickness(5); 
       B1.Height = 40; 
       B1.Foreground = Brushes.Black; 
       B1.BorderBrush = Brushes.Black; 
       B1.Background = Brushes.LightGray; 
       Grid.SetRow(B1, row); 
       Grid.SetColumn(B1, 0); 

       Button B2 = new Button(); 
       B2.Content = dr1["Drug"].ToString(); 
       B2.Margin = new Thickness(5); 
       B2.Height = 40; 
       B2.Foreground = Brushes.Black; 
       B2.BorderBrush = Brushes.Black; 
       B2.Background = Brushes.LightGray; 
       Grid.SetRow(B2, row); 
       Grid.SetColumn(B2, 1); 

       Button B3 = new Button(); 
       B3.Content = dr1["Patient"].ToString(); 
       B3.Margin = new Thickness(5); 
       B3.Height = 40; 
       B3.Foreground = Brushes.Black; 
       B3.BorderBrush = Brushes.Black; 
       B3.Background = Brushes.LightGray; 
       Grid.SetRow(B3, row); 
       Grid.SetColumn(B3, 2); 

       Button B4 = new Button(); 
       B4.Content = dr1["Date"].ToString(); 
       B4.Margin = new Thickness(5); 
       B4.Height = 40; 
       B4.Foreground = Brushes.Black; 
       B4.BorderBrush = Brushes.Black; 
       B4.Background = Brushes.LightGray; 
       Grid.SetRow(B4, row); 
       Grid.SetColumn(B4, 3); 

       CheckBox B5 = new CheckBox(); 
       if (Convert.ToBoolean(dr1["isSelected"].ToString()) == true) 
        B5.IsChecked = true; 
       else B5.IsChecked = false; 
       //B5.Checked += B5_Checked; 
       B5.Margin = new Thickness(5); 
       B5.Height = 40; 
       B5.Foreground = Brushes.Black; 
       B5.BorderBrush = Brushes.Black; 
       B5.Background = Brushes.LightGray; 
       Grid.SetRow(B5, row); 
       Grid.SetColumn(B5, 3); 

       grid1.Children.Add(B1); 
       grid1.Children.Add(B2); 
       grid1.Children.Add(B3); 
       grid1.Children.Add(B4); 
       grid1.Children.Add(B5); 
       row++;    
      } 

     } 


    } 
    } 

我沒有使用datagrid。我使用網格內的按鈕和複選框顯示我的數據表內容。

+0

。我正在使用按鈕顯示行的內容。 – aish1006

+0

*我有一個數據表,每行有一個複選框* ...不,不。 'DataTable'是一個數據類......你不能把'CheckBox's放在'DataTable'的任何地方。請編輯您的問題,使其可以理解。 – Sheridan

+0

我認爲@Sheridan所說的是你有一個DataTable **和**以及一個DataGrid。控件本身是一個DataGrid。爲了確定檢查哪些行,您需要查詢DataGrid,而不是DataTable。 – Zenexer

回答

0
You Can use below event of the datagridView 

    private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e) 
     { 
      if (dataGridView1.IsCurrentCellDirty) 
      { 
       dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit); 
      } 
     } 

在我的例子複選框在GridView的色譜柱1並且當其在列2檢查該值可以我不使用數據網格被帶到可變COL

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) 
     { 
      if (dataGridView1.Columns[e.ColumnIndex].Name == "Column1") 
      { 

       DataGridViewCheckBoxCell checkCell = 
        (DataGridViewCheckBoxCell)dataGridView1. 
        Rows[e.RowIndex].Cells["Column1"]; 
       if ((bool)checkCell.Value == true) { 
        int i = dataGridView1.CurrentRow.Index; 
        string col = dataGridView1.Rows[e.RowIndex].Cells["column2"].Value.ToString(); 
       } 

      } 
     } 
+0

這是在C#窗體中完成它的方式。不確定WPF。希望這可以幫助你。 – Hasanthi

+0

謝謝..真的有幫助.. – aish1006

相關問題