繁体   English   中英

ASP.NET中的GridView

[英]GridView in ASP.NET

我正在使用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();

尝试使用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();

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

使用此代码合并数据集

dataSet.Merge(changeDataSet);

暂无
暂无

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

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