【发布时间】:2020-07-20 09:14:42
【问题描述】:
初学者!我试图仅将对象中的某些数据显示到数据网格视图中,但在 Winforms 中运行后我什么也没有得到。有什么方法可以成功实现这个或任何替代方案?
我试图遵循模板here,但尝试不成功
// 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; }
}
在表单类中
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;
}
编辑:下面是我只想在我的数据网格视图中看到的内容,因此我要避免只使用与数据库中的内容完全相同的数据表 p>
// 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;
}
【问题讨论】:
-
StudentsForm_Load 有时为时过早。尝试显示..
-
试过了,但没有改变。早些时候直接从数据库绑定,一切正常。所以问题可能在于学生对象与 datagridview 的绑定
-
你需要安装和使用Dapper
标签: c# winforms datagridview