sallytan_2010

在GridView中绑定下拉列表(查询和编辑)

问题:用GridView显示学生信息,学生所在学院用下拉列表显示,编辑时可对所在学院进行编辑。

(概括:在GridView中嵌入下拉列表,查询状态时下拉列表显示对应的列表项,编辑时可重新选择列表项,更新后列表项相应改变。)

如下图所示

图一 查询状态

图二 编辑状态

 

Student表

 

学院表

 

前台代码:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>学生信息</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"  Width="50%"
            onrowcancelingedit="GridView1_RowCancelingEdit"
            onrowdatabound="GridView1_RowDataBound" onrowediting="GridView1_RowEditing"
            onrowupdating="GridView1_RowUpdating" DataKeyNames="stuID">
        <Columns>
            <asp:BoundField DataField="stuName" HeaderText="姓名" ReadOnly="true" />
        <asp:TemplateField HeaderText="学院">
        <ItemTemplate>
            <asp:DropDownList ID="DropDownList1" runat="server">
            </asp:DropDownList>
        </ItemTemplate>
        </asp:TemplateField>
            <asp:CommandField ShowEditButton="True" />
        </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

 

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Grid_Drop : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridView1();
        }
    }

    //DataOperate为数据库操作函数
    DataOperate sqlHelper = new DataOperate();

    /// <summary>
    /// 绑定GridView1
    /// </summary>
    protected void BindGridView1()
    {
        string sql = "select * from student";
        DataSet ds = sqlHelper.GetDataSet(sql);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            string sqlCollege = "select cID from Student where cID=" + GridView1.DataKeys[i].ToString();
            string cid = ds.Tables[0].Rows[i]["cid"].ToString();
            ((DropDownList)GridView1.Rows[i].Cells[1].FindControl("DropDownList1")).SelectedValue = cid;
        }
    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindGridView1();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if ((DropDownList)e.Row.FindControl("DropDownList1")!=null)
        {
            DropDownList ddlCollege = (DropDownList)e.Row.FindControl("DropDownList1");
            string sql = "select * from College";
            DataSet ds = sqlHelper.GetDataSet(sql);
            ddlCollege.DataSource = ds;
            ddlCollege.DataTextField = "cName";
            ddlCollege.DataValueField = "cID";
            ddlCollege.DataBind();
        }
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindGridView1();
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string cid=((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList1")).SelectedValue.ToString();
        string stuID = GridView1.DataKeys[e.RowIndex].Value.ToString();
        string cmdText = "Update student set cid=" + cid + " where stuID=" + stuID;
        sqlHelper.ExecCmd(cmdText);
        GridView1.EditIndex = -1;
        BindGridView1();       
    }

   
}

posted on 2010-06-03 09:40  sallytan_2010  阅读(2699)  评论(1编辑  收藏  举报

导航