SqlConnection cnn = new SqlConnection("Data Source=.;Initial Catalog=SSMS;Integrated Security=SSPI");
                        SqlCommand cmd3 = new SqlCommand("select * from Score where SID = '" + toolStripTextBox1.Text.Trim() + "'and CID='" + toolStripTextBox2.Text.Trim() + "'", cnn);
                        cnn.Open();
                        SqlDataAdapter da = new SqlDataAdapter(cmd3);
                        DataTable dt = new DataTable();
                        da.Fill(dt);
                        this.dataGridView1.DataSource = dt;
                        this.toolStripTextBox3.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString();
                        this.toolStripTextBox4.Text = this.dataGridView1.CurrentRow.Cells[3].Value.ToString();
为什么dataGridView1中没有显示数据啊?

解决方案 »

  1.   

    this.dataGridView1.displayMember=dt.table[0].tostring();
      

  2.   

    DataSource不是=那个吧
    BindingSource dataSrc = new BindingSource();
    dataSrc.DataSource = dt;
    然后指向这个
    dataSrc
      

  3.   

    加上
             this.GridView1.DataBind();
      

  4.   

    谢谢二楼但是dataGridView有displayMember吗?
    错误 1、 “System.Windows.Forms.DataGridView”并不包含“displayMember”的定义、
      

  5.   

    谢谢 
    错误 “System.Windows.Forms.DataGridView”并不包含“DataBind”的定义
      

  6.   

    this.dataGridView1.DataSource = dt; 在这行设置断点,看dt里有东西么!
      

  7.   

    using (cn = new SqlConnection("Data Source=.;Initial Catalog=SSMS;Integrated Security=SSPI"))
                        {
                            using (cmd = new SqlCommand("select * from Student where SID='" + toolStripTextBox1.Text.Trim() + "'", cn))
                            {
                                da = new SqlDataAdapter(cmd);
                                dt = new DataTable();
                                da.Fill(dt);
                                this.dataGridView1.DataSource = dt;
                                this.toolStripTextBox1.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
                                this.textBox1.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
                                this.textBox8.Text = this.dataGridView1.CurrentRow.Cells[3].Value.ToString();
    我这个可以啊
      

  8.   

    “System.Windows.Forms.DataGridView”并不包含“DataBind”的定义 
      

  9.   

     SqlConnection cnn = new SqlConnection("Data Source=.;Initial Catalog=SSMS;Integrated Security=SSPI");
                            SqlCommand cmd3 = new SqlCommand("select * from Score where SID = '" + toolStripTextBox1.Text.Trim() + "'and CID='" + toolStripTextBox2.Text.Trim() + "'", cnn);
                            cnn.Open();
                            SqlDataAdapter da = new SqlDataAdapter(cmd3);
                            DataTable dt = new DataTable();
                            da.Fill(dt);
                            this.dataGridView1.DataSource = dt;
                            
                            this.toolStripTextBox3.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString();
                            this.toolStripTextBox4.Text = this.dataGridView1.CurrentRow.Cells[3].Value.ToString();
    又能解决问题的啊??????
      

  10.   

    在winform 那要加个BindingSource吧
    SqlConnection cnn = new SqlConnection("Data Source=.;Initial Catalog=SSMS;Integrated Security=SSPI"); 
                            SqlCommand cmd3 = new SqlCommand("select * from Score where SID = '" + toolStripTextBox1.Text.Trim() + "'and CID='" + toolStripTextBox2.Text.Trim() + "'", cnn); 
                            cnn.Open(); 
                            SqlDataAdapter da = new SqlDataAdapter(cmd3); 
                            DataTable dt = new DataTable(); 
                            da.Fill(dt); 
                            bindingSource1.DataSource = dt;
                            this.dataGridView1.DataSource = bindingSource1;

    看的MSDN上的
      

  11.   


    多次一举,直接this.dataGridView1.DataSource = dt就完了
      

  12.   

    我昨天晚上才试了,你这样根本就不可以啊
    MSDN上的例子也是要那个bindingSource的
    不信可以去看
    我昨天晚上才看了
    http://msdn.microsoft.com/zh-cn/library/system.windows.forms.datagridview.datasource.aspx
    自己看吧
      

  13.   

                SqlConnection conn = new SqlConnection("server=.;database=21cspmain;uid=sa;pwd=123;");
                SqlCommand cmd3 = new SqlCommand("select * from news where classid = '" + toolStripTextBox1.Text.Trim() + "'and newsid='" + toolStripTextBox2.Text.Trim() + "'", conn);
                conn.Open();
                SqlDataAdapter da = new SqlDataAdapter(cmd3);
                DataTable dt = new DataTable();
                da.Fill(dt);
                this.dataGridView1.DataSource = dt;            //SqlDataAdapter da = new SqlDataAdapter("select * from News where classid='329'", conn);
                this.toolStripTextBox3.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString();
                this.toolStripTextBox4.Text = this.dataGridView1.CurrentRow.Cells[4].Value.ToString();
    运行过了,没有问题,除了第一句我是用sa登录的,第二句改了表名和自段名,其余完全一样。你试试你的查询语句能查出结果吗