dataGridView在DataSource之后不显示数据

时间:2018-01-05 21:39:33

标签: c# datagridview sqldataadapter

我是C#的新手,并且有简单的方法来显示表DimCustomer中的数据。我从一个按钮调用此方法,但它不显示数据。虽然当我调试我的代码时,我可以看到数据,但不能显示它。请提出任何建议

private void ShowGridData()
        {
            // create a connection object
            string ConnectionString = "Integrated Security=SSPI;" +
            "database=AdventureWorksDW2012;" + "server=DESKTOP-L9L3SMT\\SQL2K12;";
            SqlConnection conn = new SqlConnection(ConnectionString);

            // open the connection
            conn.Open();

            // Create a DataTableMapping object
            DataTableMapping myMapping = new DataTableMapping("DimCustomer", "mapCustomer");
            SqlDataAdapter adapter = new SqlDataAdapter("Select * FROM DimCustomer where LastName='yang' and BirthDate='1966-04-08'", conn);

            // Call DataAdapter's TableMappings.Add method
            adapter.TableMappings.Add(myMapping);

            // Create a DataSet object and Call DataAdapter's Fill method
            DataSet ds = new DataSet();
            adapter.Fill(ds, "DimCustomer");
            dataGridView1.ColumnCount = 0;
            dataGridView1.DataSource = ds.DefaultViewManager;
        }

1 个答案:

答案 0 :(得分:0)

请你试试这个吗?

dataGridView1.DataSource = ds.Tables[0];