Populate textbox2, textbox3 based on value textbox1
-
sábado, 04 de agosto de 2012 14:10
Hi All.
My form binding to Employee TableAdapter and has some TextBox controls. Such as UniqueNo, FirstName, LastName. I would like when user open form to create new record and type in UniqueNoTextBox value and if table has record for that UniqueNo in that case the FirstNameTextBox and LastNameTextBox will fill automatically with according data. If that possible how it to do? I will appreciate for sample.
For example. Employee table:
1 John Dou
2 Dog Smith
3 Mike CrueWhen user type in UniqueNoTextBox 2 the FirstNameTextBox and LastNameTextBox automatically filling by data Dog and Smith accordingly.
Thanks.
Todas las respuestas
-
sábado, 04 de agosto de 2012 16:14Moderador
I guess you'll be wanting something like this:
MainWindow.xaml
<Window x:Class="WpfApplication47.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" DataContext="{Binding ThisPerson}"> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <TextBlock Text="ID" /> <TextBlock Grid.Row="1" Text="First Name"/> <TextBlock Grid.Row="2" Text="Last Name"/> <TextBox Grid.Column="1" Text="{Binding ID, UpdateSourceTrigger=LostFocus}"/> <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding FirstName}"/> <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding LastName}"/> </Grid> </Window>MainWindow.xaml.cs
using System.Windows; using System.ComponentModel; namespace WpfApplication47 { public partial class MainWindow : Window { public Person ThisPerson { get; set; } MyDatabaseLayer db = new MyDatabaseLayer(); public MainWindow() { InitializeComponent(); ThisPerson = new Person(); ThisPerson.PropertyChanged += new PropertyChangedEventHandler(ThisPerson_PropertyChanged); DataContext = this; } void ThisPerson_PropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName=="ID" && ThisPerson.ID != 0) { var person = db.GetPerson(ThisPerson.ID); if (person != null) { ThisPerson.FirstName = person.FirstName; ThisPerson.LastName = person.LastName; } } } } public class MyDatabaseLayer { public Person GetPerson(int id) { // Do your stuff, return person if found return new Person { ID = id, FirstName = "Dog", LastName = "Smith" }; } } public class Person : INotifyPropertyChanged { int _ID; public int ID { get { return _ID; } set { if (_ID != value) { _ID = value; RaisePropertyChanged("ID"); } } } string _FirstName; public string FirstName { get { return _FirstName; } set { if (_FirstName != value) { _FirstName = value; RaisePropertyChanged("FirstName"); } } } string _LastName; public string LastName { get { return _LastName; } set { if (_LastName != value) { _LastName = value; RaisePropertyChanged("LastName"); } } } void RaisePropertyChanged(string prop) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(prop)); } public event PropertyChangedEventHandler PropertyChanged; } }
Regards,
Pete
#PEJL
- Editado XAML guyMicrosoft Community Contributor, Moderator sábado, 04 de agosto de 2012 16:14
- Editado XAML guyMicrosoft Community Contributor, Moderator sábado, 04 de agosto de 2012 16:39
- Editado XAML guyMicrosoft Community Contributor, Moderator sábado, 04 de agosto de 2012 16:41
- Marcado como respuesta Sheldon _XiaoModerator viernes, 17 de agosto de 2012 7:12
-
domingo, 05 de agosto de 2012 4:07
Try this,
You should include the unique no in your sql table.
Table Format:
Empolyee UniqueNo First_Name Last_Name 1 John Dou 2 Dog Smith 3 Mike Crue 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.Navigation; using System.Windows.Shapes; using System.Data; using System.Data.OleDb; namespace Employee { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { OleDbConnection con = new OleDbConnection(Properties.Settings.Default.EmpolyeeTableConnectionString); OleDbCommand cmd = new OleDbCommand(); OleDbDataReader rd; public MainWindow() { InitializeComponent(); } private void textBox1_TextChanged(object sender, TextChangedEventArgs e) { string FirstName = String.Empty, LastName = String.Empty; try { cmd.CommandText = "select First_Name,Last_Name from empolyee where UniqueNo = " + int.Parse(textBox1.Text); cmd.Connection = con; con.Open(); rd = cmd.ExecuteReader(); while (rd.Read()) { FirstName = rd.GetString(0); LastName = rd.GetString(1); } con.Close(); textBox2.Text = FirstName; textBox3.Text = LastName; } catch (Exception) { textBox2.Text = FirstName; textBox3.Text = LastName; } } } }Happy Coding!!!
- Editado Srithar domingo, 05 de agosto de 2012 4:08
- Marcado como respuesta Sheldon _XiaoModerator viernes, 17 de agosto de 2012 7:12
-
viernes, 17 de agosto de 2012 7:12Moderador
Hi eugzl,
I am marking your issue as "Answered", if you have new findings about your issue, please let me know.
Best regards,Sheldon _Xiao[MSFT]
MSDN Community Support | Feedback to us
Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

