0
How to bind data (from sql table) directly into gridview without using Datatable ?
6 Réponses
0
Why should you want to do that ?
0
nothing specific..just want to know a way to do it !!!
0
I would not recommend it because it merges your datalayer with the ui-layer. This means if you want to change the one or the other you have to change both.
I found one example where they use a sqlconnection, a sqlcommand and a sqlreader.
https://stackoverflow.com/questions/18960601/how-to-bind-a-gridview-from-database
using (SqlConnection con = new SqlConnection("Data Source=RapidProgramming;Integrated Security=true;Initial Catalog=RPDB"))
{
con.Open();
SqlCommand cmd = new SqlCommand("Select Name,Salary FROM YOUR TABLE", con);
SqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
con.Close();
}
0
thanks for the answer but in visual studio 2015 gridview.DataBind() property is not being allowed
0
Oeps! that is true;
Does it work without ?
https://social.msdn.microsoft.com/Forums/vstudio/en-US/e6698cad-15f7-41e7-82c3-cc71c17f30e2/datagridview-no-databind?forum=csharpgeneral
The DataBind method is not needed in winforms, just in webforms. Remove the last line, and your datagrid should work fine.
I have not tested it, I do not have a test database a the moment.
0
nope that's the problem..in windows form we need to create a DataTable else we have create an object of BindingSource then assign to datagriview datasource