[C#] 데이터셋 연동하기
첫째 BindingSource를 생성하고 거기에 데이터를 넣는다.
둘째 DataGridView의 DataSource에 BindingSource를 대입한다.
예제)
DataGridView dgv = new DataGridView();
BindingSource binding = new BindingSource();
binding.DataSource = GetData("Select * From Products");
dgv.DataSource = binding;
dgv.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
DataTable GetData(string sqlCommand)
{
string connectionString = "Integrated Security=SSPI;" +
"Persist Security Info=False;" +
"Initial Catalog=Northwind;Data Source=localhost";
SqlConnection northwindConnection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand(sqlCommand, northwindConnection);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = command;
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
adapter.Fill(table);
return table;
}