简体   繁体   中英

Bind data to grid view from different sources

I have gridview in my asp.net page, the get the data after page_load from the database directly.

        string sqlSelect = "SELECT *  FROM users";
        SqlCommand obj_Cmd = new SqlCommand(sqlSelect, con2);

        SqlDataReader obj_Reader = obj_Cmd.ExecuteReader();

        DataTable dt = new DataTable();
        dt.Columns.Add("Id");
        dt.Columns.Add("Name");
        while (obj_Reader.Read())
        {
            DataRow row = dt.NewRow();
            row["Id"] = obj_Reader["Id"];
            row["Name"] = obj_Reader["Name"];
            dt.Rows.Add(row);
        }
        GridView1.DataSource = dt;
        GridView1.DataBind();

and thats work fine, the gridview load the data, in the same page i have another datasource that should displayed in the gridview. the user can enter another name and it must be shown in the gridview with out deleting what exist.

        GridView1.DataSource = tableInsertedFromUsers;
        GridView1.DataBind();

what happend, when the user inserting another name (access the second datasource) every thing in the gridview deleted, and just displayed the second datasource and not the first.

i want to show both of them in the gridview.

any idea ? please help me.

Thank you

Declare a variable var first and store GridView1.DataSource . Then declare a second variable var to store the second datasource. Add them together into one datasource and bind the grid with this final datasource. And how to save result of a query in a variable, read this How to get SQL result saved into a C# variable?

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