public static DataTable GetByDT(string sql)
{
//
DataTable dt;
DataSet ds=new DataSet();
try
{
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand(sql, conn);
sda.Fill(ds);
dt = ds.Tables[0];
}
catch (DataException e)
{
throw e;
}
conn.Close();
return dt;
}
加载代码
private void frmEdit_Search_Load(object sender, EventArgs e)
{
string sql = "select * from [NewsInfo]";
DataTable dt = DBH.GetByDT(sql);
this.dataGridView1.DataSource = dt;
//this.dataGridView1.DataBind();
//dataGridView1.AutoGenerateColumns = false;
//dataGridView1.Columns[0].DataPropertyName = "ID";
//dataGridView1.Columns[1].DataPropertyName = "Caption";
//dataGridView1.Columns[2].DataPropertyName = "GroupName";
//dataGridView1.Columns[3].DataPropertyName = "CensorStatus";
//dataGridView1.Columns[4].DataPropertyName = "PutTime";
// MessageBox.Show(dt.Rows.Count.ToString());
}