简体   繁体   中英

GridView in ASP.NET

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();

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();

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

使用此代码合并数据集

dataSet.Merge(changeDataSet);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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