How to insert user input values from TextBox and ComboBox to DataGrid?
When we click the Save button I have done this
In my 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" VerticalAl开发者_开发技巧ignment="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>
And in my 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; }
}
}
I have tried many ways but they are not working. Any help or some reference will do.
Create a collection to represent each row of the datagrid. You will be working with the Context...(client side)
Loop through the records in the data grid and find the text box of each row - pick up that value and add to your collection.
Your XAML is close. It's missing the data bindings that connect the controls in your DataGrid
to the objects in the ItemsSource
.
First, don't set the ItemsSource
on the DataGrid
. Set its DataContext
to the customer list instead. Then set the ItemsSource
via binding:
<DataGrid ItemsSource="{Binding}" ...
For your text boxes, bind the Text
property to the property in the item:
<TextBox Text="{Binding PropertyName, Mode=TwoWay}" ... />
For your combo boxes, bind the SelectedValue
property to the property in the item:
<ComboBox SelectedValue="{Binding PropertyName, Mode=TwoWay}" ... />
精彩评论