积极答复者
C#DataGridView控件绑定数据,但没显示数据的问题

问题
-
数据源是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());
}
}
答案
-
你好,
检查一下你的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.- 已标记为答案 ZhouJun Cai 2019年1月3日 8:24
全部回复
-
你好,
根据你的代码,我做了简单的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. -
你好,
检查一下你的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.- 已标记为答案 ZhouJun Cai 2019年1月3日 8:24