if (!(txtfilepathandname.Text.ToString() == ""))
            {
                DataSet ds = createds(txtfilepathandname.Text.ToString());
                DataGridView dgvexcel =new DataGridView();
                dgvexcel.DataSource = ds.Tables[0].DefaultView;
                dgvexcel.Refresh();
                MessageBox.Show("导入成功");
                txtfilepathandname.Clear();
                txtfilepathandname.Focus();
                MessageBox.Show(ds.Tables[0].Rows.Count.ToString());
            }
 自定义函数createds,读取从excel中查询的数据,返回查询生成的dataset,经测试,可以返回,               MessageBox.Show(ds.Tables[0].Rows.Count.ToString());结果为3请指点,为什么datagridview dgvexcel不显示数据呢?
谢谢

解决方案 »

  1.   

    你是否AutoGenerateColumns=false,而你有没有添加列,是否设置了列的对应字段
      

  2.   

    我把AutoGenerateColumns设为true也不能显示啊?            if (!(txtfilepathandname.Text.ToString() == ""))
                {
                    DataSet ds = createds(txtfilepathandname.Text.ToString());
                    DataGridView dgvexcel =new DataGridView();
                    dgvexcel.AutoGenerateColumns = true;
                    dgvexcel.DataSource = ds.Tables[0].DefaultView;
                    dgvexcel.Refresh();
                    MessageBox.Show("导入成功");
                    txtfilepathandname.Clear();
                    txtfilepathandname.Focus();
                    MessageBox.Show(ds.Tables[0].Rows.Count.ToString());
                }
      

  3.   

      DataSet ds = createds(txtfilepathandname.Text.ToString());
                    DataGridView dgvexcel =new DataGridView();
                    dgvexcel.AutoGenerateColumns = true;
                    dgvexcel.DataSource = ds.Tables[0].DefaultView;
                   
    吧后面都删掉试试。
    看是不是后面的代码触发了。
      

  4.   

    this.controls.add(dgvexcel );
    是不是这个?
      

  5.   

    dataGridView没添加到窗体上,当中改成:
    DataGridView dgvexcel =new DataGridView();
    this.Controls.Add(dgvexcel);