Wednesday, 1 January 2014

How To Show Data In Text Box Through Grid view

How To Show Data In Text Box Through Grid view



        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True");
        DataTable dt;
        private void id_search_TextChanged(object sender, EventArgs e)
        {
            DataTable dd = new DataTable();
            SqlCommand cmd = new SqlCommand("select ID,Name,Designation,Salary from Accounts where ID in  ('" + id_search.Text + "')", con);
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            SqlDataAdapter dt = new SqlDataAdapter();

            if (dr.Read())
            {
                dr.Close();
                dt.SelectCommand = cmd;
                dt.Fill(dd);
                dataGridView1.DataSource = dd;
            }
            else
            {
            }
            con.Close();
        }
   
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs a)
        {
            if (a.RowIndex >= 0)
            {
                DataGridViewRow row = this.dataGridView1.Rows[a.RowIndex];

                txtid.Text = row.Cells["ID"].Value.ToString();
                txtname.Text = row.Cells["Name"].Value.ToString();
                txtdes.Text = row.Cells["Designation"].Value.ToString();
                txtsal.Text = row.Cells["Salary"].Value.ToString();
            }
            }
        private void LoadDatabase_btn_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("select * from Accounts", con);
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            dt = new DataTable();
            da.Fill(dt);
            dataGridView1.DataSource = dt;
        }

No comments:

Post a Comment