我有这个代码,它有效。但它需要永远。好吧,也许是40秒,但这绝对是在线应用程序。
protected void DataGrid_Open_RowDataBound(object sender, GridViewRowEventArgs e)
{
OracleConnection conn = GetConnection();
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView drv = e.Row.DataItem as DataRowView;
//**********************************************
//************* Auditor Names ****************
//**********************************************
OracleCommand cmd4 = new OracleCommand();
cmd4.CommandType = CommandType.StoredProcedure;
cmd4.CommandText = "CSTAPP_O.CST_AMR_AUDITORNAMES";
cmd4.Connection = conn;
cmd4.Parameters.Add("cursor_", OracleType.Cursor).Direction = ParameterDirection.Output;
var SearchAdapter4 = new OracleDataAdapter(cmd4);
var ds4 = new DataTable();
SearchAdapter4.Fill(ds4);
DataView view = new DataView(ds4);
DataTable distinctValues = new DataTable();
distinctValues = view.ToTable(true, "Auditor_Name", "AuditorID");
DropDownList ddl = e.Row.FindControl("EI_Open_AuditorName") as DropDownList;
ddl.DataSource = distinctValues;
ddl.DataTextField = "Auditor_Name";
ddl.DataValueField = "AuditorID";
ddl.DataBind();
ddl.SelectedValue = drv["Auditor_Name"].ToString();
//**********************************************
//***************** Status *******************
//**********************************************
OracleCommand cmd5 = new OracleCommand();
cmd5.CommandType = CommandType.StoredProcedure;
cmd5.CommandText = "CSTAPP_O.CST_AMR_AUDITSTATUS";
cmd5.Connection = conn;
cmd5.Parameters.Add("cursor_", OracleType.Cursor).Direction = ParameterDirection.Output;
var SearchAdapter5 = new OracleDataAdapter(cmd5);
var ds5 = new DataTable();
SearchAdapter5.Fill(ds5);
DataView view5 = new DataView(ds5);
DataTable distinctValues5 = new DataTable();
distinctValues5 = view5.ToTable(true, "StatusName", "StatusID");
// DataRowView drv = e.Row.DataItem as DataRowView;
DropDownList ddl5 = e.Row.FindControl("EI_Open_AuditStatus") as DropDownList;
ddl5.DataSource = distinctValues5;
ddl5.DataTextField = "StatusName";
ddl5.DataValueField = "StatusID";
ddl5.DataBind();
ddl5.SelectedValue = drv["Overall_Audit_Status"].ToString();
}
}
catch (Exception ex)
{
}
}
有更有效的方法吗?我无法相信为GridView中的每条记录重新运行存储过程是填充DropDownList的最佳方法,而且我有2个DropDownLists来填充。
答案 0 :(得分:1)
您可以使用DataTable
临时存储数据和方法将数据导入DataTable并重复使用,这样您就不会在每个DataBound项目上访问数据库。
DataTable dt;
protected void DataGrid_Open_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the current row is a datarow
if (e.Row.RowType == DataControlRowType.DataRow)
{
//check if the datatable already exists, if not get it from the LoadDataFromDB method
if (dt == null)
{
dt = LoadDataFromDB();
}
DropDownList ddl = e.Row.FindControl("EI_Open_AuditorName") as DropDownList;
ddl.DataSource = dt;
ddl.DataTextField = "Auditor_Name";
ddl.DataValueField = "AuditorID";
ddl.DataBind();
}
}
//method for loading data into a datatable and return it as such
public DataTable LoadDataFromDB()
{
string query = "CSTAPP_O.CST_AMR_AUDITORNAMES";
DataTable dt = new DataTable();
using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlDataAdapter adapter = new SqlDataAdapter(query, connection))
{
try
{
adapter.Fill(dt);
}
catch
{
}
}
return dt;
}