I am attempting to display data from my database into a gridview How can I assign the va开发者_JAVA百科lue to the gridview?
while (rdr.Read())
{
///Console.WriteLine(rdr[0]);
gridview1.Text = rdr[0];
}
Try this:
gridview.datasource=cmd.ExecuteReader(); gridview.databind();
sql_connection = new SqlConnection(conn_str);
sql_command = new SqlCommand(query, sql_connection);
sql_adapter = new SqlDataAdapter();
sql_adapter.SelectCommand = sql_command;
sql_connection.Open();
sql_adapter.Fill( dataSet1.Tables.GridViewDataTable );
sql_connection.Close();
DataTable dt = new DataTable;
SqlDatadapter.Fill(dt);
gridview1.DataSource=dt;
gridview1.DataBind();
You can do it by using the datasource property of GridView and assign it to the DataTable and the call the DataBind method on gridview.
精彩评论