blue dove

blue dove

  • NA
  • 20
  • 437

Want to Sort column first name in alphabetical order.

Jan 2 2016 2:35 PM
I am making telephone book in C-Sharp using visual studio and sql server for database and want to sort First Name column in alphabetical order please tell me what should I do now? This is  my code.
      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Windows.Forms;
      using System.Data.SqlClient;

    namespace Telephone
      {
      public partial class Phone : Form
    {
        SqlConnection con = new SqlConnection(@"Data Source = zohaib\SQLEXPRESS; Initial Catalog = bscs; Integrated Security = True");

        public Phone()
        {
            InitializeComponent();
        }

        private void Phone_Load(object sender, EventArgs e)
        {
            Display();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Clear();
            textBox3.Text = "";
            textBox4.Clear();
            comboBox1.SelectedIndex = -1;
            textBox1.Focus();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            con.Open();



            SqlCommand cmd = new SqlCommand(@"INSERT INTO [5th]
                
                ([First Name], [Last Name], [Mobile], [Email], [Catagory])

                
                VALUES ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + comboBox1.Text + "')", con);


          

            cmd.ExecuteNonQuery();

  
            con.Close();
            MessageBox.Show("successfully saved");
            Display();
        }
        void Display()
        {
            SqlDataAdapter sda = new SqlDataAdapter("select * from [5th]", con);

            DataTable dt = new DataTable();
            sda.Fill(dt);
            dataGridView1.Rows.Clear();
            foreach (DataRow item in dt.Rows)
            {
                int n = dataGridView1.Rows.Add();
                dataGridView1.Rows[n].Cells[0].Value = item["First Name"].ToString();
                dataGridView1.Rows[n].Cells[1].Value = item[1].ToString();
                dataGridView1.Rows[n].Cells[2].Value = item[2].ToString();
                dataGridView1.Rows[n].Cells[3].Value = item[3].ToString();
                dataGridView1.Rows[n].Cells[4].Value = item[4].ToString();



            }

        }

        private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
        {
            textBox1.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            textBox2.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
            textBox3.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
            textBox4.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
            comboBox1.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();

        }

        private void button3_Click(object sender, EventArgs e)
        {
            con.Open();

            SqlCommand cmd = new SqlCommand(@"DELETE FROM [5th]
                
               WHERE (MOBILE='" + textBox3.Text + "')", con);


            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("Delete sucessfully");
            Display();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            con.Open();

            SqlCommand cmd = new SqlCommand(@"UPDATE     [5th]
                
                 SET    [First Name]= '" + textBox1.Text + "',[Last Name]='" +     textBox2.Text + "',[Mobile]='" + textBox3.Text + "',[Catagory]='" + comboBox1.Text + "' WHERE (MOBILE='" + textBox3.Text + "')", con);


            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("update sucessfully");
            Display();
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {

            if (comboBox2.SelectedIndex == 0)
            {
                SqlDataAdapter sda = new SqlDataAdapter("Select * from [5th] Where [First Name] LIKE'%" + textBox5.Text + "%' ", con);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                dataGridView1.Rows.Clear();
                foreach (DataRow item in dt.Rows)
                {
                    int n = dataGridView1.Rows.Add();
                    dataGridView1.Rows[n].Cells[0].Value = item["First Name"].ToString();
                    dataGridView1.Rows[n].Cells[1].Value = item[1].ToString();
                    dataGridView1.Rows[n].Cells[2].Value = item[2].ToString();
                    dataGridView1.Rows[n].Cells[3].Value = item[3].ToString();
                    dataGridView1.Rows[n].Cells[4].Value = item[4].ToString();
                }
            }
            else
            {

                SqlDataAdapter sda = new SqlDataAdapter("Select * from [5th] Where Mobile LIKE'%" + textBox5.Text + "%' ", con);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                dataGridView1.Rows.Clear();
                foreach (DataRow item in dt.Rows)
                {
                    int n = dataGridView1.Rows.Add();
                    dataGridView1.Rows[n].Cells[0].Value = item["First Name"].ToString();
                    dataGridView1.Rows[n].Cells[1].Value = item[1].ToString();
                    dataGridView1.Rows[n].Cells[2].Value = item[2].ToString();
                    dataGridView1.Rows[n].Cells[3].Value = item[3].ToString();
                    dataGridView1.Rows[n].Cells[4].Value = item[4].ToString();

                }


            }
        }

 
    }
}

Answers (7)