.NET Dinamik DataGrid

private void btnRefresh_Click(object sender, EventArgs e)
{
 //set the connection string
 string connString = @"Server =.\SQL2K17; Database = SampleDB; Trusted_Connection = True;";


 try
 {
   //sql connection object
   using (SqlConnection conn = new SqlConnection(connString))
   {

      //retrieve the SQL Server instance version
      string query = @"SELECT e.id,
      e.code,
      e.firstName,
      e.lastName,    
      l.code AS locationCode,
      l.descr AS locationDescr
      FROM dbo.employees e
       INNER JOIN dbo.location l
        ON l.id = e.locationID;";

      //define the SqlCommand object
      SqlCommand cmd = new SqlCommand(query, conn);


      //Set the SqlDataAdapter object
      SqlDataAdapter dAdapter = new SqlDataAdapter(cmd);

      //define dataset
      DataSet ds = new DataSet();

      //fill dataset with query results
      dAdapter.Fill(ds);

      //set DataGridView control to read-only
      grdData.ReadOnly = true;

      //set the DataGridView control's data source/data table
      grdData.DataSource = ds.Tables[0];


      //close connection
      conn.Close();
   }
 }
 catch (Exception ex)
 {
   //display error message
   MessageBox.Show("Exception: " + ex.Message);
 }

}

No comment

Bir cevap yazın

E-posta hesabınız yayımlanmayacak.