C#中的DataGridView并未从SQL Server数据库中检索所有记录

发布于 2025-02-05 16:14:22 字数 835 浏览 3 评论 0原文

我有一个用于我们的学生记录的Visual Studio项目,并且记录来自我们的在线注册应用程序。然后数据库保存它。但是问题是,当我尝试使用另一个应用程序对其进行检测时,我的DataGridView并未从数据库中检索所有记录。换句话说,我们的在线应用程序在线收集数据,而其他应用程序用于显示其处理。

private void DisplayOnlineApplications(List<StudentInformationOnlineModel> Applicants)
{
    dataGridView1.Rows.Clear();

    foreach (var applicant in Applicants)
    {
        string gender = applicant.Gender.Substring(0, 1);
        dataGridView1.Rows.Add(applicant.ApplicationID, applicant.StudentStatus, applicant.LRN, applicant.StudentName, gender, applicant.EmailAddress, applicant.MobileNo, applicant.EducationLevel, applicant.CourseStrand, applicant.YearLevel, applicant.ApplicationDate.ToString("MM-dd-yyyy hh:mm tt"));
        dataGridView1.Rows[dataGridView1.Rows.Count - 1].Tag = applicant;
    }
}

I have a visual studio project for our student records and the records are coming from our online registration app. Then the database saves it. But the problem is my datagridview is not retrieving all the records from the database when I'm trying to retrive it using my other app. In other words our online app gathers data online and the other app is used to displays it for processing.

private void DisplayOnlineApplications(List<StudentInformationOnlineModel> Applicants)
{
    dataGridView1.Rows.Clear();

    foreach (var applicant in Applicants)
    {
        string gender = applicant.Gender.Substring(0, 1);
        dataGridView1.Rows.Add(applicant.ApplicationID, applicant.StudentStatus, applicant.LRN, applicant.StudentName, gender, applicant.EmailAddress, applicant.MobileNo, applicant.EducationLevel, applicant.CourseStrand, applicant.YearLevel, applicant.ApplicationDate.ToString("MM-dd-yyyy hh:mm tt"));
        dataGridView1.Rows[dataGridView1.Rows.Count - 1].Tag = applicant;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

节枝 2025-02-12 16:14:22

您的代码需要一些改进。您无需手动添加行。简单地做。

private void DisplayOnlineApplications(List<StudentInformationOnlineModel> Applicants)
{
    dataGridView1.DataSource = Applicants;
}

对于性别字段,您可以处理datagridview.cellformatting事件,

private void DataGridView1_CellFormatting(
    object sender, DataGridViewCellFormattingEventArgs e)
{
    var dataPropertyName = dataGridView1.Columns[e.ColumnIndex].DataPropertyName;
    if (dataPropertyName == nameof(StudentInformationOnlineModel.Gender))
    {
        e.Value = (e.Value as string).Substring(0, 1);
    }
}

如果您需要行中的对象,则可以执行(在index 0) )

var item = dataGridView1.Rows[0].DataBoundItem;
var studentInformationOnlineModel = item as StudentInformationOnlineModel;

Your code needs some improvements. You don't need to manually add rows. Simply do.

private void DisplayOnlineApplications(List<StudentInformationOnlineModel> Applicants)
{
    dataGridView1.DataSource = Applicants;
}

For Gender field you can handle DataGridView.CellFormatting event

private void DataGridView1_CellFormatting(
    object sender, DataGridViewCellFormattingEventArgs e)
{
    var dataPropertyName = dataGridView1.Columns[e.ColumnIndex].DataPropertyName;
    if (dataPropertyName == nameof(StudentInformationOnlineModel.Gender))
    {
        e.Value = (e.Value as string).Substring(0, 1);
    }
}

If you need to get your object in row then you can do (at index 0 for example)

var item = dataGridView1.Rows[0].DataBoundItem;
var studentInformationOnlineModel = item as StudentInformationOnlineModel;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文