我做到最后一部分。不确定如何或应该如何添加到代码中。
public String DisplaySheetList()
{
DataTable dt = new DataTable();
GridView gv = new GridView();
string sqlStatement1 = "SELECT cs.SheetId AS sheet_id, ltm.LocationType AS location_type, cs.PalletNo AS palletNo, cs.period AS period,cs.syncDate AS syncDate,cs.syncStatus AS syncStatus "
+ "FROM CountSheet cs JOIN LocationTypeMaster ltm ON ltm.LocationId = cs.LocationId " +
" ORDER BY 1 DESC";
SqlCommand sqlCmd1 = new SqlCommand(sqlStatement1, conn);
SqlDataAdapter sqlDa1 = new SqlDataAdapter(sqlCmd1);
sqlDa1.Fill(dt);
//What should i put here
gv.DataBind();
}
我创建了表和视图,得到了查询并将其填入表中(如果我没有错)。它是完成还是我应该做的事情,以确保它在gridview中,所以我可以显示出来。
任何帮助都会很好,谢谢你们
答案 0 :(得分:1)
Just Put:
gv.DataSource=dt;
代替我应该放在什么将数据表数据绑定到gridview
答案 1 :(得分:1)
因此,在数据表中填写结果后
使dt
成为gridview数据源。
//put it in the gridview datasource.
gv.DataSource = dt;
您可以参考此MSDN page来设置gridview数据源
完成您的代码:
public String DisplaySheetList()
{
DataTable dt = new DataTable();
GridView gv = new GridView();
string sqlStatement1 = "SELECT cs.SheetId AS sheet_id, ltm.LocationType AS location_type, cs.PalletNo AS palletNo, cs.period AS period,cs.syncDate AS syncDate,cs.syncStatus AS syncStatus "
+ "FROM CountSheet cs JOIN LocationTypeMaster ltm ON ltm.LocationId = cs.LocationId " +
" ORDER BY 1 DESC";
SqlCommand sqlCmd1 = new SqlCommand(sqlStatement1, conn);
SqlDataAdapter sqlDa1 = new SqlDataAdapter(sqlCmd1);
sqlDa1.Fill(dt);
//put it in the gridview datasource.
gv.DataSource = dt;
gv.DataBind();
}