none
C#DataGridView控件绑定数据,但没显示数据的问题 RRS feed

  • 问题

  • 数据源是DataTable类型,且不为空,但控件就是不显示数据,这是怎么回事?

    部分代码如下:

    /// <summary>
            /// 重载构造函数
            /// </summary>
            /// <param name="dataTable"></param>
            public InsertChartForm(DataTable dataTable):this()
            {
                m_dataTable = dataTable;
                this.gridControl.DataSource = null;
                devChartControl.DtChart = null;
                dataGridView1.DataSource = null;
                
            }
            private void InsertChartForm_Load(object sender, EventArgs e)
            {
                try
                {
                    //var m_result = new DataTable();
                    //m_result.Columns.Add(new DataColumn("名称", typeof(string)));
                    //m_result.Columns.Add(new DataColumn("值", typeof(double)));
                    //m_result.Columns.Add(new DataColumn("单位", typeof(string)));
                    
                    gridControl.RefreshDataSource();
                    this.gridControl.DataSource = m_dataTable;

                    devChartControl.DtChart = m_dataTable;
                    dataGridView1.Refresh();
                    dataGridView1.DataSource = m_dataTable;
                   
                }
                catch (Exception er)
                {
                    Debug.Write(er.ToString());
                }
            }


    2019年1月3日 2:05

答案

  • 你好,

    检查一下你的Datagridview里面一些设置,比如说列名是不是和你的m_table 里面一致。

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年1月3日 7:46
    版主

全部回复

  • 你好,

    根据你的代码,我做了简单的Demo, datagridview 是可以显示的,你可以分享一下简单的,完整的,可以重现问题的代码吗? 可以上次到OneDrive, 然后把共享链接发到这里。

    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Diagnostics;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class Form5 : Form
        {
    
            DataTable m_dataTable = new DataTable();
            public Form5()
            {
                InitializeComponent();
    
                string connstr = @"Data Source=related connstr";
                using (var conn = new SqlConnection(connstr))
                {
                    string sqlString = "select * from post";
                    using (var command = new SqlCommand(sqlString,conn))
                    {
                        SqlDataAdapter adapter = new SqlDataAdapter(command);
                        adapter.Fill(m_dataTable);
                    }
                }
    
                //this.gridControl.DataSource = null;
                //devChartControl.DtChart = null;
                dataGridView1.DataSource = null;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                // Displays an OpenFileDialog so the user can select a Cursor.  
                OpenFileDialog openFileDialog1 = new OpenFileDialog();
                openFileDialog1.Filter = "Cursor Files|*.cur";
                openFileDialog1.Title = "Select a Cursor File";
    
                if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
    
                }
    
            }
    
            
    
            private void Form5_Load(object sender, EventArgs e)
            {
                try
                {
                    //var m_result = new DataTable();
                    //m_result.Columns.Add(new DataColumn("名称", typeof(string)));
                    //m_result.Columns.Add(new DataColumn("值", typeof(double)));
                    //m_result.Columns.Add(new DataColumn("单位", typeof(string)));
    
                    //gridControl.RefreshDataSource();
                    //this.gridControl.DataSource = m_dataTable;
    
                    //devChartControl.DtChart = m_dataTable;
                    dataGridView1.Refresh();
                    dataGridView1.DataSource = m_dataTable;
    
                }
                catch (Exception er)
                {
                    Debug.Write(er.ToString());
                }
            }
        }
    }
    

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年1月3日 5:47
    版主
  • 完整代码不好放出来,这里涉及公司的东西,我也有试过新建一个Demo,发现是能正常显示数据的,但放到项目里就是不行,还有,假如我绑定的数据源是代码里的m_result 它又是可以显示的,即 dataGridView1.DataSource = m_result 。真的非常奇怪....
    2019年1月3日 7:13
  • 你好,

    检查一下你的Datagridview里面一些设置,比如说列名是不是和你的m_table 里面一致。

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年1月3日 7:46
    版主
  • 你好,刚刚我试了新的方法:先在视图界面添加列,再设置每列的名称,然后就可以了,我想应该就是你所说的问题,但之前我使用的时候都是设置控件的DataSource就行了,不必设置其他东西,这里却会出现这种问题,真是奇怪。不过问题也算是解决了,谢谢!
    2019年1月3日 7:59