C# DataGridView不显示数据
我使用以下代码在表的C# DataGridView不显示数据,c#,sql,.net,winforms,datagridview,C#,Sql,.net,Winforms,Datagridview,我使用以下代码在表的DataGridView中显示数据。但是DataGridView是空的: namespace WindowsFormsApplication5 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object send
DataGridView
中显示数据。但是DataGridView
是空的:
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show("sdgds");
SqlCommand sCommand;
SqlDataAdapter sAdapter;
SqlCommandBuilder sBuilder;
DataSet sDs;
DataTable sTable;
string sql = "SELECT * FROM mytable";
SqlConnection myConnection = new SqlConnection("user id=test;" +
"password=test;server=MOBILE01;" +
"Trusted_Connection=yes;" +
"database=mydatabase; " +
"connection timeout=30");
myConnection.Open();
sCommand = new SqlCommand(sql, myConnection);
sAdapter = new SqlDataAdapter(sCommand);
sBuilder = new SqlCommandBuilder(sAdapter);
sDs = new DataSet();
sAdapter.Fill(sDs);
sAdapter.Fill(sDs, "mytable");
dataGridView1.ReadOnly = true;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
myConnection.Close();
}
}
}
因为您没有设置属性:
dataGridView1.DataSource = sDs.Tables["mytable"];
因为您没有设置属性:
dataGridView1.DataSource = sDs.Tables["mytable"];
您尚未将
数据源
分配到网格视图
dataGridView1.DataSource = sDs.Tabes[0];
您尚未将
数据源
分配到网格视图
dataGridView1.DataSource = sDs.Tabes[0];
如果您的目标只是填充DataGridView以显示数据,那么您可能应该去掉Dataset、DataAdapter填充例程:
private void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show("sdgds");
SqlCommand sCommand;
SqlCommandBuilder sBuilder;
DataTable sTable;
string sql = "SELECT * FROM mytable";
SqlConnection myConnection = new SqlConnection("user id=test;" +
"password=test;server=MOBILE01;" +
"Trusted_Connection=yes;" +
"database=mydatabase; " +
"connection timeout=30");
myConnection.Open();
sCommand = new SqlCommand(sql, myConnection);
/// here is the change - getting data in data reader and filling datatable directly with it.
var reader = sCommand.ExecuteReader();
sTable = new DataTable();
sTable.Load(reader);
dataGridView1.DataSource = sTable;
dataGridView1.ReadOnly = true;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
myConnection.Close();
}
}
如果您的目标只是填充DataGridView以显示数据,那么您可能应该去掉Dataset、DataAdapter填充例程:
private void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show("sdgds");
SqlCommand sCommand;
SqlCommandBuilder sBuilder;
DataTable sTable;
string sql = "SELECT * FROM mytable";
SqlConnection myConnection = new SqlConnection("user id=test;" +
"password=test;server=MOBILE01;" +
"Trusted_Connection=yes;" +
"database=mydatabase; " +
"connection timeout=30");
myConnection.Open();
sCommand = new SqlCommand(sql, myConnection);
/// here is the change - getting data in data reader and filling datatable directly with it.
var reader = sCommand.ExecuteReader();
sTable = new DataTable();
sTable.Load(reader);
dataGridView1.DataSource = sTable;
dataGridView1.ReadOnly = true;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
myConnection.Close();
}
}
@techno为什么在表单加载时显示消息?需要吗?@techno为什么在表单加载时显示消息?需要吗?