【问题标题】:How to make updatable GridView in ItemTemplate如何在 ItemTemplate 中制作可更新的 GridView
【发布时间】:2011-09-28 07:21:48
【问题描述】:

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

我的数据库表是: Emp(EmpNo, EName, Sal, DeptNo) 部门(部门编号、部门名称、位置)

因此,我编写了一个 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 没有填写正确的值?

【问题讨论】:

    标签: asp.net gridview


    【解决方案1】:

    试试这个方法

    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);
            }
        }     
    }
    

    【讨论】:

      【解决方案2】:

      我得到了如下解决方案:

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

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

      现在,我用下面的代码行替换了上面的行,问题就解决了:

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

      这解决了我的问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多