0

How to bind data (from sql table) directly into gridview without using Datatable ?

20th Jun 2017, 3:58 AM
Suraj Kumar Pradhan
Suraj Kumar Pradhan - avatar
6 Antworten
0
Why should you want to do that ?
21st Jun 2017, 8:35 PM
sneeze
sneeze - avatar
0
nothing specific..just want to know a way to do it !!!
22nd Jun 2017, 3:59 AM
Suraj Kumar Pradhan
Suraj Kumar Pradhan - avatar
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(); }
22nd Jun 2017, 6:39 PM
sneeze
sneeze - avatar
0
thanks for the answer but in visual studio 2015 gridview.DataBind() property is not being allowed
22nd Jun 2017, 6:41 PM
Suraj Kumar Pradhan
Suraj Kumar Pradhan - avatar
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.
22nd Jun 2017, 8:01 PM
sneeze
sneeze - avatar
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
23rd Jun 2017, 2:35 AM
Suraj Kumar Pradhan
Suraj Kumar Pradhan - avatar