我有以下代碼來顯示錶中的DataGridView
中的數據。但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();
}
}
}
@techno爲什麼會在表單加載時顯示消息?需要嗎? –
答案有什麼問題?在這裏提供一些學習曲線! –
我沒有downvote ... – techno