아무리 고도의 애플빠인 나라지만 먹고살려면 시키는대로 해야한다!

그래서 C#으로 프로그램을 만들고 있다.

오늘의 주제는 DataGridView binds to SQL Result

조선말로 하면 SQL 쿼리 결과를 DataGridView에 그대로 출력하고 싶은데 어찌하나요

이렇게 하면 된다.

SqlCeConnection conn = new SqlCeConnection();
conn = new SqlCeConnection ("Data Source=localdb.sdf; Password='****'");
conn.Open();
if (conn.State == ConnectionState.Closed)
{
    MessageBox.Show("닫혔따!");
    return;
}

string query = "select e1, e2, e3 from table1";

SqlCeDataAdapter adapter = new SqlCeDataAdapter(query, conn);
SqlCeCommandBuilder cb = new SqlCeCommandBuilder(adapter);

DataTable table = new DataTable();
adapter.Fill(table);

customGridView.DataSource = table;

물론 customGridView는 디자이너에서 만든 DataGridVIew 객체다.

이렇게 하면 customGridView에 아무 설정 해주지 않아도 알아서 결과를 뿌려준다.

...근데 cb는 저기 왜 있더라?
크리에이티브 커먼즈 라이센스
Creative Commons License
2010/07/21 14:13 2010/07/21 14:13
Posted by 호빵