유용한 정보

[C#] 데이터셋 연동하기

DevReff 2025. 4. 16. 07:22
728x90
728x90
SMALL

    첫째 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;
    }

 

728x90
728x90
LIST

'유용한 정보' 카테고리의 다른 글

[C#] Excel to PrintPreview  (0) 2025.04.16
[C#] Excel To Printer  (0) 2025.04.16
[C#] DataGridView 컨트롤에서 열 고정하기  (0) 2025.04.16
[C#] 문자열을 속성으로 갖는 클래스 만들기  (0) 2025.04.16
[C#] INI Parser  (0) 2025.04.16