Beginner! I'm trying to display only certain data from an object into a data grid view, but I don't get anything after running in Winforms. Is there any way I could successfully implement this or any alternatives?
I was trying to follow the template here but attempt not successfull
// Object
public class Student
{
public string StudentNumber { get; set; }
public string StudentName { get; set; }
public string StudentNameChinese { get; set; }
public string StudentNameShort { get; set; }
public string Gender { get; set; }
public string IdPlaceDescription { get; set; }
public string MobileNumber { get; set; }
public string Major { get; set; }
public string RoomNumber { get; set; }
public string CheckInDate { get; set; }
public string CheckOutDate { get; set; }
public string RoomNoBefore { get; set; }
public string ChangeDate { get; set; }
public DateTime BirthDate { get; set; }
public string Province { get; set; }
public string Email { get; set; }
public string OtherEmail { get; set; }
public byte[] ProfileImage { get; set; }
}
In Form class
private void StudentsForm_Load(object sender, EventArgs e)
{
BindDataToDataGrid();
}
// Get data from the database into a Student object class
public Collection<Student> GetData()
{
// Initialize collection of students
var collection = new Collection<Student>();
// string sqlQuery = "select * from ProfileTable";
string sqlQuery = "select StudentNo, StudentName, StudentNameChinese, " +
"StudentNameShort, Gender, IdPlaceDesc, MobileNo, MajorDesc, RoomNo," +
"CheckInDate, CheckOutDate, RoomNoBefore, ChangeDate, BirthDate," +
"ProvinceDesc, Email, OtherEmail, Image from ProfileTable";
using (SqlCommand command = new SqlCommand(sqlQuery, connection))
{
connection.Open();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
// Update student variables with values from ProfileTable column
Student student = new Student
{
StudentNumber = (string)reader["StudentNo"],
StudentName = (string)reader["StudentName"],
StudentNameChinese = (string)reader["StudentNameChinese"],
StudentNameShort = (string)reader["StudentNameShort"],
Gender = (string)reader["Gender"],
IdPlaceDescription = (string)reader["IdPlaceDesc"],
MobileNumber = (string)reader["MobileNo"],
Major = (string)reader["MajorDesc"],
RoomNumber = (string)reader["RoomNo"],
CheckInDate = (string)reader["CheckInDate"],
CheckOutDate = (string)reader["CheckOutDate"],
RoomNoBefore = (string)reader["RoomNoBefore"],
ChangeDate = (string)reader["ChangeDate"],
BirthDate = (DateTime)reader["BirthDate"],
Province = (string)reader["ProvinceDesc"],
Email = (string)reader["Email"],
OtherEmail = (string)reader["OtherEmail"],
ProfileImage = (byte[])reader["Image"]
};
// Add student to collection
collection.Add(student);
}
}
connection.Close();
}
return collection;
}
Edit: Below is what I only want to see in my data grid view, thus just using the datatable which is an exact replica of what is in the database is what I'm avoiding
// Bind data from collections to datagrid columns
void BindDataToDataGrid()
{
var students = GetData();
var bind = from student in students
select new
{
// Values are assigned to Datagrid columns
studentNumber = student.StudentNumber,
nameShort = student.StudentNameShort,
room = student.RoomNumber,
gender = student.Gender,
major = student.Major,
number = student.MobileNumber,
email = student.Email,
profile = student.ProfileImage
};
dataGridViewStudents.DataSource = bind;
}