我有一个数据表,每行都有一个复选框。我想要检查复选框的行的每个列值。请告诉我如何实现这一点。 这是我的代码。选中复选框后,我想要特定的行详细信息。
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 :(得分: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();
}
}
}