读书人

dataGridView数据不显示

发布时间: 2012-07-15 20:20:06 作者: rapoo

dataGridView数据不显示,请指教
winform 程序

C# code
        public void GeiView()        {            SqlConnection con = null;            string ConnectionString = "Data Source=.;Integrated Security=SSPI;Initial Catalog=PGWEI";            con = new SqlConnection(ConnectionString);            try            {                con.Open();                String sql = "SELECT 客户号,膘厚,级别,重量,头数,类型,日期,时间,操作人 FROM DATAWEI ORDER BY ID DESC";//还有一个ID不需要显示所以没有查                SqlDataAdapter sa = new SqlDataAdapter(sql, con);                DataSet ds = new DataSet();                sa.Fill(ds, "DATAWEI");                wg.dataGridView1.DataSource = ds.Tables["DATAWEI"];                SqlCommandBuilder sqlCmdBuilder = new SqlCommandBuilder(sa);            }            catch            {                MessageBox.Show("获取data失败");            }            finally             {                con.Close();            }        }


数据显示不到dataGridView控件上面去 dataGridView是其他窗体上的
不知道控件还需要设置什么吗,public属性可编辑什么的都设置了

[解决办法]
wg.dataGridView1 中列有没创建好,如果没有在 DataSource 赋值前加上
wg.dataGridView1.AutoGenerateColumns = true;
如果已经创建了就将列的 DataPropertyName 属性输入列名
[解决办法]
那就在 dataGridView1 中把列都创建好,每个列的 DataPropertyName 属性都输入数据库的列名
[解决办法]
“ 数据显示不到dataGridView控件上面去 dataGridView是其他窗体上的” 不同窗体上的控件一般不允许调用,如果要用,需要将两个form之间联系起来。

方法:
Form1里:
Form f2 =new Form();
Form1 =(Form1)this.owner;

Form2里:
From1 f1=new From();
this.AddOwnerForm(f1);

[解决办法]
这样试试是否显示
C# code
public void GeiView(){    SqlConnection con = null;    string ConnectionString = "Data Source=.;Integrated Security=SSPI;Initial Catalog=PGWEI";    con = new SqlConnection(ConnectionString);    try    {        con.Open();        String sql = "SELECT 客户号,膘厚,级别,重量,头数,类型,日期,时间,操作人 FROM DATAWEI ORDER BY ID DESC";        SqlDataAdapter sa = new SqlDataAdapter(sql, con);        DataTable dtbl = new DataTable();        sa.Fill(dtbl);        sa.Dispose();        MessageBox.Show(dtbl.Rows.Count.ToString());        wg.dataGridView1.AutoGenerateColumns = false;        wg.dataGridView1.Columns.Clear();        var array = new DataGridViewColumn[9];        for (int i = 0; i < array.Length; i++)        {            array[i] = new DataGridViewTextBoxColumn();            array[i].Width = 100;        }        array[0].DataPropertyName = array[0].HeaderText = "客户号";        array[1].DataPropertyName = array[1].HeaderText = "膘厚";        array[2].DataPropertyName = array[2].HeaderText = "级别";        array[3].DataPropertyName = array[3].HeaderText = "重量";        array[4].DataPropertyName = array[4].HeaderText = "头数";        array[5].DataPropertyName = array[5].HeaderText = "类型";        array[6].DataPropertyName = array[6].HeaderText = "日期";        array[7].DataPropertyName = array[7].HeaderText = "时间";        array[8].DataPropertyName = array[8].HeaderText = "操作人";        wg.dataGridView1.Columns.AddRange(array);        wg.dataGridView1.DataSource = dtbl;        SqlCommandBuilder sqlCmdBuilder = new SqlCommandBuilder(sa);    }    catch    {        MessageBox.Show("获取data失败");    }    finally    {        con.Close();    }}
[解决办法]
断点可以看到ds里的数据么
------解决方案--------------------


探讨

引用:

这样试试是否显示
C# code
public void GeiView()
{
SqlConnection con = null;
string ConnectionString = "Data Source=.;Integrated Security=SSPI;Initial Catalog=PGWEI";
con = new SqlConnect……

[解决办法]
控件在别的窗体...是不是datagridview控件没传好?
[解决办法]
你需要最后refresh一下Datagridview

读书人网 >C#

热点推荐