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

C# 如何将用户输入值从TextBox和ComboBox插入DataGrid,c#,.net,wpf,C#,.net,Wpf,如何将用户输入值从TextBox和ComboBox插入到DataGrid 当我们点击Save按钮时,我已经完成了这项工作 在我的Xmal中: <Grid Margin="10,10,12,12"> <DataGrid Name="customerDataGrid" CanUserAddRows="True" CanUserDeleteRows="True" Margin="34,56,237,73" AlternatingRowBackground="{

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

当我们点击Save按钮时,我已经完成了这项工作

在我的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; }

    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Shapes;
命名空间WpfApplication3
{
/// 
///Window1.xaml的交互逻辑
/// 
公共部分类Window1:Window
{
公共窗口1()
{
初始化组件();
customerDataGrid.ItemsSource=LoadCollectionData();
}
公共列表LoadCollectionData()
{
列表客户=新列表();
customer.Add(新客户()
{
Bay=“Kolhapur 1”,
Euqipment=“JKL”,
ID=“JKL”,
Startmdy=新日历(),
Endmdy=新日历(),
目的=“加油。”,
Requestedby=“Ashish.”,
});
退货客户;
}
}
公共类客户
{
公共字符串间隔{get;set;}
公共字符串设备{get;set;}
公共字符串ID{get;set;}
公共日历开始日期{get;set;}
公共日历Endmdy{get;set;}
公共字符串用途{get;set;}
{get;set;}请求的公共字符串
}
}

我试过很多方法,但都不管用。任何帮助或参考都可以。

创建一个集合来表示datagrid的每一行。您将使用上下文…(客户端)


循环浏览数据网格中的记录并找到每行的文本框-提取该值并添加到您的集合中。

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

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

<DataGrid ItemsSource="{Binding}" ...
对于组合框,将
SelectedValue
属性绑定到项目中的属性:

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

您尚未提供完整的代码,没有保存按钮的单击事件,xaml也不完整