Wpfdata
Wpfdata
Step 1: Click Start All Program Microsoft Visual Studio 2010 Microsoft Visual St
udio 2010
Step 2: Click
New Project
Step 3: Under
Visual C#
Give application name for your new WPF applications, mention the location and so
lution name.
Start Designing and Programming using WPF
Designing windows or web application is quite easy and interesting also. Let s sta
rt designing our first application using WPF.
WPF will generate XAML tags similar to HTML. Here I have used the below mentione
d tools for design:
fName_lbl -> First Name Label
fName_Txt -> Textbox
lName_lbl -> Last Name Label
lName_Txt -> Textbox
DOB_lbl -> Date of Birth Label
DOB_Txt -> Date Picker
City_lbl -> City Label
City_Txt -> Combo Box
New_Btn -> New Button
Add_Btn -> Add Button
Del_Btn -> Delete Button
Update_Btn -> Update Button
Datagrid1
Databinding to Datagrid1
r binding.
We can bind the data to datagrid by assigning it to datagrid s ItemsSource and menti
on the data path which will connect to database to get the data. This is an add
on feature in WPF.
App.config is a configuration file which will have the setting and other stuff f
or an application. Here, I have used this to store the database connectionstring
and it is as follows:
Collapse | Copy Code
<configuration>
<connectionstrings>
<add name="ConnectionString1"
connectionstring="Data Source=(Database file location goes here)
\DatabindusingWPF.sdf; Password=test@123; Persist Security Info=False;"
>
</add></connectionstrings>
</configuration>
Here, I have used SQL Server Compact 3.5 sp1 as my data source so we have to giv
e the exact path of the database file where it is stored. (Note: You need to giv
e exact path of the database file to make this code work).
Whenever we add, delete, update datagrid has to be changed accordingly, so I cre
ated a public method named BindGrid() .
Collapse | Copy Code
// Establishing Connection String from Configuration File
string _ConnectionString = ConfigurationManager.ConnectionStrings
["ConnectionString1"].ConnectionString;
public void BindGrid()
{
SqlCeConnection _Conn = new SqlCeConnection(_ConnectionString);
// Open the Database Connection
_Conn.Open();
SqlCeDataAdapter _Adapter = new SqlCeDataAdapter("Select * from Details", _C
onn);
DataSet _Bind = new DataSet();
_Adapter.Fill(_Bind, "MyDataBinding");
dataGrid1.DataContext = _Bind;
// Close the Database Connection
_Conn.Close();
}
In the above, I have shown a very simple way to get the SQL connection string wh
ich will connect to database and used dataset to bind the data to datagrid. The
below code will add new record using the values from the textbox.
Add New Record
To add new record on database, I had used simple insert query and it will be pop
ulated in the datagrid. The below will do the trick:
Collapse | Copy Code
private void Add_Btn_Click(object sender, RoutedEventArgs e)
{
try
{
SqlCeConnection _Conn = new SqlCeConnection(_ConnectionString);
// Open the Database Connection
_Conn.Open();
string _Date = DOB_Txt.DisplayDate.ToShortDateString();
// Command String
string _Insert = @"insert into Details
(fName,lName,DOB,City)
Values('" + fName_Txt.Text + "','" + lName_Txt.Text +
"','" +
_Date.ToString() + "','" + City_Txt.Text + "')";
// Initialize the command query and connection
SqlCeCommand _cmd = new SqlCeCommand(_Insert, _Conn);
// Execute the command
_cmd.ExecuteNonQuery();
MessageBox.Show("One Record Inserted");
fName_Txt.Text = string.Empty;
lName_Txt.Text = string.Empty;
DOB_Txt.Text = string.Empty;
City_Txt.Text = string.Empty;
this.BindGrid();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
To store only date into the database, here I have used
es trim the time.
ToShortDateString
which giv
Update Code
Collapse | Copy Code
private void Update_Btn_Click(object sender, RoutedEventArgs e)
{
try
{
SqlCeConnection _Conn = new SqlCeConnection(_ConnectionString);
// Open Database Connection
_Conn.Open();