Datagridview不会显示值。显示正确的行数,但列中不显示任何文本

时间:2014-02-03 10:33:11

标签: c# winforms datagridview

我的问题是,当数据库从数据库加载到我的datagridview时,datagridview将不会显示值/信息。显示正确的行数,但列中不显示任何文本。

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

 namespace Movie_Memorizer2
 {
    class clsDB
    {
    static SqlConnection conn = new SqlConnection(@"Data Source");


    public static DataTable franDB(string str)
    {
        DataTable dt = new DataTable();

        try
        {
            if (!(conn.State == ConnectionState.Open)) conn.Open(); 

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = str;

            SqlDataAdapter da = new SqlDataAdapter(); 
            da.SelectCommand = cmd;

            da.Fill(dt);

            da = null; 
            cmd = null;
            conn.Close(); 

        }

        catch (SqlException ex)
        {
            string felet = ex.Message; //mer kod här om man vill
        }

        return dt;
    }

class frmMovie - 这就是我称之为方法" franDB"在上面的clsDb类中

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Windows.Forms;

 namespace Movie_Memorizer2
 {

 public partial class frmMovie : FormBase
 {
    public frmMovie()
    {
        InitializeComponent();
    }

    private void FormMovie_Load(object sender, EventArgs e)
    {            
        dgvMovies.DataSource = clsDB.franDB("SELECT * FROM Movie ORDER BY name");

    }

1 个答案:

答案 0 :(得分:0)

设置DataSource后,添加:

dvgMovies.DataBind()