2

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

3 Answers 3

1

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();
Sign up to request clarification or add additional context in comments.

1 Comment

@naveen: to make it even more correct, it's better to use parameterized queries. can you add that please?:)
0

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

Comments

0

use this code for merge the dataset

dataSet.Merge(changeDataSet);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.