2011-09-28 58 views
0

我想在GridView中显示员工详细信息。我的客户希望他应该能够更新记录而无需点击编辑按钮来更新。因此,我将所有内容都保存在GridView的ItemTemplate字段中。如何在ItemTemplate中更新GridView

我的数据库表: 的Emp(EMPNO,ENAME,萨尔,DEPTNO) 部门(DEPTNO,DEPTNAME,地点)

因此,我写了一个SP获得的数据:EMPNO,ENAME,萨尔, DeptName

在这里,我想在DropDownList中显示DeptName字段。这里只有我面临这个问题。 DropDownList不填充适当的DeptName值,但是第一个字段。

我的代码背后,是象下面这样:

public partial class GvwEmployee : System.Web.UI.Page 
{ 
    SqlConnection objConn = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["STERIAConnectionString"].ConnectionString); 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!Page.IsPostBack) 
     { 
      GridView1.DataSource = EmployeeList; 
      GridView1.DataBind(); 
     } 
    } 

    private List<Employee> EmployeeList 
    { 
     get{ 
       List<Employee> employeeList = new List<Employee>(); 
       SqlCommand objCmd = new SqlCommand("usp_GetEmpDetails", objConn); 
       objCmd.CommandType = CommandType.StoredProcedure; 
       objConn.Open(); 
       SqlDataReader objDR = objCmd.ExecuteReader(); 
       while (objDR.Read()) 
       { 
        Employee employee = new Employee(); 
        employee.EmpNo = Convert.ToInt32(objDR["EmpNo"]); 
        employee.EName = Convert.ToString(objDR["EName"]); 
        employee.Salary = Convert.ToDouble(objDR["Salary"]); 
        employee.DeptNo = Convert.ToInt32(objDR["DeptNo"]); 
        employee.DeptName = Convert.ToString(objDR["DeptName"]); 
        employeeList.Add(employee); 
       } 
       objDR.Close(); 
       objConn.Close(); 
       return employeeList; 
     } 
    } 

    private List<Department> DeptList 
    { 
     get 
     { 
      List<Department> deptList = new List<Department>(); 
      SqlCommand objCmd = new SqlCommand("usp_GetDeptDetails", objConn); 
      objCmd.CommandType = CommandType.StoredProcedure; 
      objConn.Open(); 
      SqlDataReader objDR = objCmd.ExecuteReader(); 
      while (objDR.Read()) 
      { 
       Department dept = new Department(); 
       dept.DeptNo = Convert.ToInt32(objDR["DEPTNO"]); 
       dept.DName = Convert.ToString(objDR["DNAME"]); 
       dept.Location = Convert.ToString(objDR["Location"]); 
       deptList.Add(dept); 
      } 
      objDR.Close(); 
      objConn.Close(); 
      return deptList; 
     } 
    } 

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      DropDownList ddlDeptName = e.Row.FindControl("ddlDeptName") as DropDownList; 
      if (ddlDeptName != null) 
      { 
       ddlDeptName.DataSource = DeptList; 
       ddlDeptName.DataBind(); 
       ddlDeptName.SelectedValue = GridView1.DataKeys[e.Row.RowIndex].Value.ToString(); 
      } 
     } 
     if (e.Row.RowType == DataControlRowType.Footer) 
     { 
     } 
    } 
} 

谁能告诉哪里出了问题,为什么DEPTNAME DropDownList会不会与它应有的价值填补?

回答

0

我得到了解决象下面这样:

在GridView1_RowDataBound事件的方法,我已经写了下面的代码行映射的DropDownList与数据从后端了。

//ddlDeptName.SelectedValue = GridView1.DataKeys [e.Row.RowIndex] .Value.ToString();

现在,我已经替换为以下代码行上面的行,因此,问题得到解决:

ddlDeptName.SelectedValue =((员工)e.Row.DataItem).DeptNo.ToString() ;

这已经解决了我的问题。

0

尝试这种方式

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     var index = Convert.ToInt32(e.RowIndex.ToString()); 
     GridViewRow row = GridView1.Rows[index]; 
     var ddlDeptName = row.FindControl("ddlDeptName") as ddlDeptName; 
     if (ddlDeptName != null) 
     { 
      ddlDeptName.DataSource = DeptList; 
      ddlDeptName.DataTextField = "DName"; 
      ddlDeptName.DataValueField = "DeptNo"; 
      ddlDeptName.DataBind(); 
      var item = new ListItem("Select Department", ""); 
      ddlDeptName.Items.Insert(0, item); 
     } 
    }  
} 
相关问题