c# - datagridview is showing empty rows in windows form -


in database there table called student. table has 6 rows datagridview of windows form showing 6 empty rows. have tried several ways including studentdatagridview.allowusertoaddrows = true/false; nothing working. shows empty rows.

using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; using system.data.sqlclient;  namespace varsityproject {     public partial class report : form     {         public report()         {             initializecomponent();         }          private void report_load(object sender, eventargs e)         {             string connectionstring = @"server=.\sqlexpress; database = varsityproject; integrated security = true";             sqlconnection connection = new sqlconnection(connectionstring);             string query = "select * student";             sqlcommand command = new sqlcommand(query, connection);             connection.open();              sqldatareader reader = command.executereader();              list<students> stu = new list<students>();             students student2 = new students();              while (reader.read())             {                 student2.id = (int)reader["id"];                 student2.firstname = reader["firstname"].tostring();                 student2.lastname = reader["lastname"].tostring();                 student2.program = reader["program"].tostring();                 student2.birthdate = reader["birthdate"].tostring();                 student2.fathersname = reader["fathersname"].tostring();                 student2.mothersname = reader["mothersname"].tostring();                 student2.presentaddress = reader["presentaddress"].tostring();                 student2.permanentaddress = reader["permanentaddress"].tostring();                 stu.add(student2);             }             reader.close();             connection.close();             studentdatagridview.datasource = stu;         }     } } 

the above code have posted working fine without issues.

your student properties (poco) class should be:

public class students {     public int id { get; set; }     public string firstname { get; set; }     public string lastname { get; set; }     public string program { get; set; }     public string birthdate { get; set; }     public string fathersname { get; set; }     public string mothersname { get; set; }     public string presentaddress { get; set; }     public string permanentaddress { get; set; } } 

and table schema should be:

enter image description here


Comments