【发布时间】:2014-09-09 20:25:02
【问题描述】:
我的 ASP.NET Web 表单中有一个 gridview 控件。我过去曾使用相同的代码在 Windows 窗体应用程序中使用数据填充网格视图。
这是我的代码:
var sql = new SQL_Statements();
var stringSql = "select col1, col2, col3, col4 from table1 where col5=" + stringPO_NUM;
var sql_ds = sql.SelectFromDB(stringSql);
int DataIndex;
if (sql_ds.Tables["CurData"].Rows.Count > 0)
for (DataIndex = 0; DataIndex <= sql_ds.Tables["CurData"].Rows.Count - 1; DataIndex++)
{
sql_ds.Tables["CurData"].Rows[DataIndex]["col4"].ToString();
//Fill the datagridview with the data that was just pulled from SQL_Statements
GV_POI.DataSource = sql_ds;
GV_POI.DataMember = "CurData";
}
这是 SelectFromDB 的代码:
public DataSet SelectFromDB(String Sql)
{
var functionReturnValue = default(DataSet);
var Class_Connection = new SQL_Connection();
Class_Connection.cnn.Close(); //Let's close the connection, just in case it is open
Class_Connection.cnn.Open();
var myDataAdaptor = new SqlDataAdapter(Sql, Class_Connection.cnn);
var myDataset = new DataSet();
try
{
myDataAdaptor.SelectCommand.Connection = Class_Connection.cnn;
myDataAdaptor.SelectCommand.CommandText = Sql;
myDataAdaptor.SelectCommand.CommandType = CommandType.Text;
myDataAdaptor.Fill(myDataset, "CurData");
functionReturnValue = myDataset;
}
catch (Exception ex)
{
Console.WriteLine(ex);
Class_Connection.cnn.Close();
}
return functionReturnValue;
Class_Connection.cnn.Close();
}
这是 ASPX 页面中 GridView 的代码:
<asp:GridView ID="GV_POI" runat="server" AutoGenerateColumns="False">
</asp:GridView>
我不确定我做错了什么。当页面加载时,gridview 是空白的。我检查了调试器中的代码,并且代码正在触发。有人可以告诉我我做错了什么吗?
【问题讨论】:
-
col5 是字符类型还是数字类型?无论如何,您不应该使用参数来设置该值吗?
-
@rontornambe col5 是数字
-
你检查过回发吗?
if(!Page.IsPostBack) { CallBindFunction(); } -
@HassanNisar No. 代码在 Page_LoadComplete 中触发,我在调试器中检查了
-
check
@Lorin's回答我认为你错过了GV_POI.DataBind();
标签: c# asp.net gridview webforms sqldatasource