【问题标题】:Bind GridView with table from sql server将 GridView 与 sql server 中的表绑定
【发布时间】:2014-03-12 13:50:49
【问题描述】:

我想将我的网格视图与来自 sql server 的表绑定,但没有得到适当的解决方案。 与我分享它是如何完成的 我的应用程序是一个测试应用程序

【问题讨论】:

  • 欢迎来到山姆!发布您迄今为止尝试过的内容以及收到的错误,这样会更容易提供帮助。

标签: c# sql sql-server gridview data-binding


【解决方案1】:

私有字符串 ConnectionString { get;放; } //绑定你的连接字符串

私有 SqlCommand cmd;

private SqlConnection con;

私有数据集 ds;

数据集 GetTableData() {

    try

    {

        using (con = new SqlConnection(ConnectionString)) 

        {

            cmd = new SqlCommand("select * from <table-name> ", con);

            ds = new DataSet();


            var data = new SqlDataAdapter(cmd);

            data.Fill(ds);

        }

    }

    catch (Exception ex)

    {

        //Error Logic

    }

    return ds;

}

一旦你得到从 abovefunction 返回的值,绑定到 gridview

[GridviewID].dataSource=GetTableData();

[GridviewID].dataBind();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2012-11-30
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    • 2012-02-17
    相关资源
    最近更新 更多