如何在ItemTemplate中创建可更新的GridView

时间:2011-09-28 07:21:49

标签: asp.net gridview

我想在GridView中显示员工详细信息。我的客户希望他能够更新记录而无需单击“编辑”按钮进行更新。因此,我将所有内容保存在GridView的ItemTemplate字段中。

我的数据库表是: Emp(EmpNo,EName,Sal,DeptNo) 部门(DeptNo,DeptName,Location)

因此,我写了一个sp来获取数据:EmpNo,EName,Sal,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没有填充其正确的值?

2 个答案:

答案 0 :(得分: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);
        }
    }     
}

答案 1 :(得分:0)

我得到了如下解决方案:

在GridView1_RowDataBound事件方法中,我编写了以下代码行来映射DropDownList和从后端获取的数据。

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

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

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

这解决了我的问题。