asp.net 网格视图未显示,

cetgtptt  于 2023-02-06  发布在  .NET
关注(0)|答案(1)|浏览(185)
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        getControls();
    }
}

public void getControls()
{
    string connn = ConfigurationManager.ConnectionStrings["RiskRegisterDBConnectionString"].ConnectionString;

    DataTable ds = DB.GetDataTable("Select * from ControlTable", connn);

    // GridView11.DataSource = ds;
    GridView11.DataBind(); 
}

我试过使用AutoGenerationColumn作为“true”和“false”,但是在运行程序时,我无法在浏览器中显示gridview。

<asp:GridView ID="GridView11" runat="server" 
     AutogenerateColumns="False" Cellpadding="4" 
     CssClass="auto-style6" DataKeyNames="Control_ID" 
     DataSourceID="SqlDataSource1" 
     Forecolor="#333333" Gridlines="None" Width="1124px" >
kq0g1dla

kq0g1dla1#

尝试你的代码说这样:

public void getControls()
{
    string strCon =
        ConfigurationManager.ConnectionStrings["RiskRegisterDBConnectionString"].ConnectionString;

    using (SqlConnection conn = new SqlConnection(strCon))
    {
        string strSQL =
            @"SELECT * FROM ControlTable";

        using (SqlCommand cmd = new SqlCommand(strSQL, conn))
        {
            DataTable dt = new DataTable();
            conn.Open();
            dt.Load(cmd.ExecuteReader());
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
}

相关问题