This document contains C# code that connects to a SQL database, retrieves student and class data from tables, and displays it in a Windows Forms application. It opens a SQL connection, uses SQL data adapters to fill DataTables with data from "HocSinh" and "Lop" tables, sets the DataTables as data sources for a data grid view and combo box. It also includes an event handler that populates text boxes with selected row data from the grid.
1 of 1
Download to read offline
More Related Content
New text document
1. using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string cstr = "Server=.sqlexpress; Database=DBHocSinh; Integrated
security=true";
SqlConnection con = new SqlConnection(cstr);
con.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM HocSinh",
con);
DataTable hs = new DataTable();
da.Fill(hs);
dgvDSHS.DataSource= hs;
SqlDataAdapter da1 = new SqlDataAdapter("SELECT * FROM Lop", con);
DataTable lop = new DataTable();
da1.Fill(lop);
//Dien ra combobox lop
cboLop.DataSource = lop;
cboLop.DisplayMember = lop.Columns[1].ColumnName;
cboLop.ValueMember = "MaLop";
con.Close();
}
private void dgvDSHS_CellContentClick(object sender,
DataGridViewCellEventArgs e)
{
DoDuLieuRaLuoi(e.RowIndex);
}
private void DoDuLieuRaLuoi(int dong)
{
txtMaHS.Text = dgvDSHS.Rows[dong].Cells[0].Value.ToString();
txtTen.Text = dgvDSHS.Rows[dong].Cells[1].Value.ToString();
dtpNgSinh.Text = dgvDSHS.Rows[dong].Cells[2].Value.ToString();
txtDiaChi.Text = dgvDSHS.Rows[dong].Cells[3].Value.ToString();
txtDTB.Text = dgvDSHS.Rows[dong].Cells[4].Value.ToString();
cboLop.Text = dgvDSHS.Rows[dong].Cells[4].Value.ToString();
}
}
}