【问题标题】:datagridview is showing empty rows in windows formdatagridview 在 Windows 窗体中显示空行
【发布时间】:2016-09-02 00:20:40
【问题描述】:

在我的数据库中有一个名为 student 的表。该表有 6 行,但我的 windows 窗体的 datagridview 显示 6 个空行。我尝试了几种方法,包括studentdataGridView.AllowUserToAddRows = true/false;,但没有任何效果。它显示空行。

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 * FROM 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;
        }
    }
}

【问题讨论】:

  • 1)students student2 = new students(); 移动到循环中。 2) 与我们分享students 的代码。 3) 使用SqlDataAdapter 加载数据更简单,然后您可以根据需要将数据调整为List&lt;T&gt;
  • 您是否验证可以连接到sql数据库
  • 是的,mysql数据库连接没问题
  • 调试后也计算结果
  • 还要确保您的 Students 类使用 properties 只是字段绑定方式不同

标签: c# winforms datagridview datatable


【解决方案1】:

您发布的上述代码运行良好没有任何问题。

您的学生属性 (POCO) 类应该是:

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; }
}

你的表架构应该是:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-30
    • 1970-01-01
    • 1970-01-01
    • 2016-08-28
    • 2015-04-18
    • 1970-01-01
    • 2012-12-20
    相关资源
    最近更新 更多