简体   繁体   English

datagrid不与数据表绑定

[英]datagrid doesn't bind with datatable

I'm working on a webpage in azure. 我正在使用Azure制作网页。 Here's a part of my code: 这是我的代码的一部分:

 using (SqlConnection conn = new SqlConnection("my connection string"))
            {
                SqlCommand cmd = new SqlCommand("SELECT * FROM Students", conn);
                conn.Open();
                SqlDataAdapter _adp = new SqlDataAdapter(cmd);
                DataTable ds = new DataTable();
                _adp.Fill(ds);
                GridView1.DataSource = ds;

                GridView1.DataBind();
            }

Off course that instead of "my connection string" there's the real one... 当然,不是“我的连接字符串”,而是真正的...

The problem is that the page loads but without the gridview for some reason... 问题是页面加载但由于某种原因没有gridview ...

Any help will be great, Thanks 任何帮助都会很棒,谢谢

EDIT: I also tried with datasets, like this: 编辑:我也尝试使用数据集,像这样:

 using (SqlConnection conn = new SqlConnection("my connection string"))
            {
                SqlCommand cmd = new SqlCommand("SELECT * FROM Students", conn);
                conn.Open();
                SqlDataAdapter _adp = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                _adp.Fill(ds);
                GridView1.DataSource = ds.Tables[0];

                GridView1.DataBind();
            }

Try with GridView1.DataSource = ds.Tables[0]; 尝试使用GridView1.DataSource = ds.Tables[0];

whole code will look as follows: 整个代码如下所示:

using (SqlConnection conn = new SqlConnection("my connection string"))
            {
                SqlCommand cmd = new SqlCommand("SELECT * FROM Students", conn);
                conn.Open();
                SqlDataAdapter _adp = new SqlDataAdapter(cmd);
                DataSet ds = new DataTable();
                _adp.Fill(ds);
                GridView1.DataSource = ds.Tables[0];

                GridView1.DataBind();
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM