一般分为两步:
①从数据库取值,
public DataTable GetTable()
{
DataTable Table = new DataTable();//目标table;
DataSet ds = new DataSet();//数据集;
string SQL = "select .... ";
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["Connector"].ToString());//Connector是一个键,需要在配置文件里面保存好数据库的相关信息,包括:即<appSettings> <add key="Connector" value="Server=...;DataBase=...;Uid=...;Pwd=..."/>
SqlDataAdapter Ad = new SqlDataAdapter(SQL, conn);
try
{
conn.Open();
Adapter.Fill(ds, "GetTable_name");
Table = ds.Tables["GetTable_name"];
}
catch
{
}
finally
{
conn.Close();
conn.Dispose();
}
return Table;
}
②装到datagrid里面
public void LoadGrid()
{
DataTable TempTable = new DataTable();
TempTable = GetTable();
Session["TempTable"]=TempTable;
DataGrid.DataSource =TempTable;
DataGrid.DataBind();
}