ASP.NET 中的 GridView

发布于 2024-11-08 04:46:12 字数 520 浏览 0 评论 0原文

我正在使用 DataGrid 控件。我将它与ADO.NET绑定。

我希望如果有任何新数据出现,它应该在网格视图中将其与旧数据列一起附加。如何做到这一点?

DataTable dt = new DataTable();
SqlConnection con = new SqlConnection();
con.ConnectionString = "server = (local); initial Catalog = SQLTraining; Integrated Security = SSPI";
con.Open();
string Selectstring = "select "+attribute_name+" from "+tablename+"";
SqlCommand cmd = new SqlCommand(Selectstring, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);

gd.DataSource = dt;
gd.DataBind();

i am using DataGrid control. i bind it with the ADO.NET.

i want if any new data comes it should append it with the old data column wise in grid view. how this can be done?

DataTable dt = new DataTable();
SqlConnection con = new SqlConnection();
con.ConnectionString = "server = (local); initial Catalog = SQLTraining; Integrated Security = SSPI";
con.Open();
string Selectstring = "select "+attribute_name+" from "+tablename+"";
SqlCommand cmd = new SqlCommand(Selectstring, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);

gd.DataSource = dt;
gd.DataBind();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苏大泽ㄣ 2024-11-15 04:46:12

尝试使用 DataTable.Merge()

DataTable newData = GetDataFromDatabase(); // your code there
DataTable oldData = (DataTable)gd.DataSource;
gd.DataSource = oldData.Merge(newData);
gd.DataBind();

无论如何,考虑使用 using(){} 块:

DataTable dataTable = new DataTable();
string connectionString = "server = (local); initial Catalog = SQLTraining; Integrated Security = SSPI";
string selectCommandText = String.Format("SELECT {0} FROM {1}",
    attribute_name,
    tablename);
using (SqlConnection selectConnection = new SqlConnection(connectionString))
{
    using (SqlDataAdapter adapter = new SqlDataAdapter(selectCommandText, selectConnection))
    {
        adapter.Fill(dataTable);
    }
}
gridView.DataSource = dataTable;
gridView.DataBind();

Try to use DataTable.Merge():

DataTable newData = GetDataFromDatabase(); // your code there
DataTable oldData = (DataTable)gd.DataSource;
gd.DataSource = oldData.Merge(newData);
gd.DataBind();

Anyway, consider to use using(){} block:

DataTable dataTable = new DataTable();
string connectionString = "server = (local); initial Catalog = SQLTraining; Integrated Security = SSPI";
string selectCommandText = String.Format("SELECT {0} FROM {1}",
    attribute_name,
    tablename);
using (SqlConnection selectConnection = new SqlConnection(connectionString))
{
    using (SqlDataAdapter adapter = new SqlDataAdapter(selectCommandText, selectConnection))
    {
        adapter.Fill(dataTable);
    }
}
gridView.DataSource = dataTable;
gridView.DataBind();
陪你搞怪i 2024-11-15 04:46:12

您可以使用 DataTable.Merge 方法。请查看 MSDN

You could use DataTable.Merge method. Have a look at MSDN.

叫思念不要吵 2024-11-15 04:46:12

使用此代码合并数据集

dataSet.Merge(changeDataSet);

use this code for merge the dataset

dataSet.Merge(changeDataSet);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文