Gridview没有显示数据(当我运行程序时,表单是空白的)

时间:2015-05-30 00:53:57

标签: c#

请帮我解决这个问题。由于某些原因,我无法在gridview中显示数据库中的表。每当我运行程序时,Windows窗体都是空白的(全部都没有)。

Connection Manager类从这里开始:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OCUTutoringSchedule
{
    public class OCUTutoringScheduleDBConn
    {
        public static SqlConnection getStringConn()
        {
            string connectionString = "Data Source=  (LocalDB)\\v11.0;AttachDbFilename="
                                  + "C:\\Users\\Honopac\\Documents\\Visual Studio 2013\\"
                                  + "Projects\\OCUTutoringSchedule\\OCUTutoringSchedule\\"
                                  + "TutoringSchedule.mdf;Integrated Security=True";

        SqlConnection connection = new SqlConnection(connectionString);

        return connection;
    }
}

}

数据访问类从这里开始:

using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OCUTutoringSchedule
{
public class TutorDB
{
    public TutorDB()
    {

    }

    public DataTable displayTutor()
    {
        DataTable dt = new DataTable("TutorsInformation");

        string selectStatement = "SELECT * FROM Employees";
        SqlConnection connString = OCUTutoringScheduleDBConn.getStringConn();
        connString.Open();

        using (SqlCommand cmd = new SqlCommand(selectStatement, connString))
        {
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);          
        }
        return dt;
    }
}    

}

表单加载事件从此处开始:

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;

namespace OCUTutoringSchedule
{
public partial class Tutoring_Schedule_Maintainace : Form
{
    public Tutoring_Schedule_Maintainace()
    {
        InitializeComponent();
    }

    private void Tutoring_Schedule_Maintainace_Load(object sender, EventArgs e)
    { 
        TutorDB tutorDB = new TutorDB();

        DataGridView gv = new DataGridView();
        gv.AutoGenerateColumns = true;
        gv.DataSource = tutorDB.displayTutor();
        gv.Visible = true;

    }
}

}

2 个答案:

答案 0 :(得分:1)

您没有将datagridview添加到表单中。您可以使用设计器轻松完成此操作,而不是自己实例化。

gv.Location = New System.Drawing.Point(10, 55)
gv.Size() = New System.Drawing.Size(130, 95)
this.Controls.Add (gv)

答案 1 :(得分:0)

您需要将gv.Visible添加到true gv,而不是将this.Controls.Add(gv);设置为{{1}}。