如何将TextBox和ComboBox中的用户输入值插入DataGrid

时间:2011-02-12 11:34:42

标签: c# .net wpf

如何将用户输入值从TextBox和ComboBox插入DataGrid?

当我们点击“保存”按钮时,我已经完成了这个

在我的Xmal:

<Grid Margin="10,10,12,12">
            <DataGrid Name="customerDataGrid" CanUserAddRows="True" CanUserDeleteRows="True" Margin="34,56,237,73" AlternatingRowBackground="{x:Null}" MinRowHeight="10"></DataGrid>
            <TextBlock Height="16" HorizontalAlignment="Left" Margin="554,110,0,0" Name="textBlock1" Text="Equipment" VerticalAlignment="Top" />
            <TextBlock Height="15" HorizontalAlignment="Left" Margin="554,137,0,0" Name="textBlock2" Text="ID" VerticalAlignment="Top" />
            <TextBlock Height="15" HorizontalAlignment="Left" Margin="554,163,0,0" Name="textBlock3" Text="Start Date" VerticalAlignment="Top" />
            <TextBlock Height="16" HorizontalAlignment="Left" Margin="554,187,0,0" Name="textBlock4" Text="End Date" VerticalAlignment="Top" />
            <TextBlock Height="16" HorizontalAlignment="Left" Margin="554,211,0,0" Name="textBlock5" Text="Requsted By" VerticalAlignment="Top" />
            <TextBlock Height="16" HorizontalAlignment="Left" Margin="554,233,0,0" Name="textBlock6" Text="Purpose" VerticalAlignment="Top" />
            <TextBlock Height="17" HorizontalAlignment="Left" Margin="568,56,0,0" Name="textBlock7" Text="ADD Maintainence Shedule" VerticalAlignment="Top" FontWeight="Bold" />
            <ComboBox Height="21" HorizontalAlignment="Left" Margin="617,110,0,0" Name="comboBox1" VerticalAlignment="Top" Width="99">
                <ComboBoxItem Content="LA"/>
                <ComboBoxItem Content="CVT"/>
                <ComboBoxItem Content="Isolator"/>
                <ComboBoxItem Content="WT"/>
                <ComboBoxItem Content="ICT"/>
                <ComboBoxItem Content="CT"/>
                <ComboBoxItem Content="PT"/>
                <ComboBoxItem Content="E/S"/>
                <ComboBoxItem Content="CB"/>
                <ComboBoxItem Content="BUS" />
                <ComboBoxItem Content="CAP" />
            </ComboBox>
            <ComboBox Height="21" HorizontalAlignment="Left" Margin="618,133,0,0" Name="comboBox2" VerticalAlignment="Top" Width="99">
                <ComboBoxItem Content="29A/00"/>
                <ComboBoxItem Content="29B/00"/>
                <ComboBoxItem Content="30A/03"/>
                <ComboBoxItem Content="45B/50"/>
                <ComboBoxItem Content="39A/00"/>
                <ComboBoxItem Content="59A/00"/>
                            </ComboBox>
            <DatePicker Height="21" HorizontalAlignment="Left" Margin="618,157,0,0" Name="datePicker1" VerticalAlignment="Top" Width="99" />
            <DatePicker Height="21" HorizontalAlignment="Left" Margin="618,0,0,251" Name="datePicker2" VerticalAlignment="Bottom" Width="99" />
            <TextBox Height="21" Margin="618,208,51,0" Name="textBox1" VerticalAlignment="Top" />
            <TextBox Height="21" Margin="618,231,51,0" Name="textBox2" VerticalAlignment="Top" />
            <Button Content="Save" Height="23" HorizontalAlignment="Left" Margin="612,270,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
            <TextBlock Height="16" HorizontalAlignment="Left" Margin="554,85,0,0" Name="textBlock8" Text="BAY" VerticalAlignment="Top" />
            <ComboBox Height="21" HorizontalAlignment="Left" Margin="617,85,0,0" Name="comboBox3" VerticalAlignment="Top" Width="99">
                <ComboBoxItem Content="Kolhapur 1" />
                <ComboBoxItem Content="Kolhapur 2" />
                <ComboBoxItem Content="Kolhapur 3" />
                <ComboBoxItem Content="Kolhapur 4" />
                <ComboBoxItem Content="Vita 1" />
                <ComboBoxItem Content="Vita 2" />
                <ComboBoxItem Content="Karad 1" />
                <ComboBoxItem Content="Karad 2" />
                <ComboBoxItem Content="Miraj" />
                <ComboBoxItem Content="Pophali" />
                <ComboBoxItem Content="Pedambe" />
                <ComboBoxItem Content="Peth" />
                <ComboBoxItem Content="Koyana 1" />
                <ComboBoxItem Content="Koyana 2" />
                <ComboBoxItem Content="Lonikand" />
                <ComboBoxItem Content="Lamboti" />
            </ComboBox>
        </Grid>

在我的Xmal.cs中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Shapes;

namespace WpfApplication3
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            customerDataGrid.ItemsSource = LoadCollectionData();
        }
    public List<Customer> LoadCollectionData()
        {
            List<Customer> customer = new List<Customer>();

    customer.Add(new Customer()
            {
                Bay = "Kolhapur 1",
                Euqipment = "JKL",
                ID = "JKL",
                Startmdy = new Calendar(),
                Endmdy = new Calendar(),
                Purpose = "Oiling.", 
                Requestedby = "Ashish.",
            });
            return customer;
        }
    }
public class Customer 
    {
        public string  Bay { get; set; }
        public string Euqipment { get; set; }
        public string ID { get; set; }
        public Calendar Startmdy { get; set; }
        public Calendar Endmdy { get; set; }
        public string Purpose { get; set; }
        public string Requestedby { get; set; }

    }
}

我尝试了很多方法,但他们没有工作。任何帮助或一些参考都可以。

2 个答案:

答案 0 :(得分:0)

创建一个集合来表示datagrid的每一行。您将使用Context ...(客户端)

遍历数据网格中的记录并找到每行的文本框 - 选取该值并添加到您的收藏中。

答案 1 :(得分:0)

您的XAML已关闭。它缺少将DataGrid中的控件连接到ItemsSource中的对象的数据绑定。

首先,不要在ItemsSource上设置DataGrid。将其DataContext设置为客户列表。然后通过绑定设置ItemsSource

<DataGrid ItemsSource="{Binding}" ...

对于文本框,将Text属性绑定到项目中的属性:

<TextBox Text="{Binding PropertyName, Mode=TwoWay}" ... />

对于组合框,将SelectedValue属性绑定到项目中的属性:

<ComboBox SelectedValue="{Binding PropertyName, Mode=TwoWay}" ... />