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

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