【问题标题】:Gridview not displaying values from DatabaseGridview 不显示数据库中的值
【发布时间】:2015-07-02 09:02:38
【问题描述】:

我试图在 DataGrid 中显示一个数据库表,但 DataGrid 没有显示。我只有一个文本框,在它下面我希望 DataGrid 显示表格。文本框正在显示,但 DataGrid 未显示。

<table>
     <tbody>
        <tr>
          <td class="Header">Public Holiday Date</td>
          <td>
             <asp:TextBox runat="server"  ID="txtDate" rel="datepicker" ></asp:TextBox>
          </td>
        </tr>
        <tr>
          <td>
            <asp:DataGrid runat="server" id="dataGridView1"  AutoGenerateColumns="true"></asp:DataGrid>    
          </td>
        </tr>
       </tbody>
     </table>

从数据库中获取数据的代码:

private DataSet GetBankHolidays()
    {
        DataSet ds = new DataSet();

        string sql = "proc_GetAllCustomers";

        string query = "SELECT BankHol FROM bankholidays";

        DataTable dt = new DataTable();
        using (MySql.Data.MySqlClient.MySqlDataAdapter adapter = new MySql.Data.MySqlClient.MySqlDataAdapter(sql, DataUtils.ConnectionStrings["TAT"]))
        {
            adapter.SelectCommand.CommandType = CommandType.Text;
            adapter.SelectCommand.CommandText = query;
            adapter.Fill(dt);
            ds.Tables.Add(dt);
            dataGridView1.DataSource = dt;
            dataGridView1.DataBind();
        }
        return ds;
    }

我在页面加载中调用了这个方法:

 protected void Page_Load(object sender, EventArgs e)
    {
if (!Page.IsPostBack)
        {
            GetBankHolidays();
        }

    }

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    你需要调用DataBind方法:-

    dataGridView1.DataBind();
    

    除此之外,我建议您将 DAL 和 UI 代码分开。

    【讨论】:

    • 这给出了错误 DataGrid with id 'dataGridView1' could not automatically generate any columns from the selected data source.
    • 尝试将AutoGenerateColumns属性设置为true
    【解决方案2】:

    dataGridView1.DataSource = dt;下面写dataGridView1.DataBind();

    【讨论】:

    • 这给出了错误 DataGrid with id 'dataGridView1' could not automatically generate any columns from the selected data source.
    • 在您的 aspx 代码中将 gridview 的 Autogeneratecolumn 属性设置为 true
    • @user123456789 您确定 Datatable 包含任何记录。 ?
    • @user123456789 不是我要问的数据库 adapter.Fill(dt); 是否包含记录?
    • 尝试调试并查看dt中的值
    【解决方案3】:

    您的代码缺少{dataGridView1.DataBind();} 命令。

    使用DataBind() 方法将数据从数据源绑定到GridView 控件。

    【讨论】:

      【解决方案4】:

      在您的编码中,您使用的是stored procedure,但您有 提到的命令类型为文本

      adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
      

      您需要将 DataSource 绑定到 DataGrid

      dataGridView1.DataBind();
      

      最终代码

      using (MySql.Data.MySqlClient.MySqlDataAdapter adapter = new MySql.Data.MySqlClient.MySqlDataAdapter(sql, DataUtils.ConnectionStrings["TAT"]))
              {
                  adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
                  adapter.SelectCommand.CommandText = query;
                  adapter.Fill(dt);
                  ds.Tables.Add(dt);
                  dataGridView1.DataSource = dt;
                  dataGridView1.DataBind();
              }
      

      【讨论】:

        猜你喜欢
        • 2022-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-05
        • 2012-08-31
        • 2020-11-07
        • 2016-10-16
        • 2014-02-21
        相关资源
        最近更新 更多