简体   繁体   English

选中复选框时将列表添加到数据网格

[英]Adding list to data grid when checkbox is checked

I am not fluent in WPF.我对 WPF 不流利。 I have a list in the data grid view and I also have a checkbox outside the Datagrid.我在数据网格视图中有一个列表,并且在数据网格之外还有一个复选框。 I need to have an output that when the checkbox is checked it will add another row to the Datagrid.我需要有一个output,当检查复选框时,它将在DataGrid中添加另一行。 Here's the code that I have.这是我拥有的代码。

.cs 。CS

public InletPiping()
        {
            InitializeComponent();

            DataTable dt = new DataTable();
            DataColumn inletno = new DataColumn("Inlet pipe #", typeof(string));
            DataColumn pipeSize = new DataColumn("Pipe size (inch)", typeof(string));
            DataColumn actualID = new DataColumn("Actual ID (mm)", typeof(string));
            DataColumn roughness = new DataColumn("Roughness (mm)", typeof(string));

            dt.Columns.Add(inletno);
            dt.Columns.Add(pipeSize);
            dt.Columns.Add(actualID);
            dt.Columns.Add(roughness);

            dt.Rows.Add("Inlet Pipe 1", 10, 254.00, 0.0460);
            dt.Rows.Add("Inlet Pipe 2", 10, 254.00, 0.0460);
            dt.Rows.Add("Inlet Pipe 3", 10, 254.00, 0.0460);
            dt.Rows.Add("Inlet Pipe 4", 10, 254.00, 0.0460);
            dt.Rows.Add("Inlet Pipe 5", 10, 254.00, 0.0460);

            dataGrid.ItemsSource = dt.DefaultView;

            DataTable dt2 = new DataTable();
            DataColumn description = new DataColumn("Description", typeof(string));
            DataColumn unit = new DataColumn("Unit", typeof(string));
            DataColumn case1 = new DataColumn("Case 1", typeof(string));
            DataColumn case2 = new DataColumn("Case 2", typeof(string));
            DataColumn case3 = new DataColumn("Case 3", typeof(string));
            DataColumn case4 = new DataColumn("Case 4", typeof(string));
            DataColumn case5 = new DataColumn("Case 5", typeof(string));
            DataColumn case6 = new DataColumn("Case 6", typeof(string));
            DataColumn max = new DataColumn("Max", typeof(string));

            dt2.Columns.Add(description);
            dt2.Columns.Add(unit);
            dt2.Columns.Add(case1);
            dt2.Columns.Add(case2);
            dt2.Columns.Add(case3);
            dt2.Columns.Add(case4);
            dt2.Columns.Add(case5);
            dt2.Columns.Add(case6);
            dt2.Columns.Add(max);

            dt2.Rows.Add("Mist fraction - predicted", "%", "0.00", "0.00");
            dt2.Rows.Add(" ", " ", " ", " "," "," ");
            dt2.Rows.Add("Mist flow rate", "m/hr", "0.00", "0.00");

            dataGrid2.ItemsSource = dt2.DefaultView;
        }

My Xaml我的 Xaml

        <StackPanel Margin="10,0,10,0">
            <Grid Margin="0,10,0,20">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="6*" />
                </Grid.ColumnDefinitions>

                <TextBlock Text="Results"  Grid.Column="0"/>
                <ComboBox  Margin="10,0" Height="30" SelectedIndex="0" MinWidth="140" HorizontalAlignment="Left"  Grid.Column="1">
                    <ComboBoxItem Content = "Inlet pipe 1"/>
                    <ComboBoxItem Content = "Inlet pipe 2"/>
                    <ComboBoxItem Content = "Inlet pipe 3"/>
                    <ComboBoxItem Content = "Inlet pipe 4"/>
                    <ComboBoxItem Content = "Inlet pipe 5"/>
                </ComboBox>
            </Grid>
            
            <DataGrid x:Name="dataGrid2" ColumnWidth="*"/>
        </StackPanel>
        <StackPanel>
            <Grid Margin="10,10,0,0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="6*" />
                    <ColumnDefinition Width="20*" />
                </Grid.ColumnDefinitions>
                
                <CheckBox x:Name="a1"   Grid.Column="0" HorizontalAlignment="Right"/>
                <TextBlock Text="Override predicted misfraction"  Grid.Column="1" HorizontalAlignment="Left"/>
            </Grid>
            
        </StackPanel>

This is my output.这是我的 output。 When the checkbox is checked data will appear on the space that I put red on it当复选框被选中时,数据将出现在我放红色的空间上

在此处输入图像描述

You can add a Checked event in the Checkbox of your XAML as shown below.您可以在 XAML 的 Checkbox 中添加 Checked 事件,如下所示。

<CheckBox x:Name="a1"   Grid.Column="0" HorizontalAlignment="Right" Checked="A1_Checked"/>

In the xaml.cs add the code to handle this event在 xaml.cs 中添加处理此事件的代码

private void A1_Checked(object sender, System.Windows.RoutedEventArgs e)
        {
            var row =(dataGrid.ItemsSource as DataView).ToTable().AsEnumerable().Where(x => string.IsNullOrWhiteSpace(Convert.ToString(x["Description"]))).FirstOrDefault();
            if (row != null)
            {
                row["Description"] = "Add whatever you want";
                row["Unit"] = "Add whatever you want";
                ....

                 dataGrid2.ItemsSource = dt2.DefaultView;
            }
        }

You could insert a new row to the DataTable in a Checked event handler like this:您可以在Checked事件处理程序中向DataTable插入新行,如下所示:

private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
    DataView dv = dataGrid2.ItemsSource as DataView;
    DataRow newRow = dv.Table.NewRow();
    newRow[0] = "";
    newRow[1] = "";
    newRow[2] = "";
    newRow[3] = "";
    newRow[4] = "";
    newRow[5] = "";
    dv.Table.Rows.InsertAt(newRow, 1);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM