Support us on YouTube by Subscribing our YouTube Channel. Click here to Subscribe our YouTube Channel

Saturday, 7 March 2015

Display Date and Time in StatusStrip / StatusBar in Windows Form Application

Generally, You have seen many applications in which Date and Time is displayed in the StatusBar. In this Article, We will see How to Display Date and Time in StatusStrip / StatusBar in Windows Form Application.

Let's Begin:
1. Open Visual Studio and Create a New Windows Form Application.
2. Drop StatusStrip Control from the ToolBox.
3. Click on down arrow of StatusStrip and add StatusLabel on it (named as toolStripStatusLabel1).
Set Text of toolStripStatusLabel1 as Empty or "".
4. Drop the Timer control from the ToolBox -> Go to Properties of timer1 control -> Set Interval to 1000 milliseconds and Enabled to True.
5. Create Tick event of timer1 control by double clicking on the timer1 control and Add following line of code:
using System;
using System.Windows.Forms;

namespace StatusBarDateTime
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //Timer Tick Event
        private void timer1_Tick(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = DateTime.Today.ToLongDateString();
        }
    }
}
Preview:

Display Current Date and Time:
using System;
using System.Windows.Forms;

namespace StatusBarDateTime
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //Timer Tick Event
        private void timer1_Tick(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = DateTime.Now.ToString();
        }
    }
}
Preview:
I hope you like it. Thanks.
[Download Source Code via Google Drive]

Sunday, 1 March 2015

Chart Control in Windows Form Application

In this Article, We will see How to use the Chart control in Windows Form Application. As we know that Charts and Graphs makes data easier to understand and interpret. Chart is used to present the data in visual form.

Let's Begin:
1. Open Visual Studio (I am using Visual Studio 2012) and Create new Windows Form Application (.NET Framework 4).
2. Drop Chart control from Toolbox present in the Data tab.

3. Go to Chart properties -> Click on Series.
Change the Name of Series. Here, I set Name as Salary. We can also change the ChartType as well as appearance from the Series Collection Editor.
Go to Form1.cs Code and add these line of code:
using System;
using System.Windows.Forms;

namespace DemoChart
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            fillChart();
        }
        //fillChart method
        private void fillChart()
        {
            //AddXY value in chart1 in series named as Salary
            chart1.Series["Salary"].Points.AddXY("Ajay", "10000");
            chart1.Series["Salary"].Points.AddXY("Ramesh", "8000");
            chart1.Series["Salary"].Points.AddXY("Ankit", "7000");
            chart1.Series["Salary"].Points.AddXY("Gurmeet", "10000");
            chart1.Series["Salary"].Points.AddXY("Suresh", "8500");
            //chart title
            chart1.Titles.Add("Salary Chart");
        }
    }
}
Preview:

Displaying Data in Chart control from the Database in Windows Form Application:
1. Create a Database (named as Sample). Add a Table tbl_EmpSalary. The following is the table schema for creating tbl_EmpSalary.
Follow above steps. Add these lines of Code in Form1.cs Code:
using System;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace DemoChart
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            fillChart();
        }
        //fillChart method
        private void fillChart()
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Sample;Integrated Security=true;");
            DataSet ds = new DataSet();
            con.Open();
            SqlDataAdapter adapt = new SqlDataAdapter("Select Name,Salary from tbl_EmpSalary", con);
            adapt.Fill(ds);
            chart1.DataSource = ds;
            //set the member of the chart data source used to data bind to the X-values of the series
            chart1.Series["Salary"].XValueMember = "Name";
            //set the member columns of the chart data source used to data bind to the X-values of the series
            chart1.Series["Salary"].YValueMembers = "Salary";
            chart1.Titles.Add("Salary Chart");
            con.Close();

        }
    }
}
Final Preview:
Hope you like it. Thanks.
[Download Source Code via Google Drive]

Sunday, 15 February 2015

Databinding in ComboBox in C#

In this Article, we will see How to bind data in ComboxBox from database in C# Windows Form Application. In previous post, we saw How to:

Let's Begin:
1. Go to New -> Project -> Select Windows Form Application.
2. Create a Database (named as Sample). Add a Table tbl_Country. The following is the table schema for creating tbl_Country.
3. Drop Label and ComboBox control from the toolbox.
Form1.cs Code:

using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ComboBoxDataBind
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //Form Load
        private void Form1_Load(object sender, EventArgs e)
        {
            fillComboBox();
        }
        //fillComboBox method for filling the ComboBox with Data
        private void fillComboBox()
        {
            try
            {
                using (SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Sample;Integrated Security=true;"))
                {
                    con.Open();
                    SqlDataAdapter adapt = new SqlDataAdapter("Select * from tbl_Country", con);
                    DataTable dt = new DataTable();
                    adapt.Fill(dt);
                    //Set Displaymember as Country
                    cmb_Country.DisplayMember = "Country";
                    //Set ValueMember as ID
                    cmb_Country.ValueMember = "ID";
                    cmb_Country.DataSource = dt;
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        //cmb_Country_SelectedIndexChanged Event
        private void cmb_Country_SelectedIndexChanged(object sender, EventArgs e)
        {
            lbl_DisplayValue.Text = cmb_Country.SelectedValue.ToString();
        }
    }
}
In the above code, I have created fillComboBox() method for filling the ComboBox with data and also created cmb_Country_SelectedIndexChanged event which fires when the SelectedIndex property changed.
Final Preview:
Hope you like it. Thanks.
[Download Source code via Google Drive]

Monday, 2 February 2015

Search Record in DataGridView C#

In this Article, we will learn How to Search Record in DataGridView in C# Windows Form Application. In previous post, we saw How to Insert, Update and Delete Record in DataGridView C#.

Let's Begin:
1. Create a new Windows Form Application.
2. Create Database (named as Sample). Add a Table tbl_Employee. The following is the table schema for creating tbl_Employee.
3. Create a form (named as frmSearch) and Drop Label, TextBox and DataGridView control from the ToolBox.
Now, go to frmSearch.cs code and add System.Data and System.Data.SqlClient namespace.
frmSearch.cs code:
using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SearchRecord
{
    public partial class frmSearch : Form
    {
        //Connection String
        string cs = "Data Source=.;Initial Catalog=Sample;Integrated Security=true;";
        SqlConnection con;
        SqlDataAdapter adapt;
        DataTable dt;
        public frmSearch()
        {
            InitializeComponent();
        }
        //frmSearch Load Event
        private void frmSearch_Load(object sender, EventArgs e)
        {
            con = new SqlConnection(cs);
            con.Open();
            adapt = new SqlDataAdapter("select * from tbl_Employee",con);
            dt = new DataTable();
            adapt.Fill(dt);
            dataGridView1.DataSource = dt;
            con.Close();
        }
        //txt_SearchName TextChanged Event
        private void txt_SearchName_TextChanged(object sender, EventArgs e)
        {
            con = new SqlConnection(cs);
            con.Open();
            adapt = new SqlDataAdapter("select * from tbl_Employee where FirstName like '"+txt_SearchName.Text+"%'", con);
            dt = new DataTable();
            adapt.Fill(dt);
            dataGridView1.DataSource = dt;
            con.Close();
        }
    }
}
In the above code, we have created frmSearch_Load Event for Displaying tbl_Employee Data in DataGridView when form loads. txt_SearchName_TextChanged Event  fires when the Text of txt_SearchName TextBox changes.
Final Preview:
Hope you like it. Thanks.
[Download Source Code via Google Drive]

Subscribe us on YouTube

Subscribe Now

Popular Posts

Contact us

Name

Email *

Message *

Like us on Facebook