0% found this document useful (0 votes)
13 views

Windows Form Application With Data Binding

Uploaded by

jasonjacob
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Windows Form Application With Data Binding

Uploaded by

jasonjacob
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Windows form application

with data binding


Creating a Windows Form Application using
Notepad
• Create a blank form and show it on the screen
using System;
using System.Windows.Forms;
namespace NotepadForms {

public class MyForm : Form {

public MyForm() {
}

static void Main() {


Application.Run(new MyForm());
}

}}
public class MyForm : Form

• The Form class is one of the main classes in the System.Windows.Forms


namespace.
• Application.Run() method is responsible for starting the standard application
message loop.
Creating a Windows Form Application using
Visual Studio
• Form1.cs[Design] contain
the design layout
• To add control or
component press
Ctrl+Alt+X or Toolbox from
view menu
• Toolbox is available in left
side
• Press F4 or select
properties windows from
view menu, to change
property value of a control
• Solution Explorer -> Program.cs
• Right click in form->view code->Form1.cs
• Form1.cs[Design]
–Contain design layout
–Add controls by drag and drop
• Form1.cs
–Partial class
–Invoke InitializeComponent() in its constructor
–Controls events are handled here
• Form1.Designer.cs
–Partial class
–Implementation of InitializeComponent()
–Implementation of Dispose()
–Initialize controls that may added to form
• Program.cs
–Main method
–Application.Run(Formname)
Form
Controls
•Syntax to create a text box
private System.Windows.Forms.TextBox textBox1;
• Syntax to create a button
private System.Windows.Forms.Button button1;
•Syntax to create a label
private System.Windows.Forms.Label label1;
•Syntax to create a check box
private System.Windows.Forms.CheckBox checkBox1;
•Syntax to create a radio button
private System.Windows.Forms.RadioButton radioButton1;
•Syntax to create a combo box
private System.Windows.Forms.ComboBox comboBox1;
Data Binding
• Connection object - open
string source = @"Data Source= DESKTOP-GHTJOE3\SQLEXPRESS; Initial
Catalog=Student; Integrated Security=SSPI;";
SqlConnection con = new SqlConnection(source);
con.Open();
• Connection object - close
con.Close();
• Insert query
SqlCommand insertCommand = new SqlCommand("INSERT INTO [Login]
(Name,Passwd,Percentage) VALUES (@n,@p,@r)", con);
insertCommand.Parameters.Add("@n", SqlDbType.VarChar, 255, "Name").Value =
textBox1.Text;
insertCommand.Parameters.Add("@p", SqlDbType.VarChar, 255, "Passwd").Value =
textBox2.Text;
insertCommand.Parameters.Add("@r", SqlDbType.Int, 0, "Percentage").Value =
int.Parse(textBox3.Text);
int queryResult = insertCommand.ExecuteNonQuery();
• Delete query:
SqlCommand deleteCommand = new SqlCommand("DELETE FROM [Login] WHERE
Name=@n", con);
deleteCommand.Parameters.Add("@n", SqlDbType.VarChar, 255,"Name").Value =
textBox1.Text;
int queryResult = deleteCommand.ExecuteNonQuery();

• Update query:
SqlCommand updateCommand = new SqlCommand("UPDATE [Login] SET
Name=@n WHERE Percentage=@r", con);
updateCommand.Parameters.Add("@n", SqlDbType.VarChar, 255,"Name").Value =
textBox1.Text;
updateCommand.Parameters.Add("@r", SqlDbTypeSqlDbType.Int, 0,
"Percentage").Value = int.Parse(textBox3.Text);
int queryResult = updateCommand.ExecuteNonQuery();
• Select query to display in MessageBox
SqlCommand select = new SqlCommand("SELECT Percentage FROM Login where
Name=@n", con);
select.Parameters.Add("@n", SqlDbType.NVarChar, 255, "Name").Value =
textBox1.Text;
SqlDataReader aReader = select.ExecuteReader();
while (aReader.Read())
{
int n = aReader.GetInt32(0);
MessageBox.Show("Percentage " + n);
}
aReader.Close();
• Select query to display in DataGridView
SqlDataAdapter adp = new SqlDataAdapter("select * from Login", con);
DataSet ds = new DataSet();
adp.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
MDI in Windows Form
• The Multiple-Document Interface (MDI) is a specification that defines a user
interface for applications that enable the user to work with more than one
document at the same time under one parent form (window).
Form2 f = new Form2();
f.Show();

You might also like