【问题标题】:How to select datatable row when checkbox is checked? [duplicate]选中复选框时如何选择数据表行? [复制]
【发布时间】:2014-09-16 21:11:25
【问题描述】:

我有一个数据表,每一行都有一个复选框。我想要选中复选框的行的每一列值。请告诉我如何实现这一点。 这是我的代码。选中复选框时,我想要特定的行详细信息。

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++;            
            }

        }


    }
    }

我没有使用数据网格。我使用网格内的按钮和复选框显示我的数据表内容。

【问题讨论】:

  • 我没有使用数据网格。正在使用按钮显示行的内容。
  • 我有一个数据表,每一行都有一个复选框...不,你没有。 DataTable 是一个数据类...您不能将 CheckBoxes 放在 DataTable 中的任何位置。请编辑您的问题以使其易于理解。
  • 我认为@Sheridan 的意思是你有一个DataTable and 和一个DataGrid。控件本身是一个 DataGrid。为了确定检查了哪些行,您需要查询 DataGrid,而不是 DataTable。

标签: c# wpf datatable


【解决方案1】:
You Can use below event of the datagridView

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

在我的示例中,CheckBox 位于 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();
                }

            }
        }

【讨论】:

  • 这是在 C# windows 窗体中执行此操作的方式。不确定WPF。希望这可以帮助你。
  • 谢谢..真的很有帮助..
猜你喜欢
  • 2023-03-08
  • 1970-01-01
  • 2017-10-02
  • 1970-01-01
  • 2023-03-16
  • 2015-11-30
  • 2016-05-13
  • 2012-08-16
  • 2016-06-15
相关资源
最近更新 更多