SqlDataAdapter dataAdapter = new SqlDataAdapter();
DataTable dataTable = new DataTable();
private void GetData()
{
try
{
string ConnectionString = "server=PP123456\\SQLEXPRESS;database=Northwind;uid=sa;pwd=123";
SqlConnection myCon = new SqlConnection(ConnectionString);
myCon.Open();
SqlCommand selectCommand = new SqlCommand("SELECT top 10 CustomerId,CompanyName FROM Customers", myCon);
this.dataAdapter = new SqlDataAdapter();
dataAdapter.SelectCommand = selectCommand;
SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(dataAdapter);
this.dataTable.Clear();
dataAdapter.Fill(this .dataTable);
}
catch (SqlException e)
{
MessageBox.Show("查询结果出错了:" + e.Message);
}
}
private void BindData2dgv()
{
DataTable dt = this.GetData(); this.dataGridView1.DataSource = dt;
}
private void Form1_Load(object sender, EventArgs e)
{
this.BindData2dgv(); }
运行程序提示无法将类型void转换为System.Data.DataTable程序哪里错了
请大家帮帮我
DataTable dataTable = new DataTable();
private void GetData()
{
try
{
string ConnectionString = "server=PP123456\\SQLEXPRESS;database=Northwind;uid=sa;pwd=123";
SqlConnection myCon = new SqlConnection(ConnectionString);
myCon.Open();
SqlCommand selectCommand = new SqlCommand("SELECT top 10 CustomerId,CompanyName FROM Customers", myCon);
this.dataAdapter = new SqlDataAdapter();
dataAdapter.SelectCommand = selectCommand;
SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(dataAdapter);
this.dataTable.Clear();
dataAdapter.Fill(this .dataTable);
}
catch (SqlException e)
{
MessageBox.Show("查询结果出错了:" + e.Message);
}
}
private void BindData2dgv()
{
DataTable dt = this.GetData(); this.dataGridView1.DataSource = dt;
}
private void Form1_Load(object sender, EventArgs e)
{
this.BindData2dgv(); }
运行程序提示无法将类型void转换为System.Data.DataTable程序哪里错了
请大家帮帮我
private void BindData2dgv()
{
this.GetData(); this.dataGridView1.DataSource = this.dataTable;
}