我在gridview里放了个下拉菜单 现在从数据库里读取一行数据绑定在gridview里 将其中的一列数据绑定在dropdownlist里 参照网上的说法 谢啦个rowdatabound事件 不知道哪里出错了 下拉菜单并没有绑定上数据protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {                
                    string sql = "select * from EP_TblMailbox where SendToDepartment = '维修部'";
                    SqlDataReader sdr = dataOperate.getRow(sql);
                    sdr.Read();
                    if (sdr["IsVisted"].ToString() =="true")
                    {
                        DropDownList ddl = (DropDownList)e.Row.Cells[0].FindControl("DropDownList2");
                        ddl.SelectedValue = "是";
                    }
                    if (sdr["IsVisted"].ToString() == "false")
                    {
                        DropDownList ddl = (DropDownList)e.Row.Cells[0].FindControl("DropDownList2");
                        ddl.SelectedValue="否";
                    }                
            }            
        }
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            onrowdatabound="GridView1_RowDataBound">
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="编号" />
                <asp:BoundField DataField="SenderName" HeaderText="发送人" />
                <asp:BoundField DataField="SendDepartment" HeaderText="发送部门" />
                <asp:BoundField DataField="SendToDepartment" HeaderText="接受部门" />
                <asp:BoundField DataField="ReportID" HeaderText="报告编号" />
                <asp:BoundField DataField="Type" HeaderText="报告类型" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:DropDownList ID="DropDownList2" runat="server">
                            <asp:ListItem>true</asp:ListItem>
                            <asp:ListItem>false</asp:ListItem>
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

解决方案 »

  1.   

    DropDownList ddl = (DropDownList)e.Row.Cells[0].FindControl("DropDownList2"); 
    中的cell[0]去掉
      

  2.   

    //绑定下拉框
                    this.DropDownList1.DataSource = new UserTypeManager().SelectUserType();
                    this.DropDownList1.DataValueField = "TypeId";
                    this.DropDownList1.DataTextField = "UserTypeName";
                    this.DropDownList1.DataBind();
      

  3.   

    在GridView1_RowUpdating事件上附给DropDownList1值
      

  4.   

    还要说一句Lz的代码需要优化一下 ,每次遍历GridView行的时候都要连接数据库,浪费资源,可以把 string sql = "select * from EP_TblMailbox where SendToDepartment = '维修部'";
        SqlDataReader sdr = dataOperate.getRow(sql);
    既然这个sql每次都是读取相同的数据行,就可以用 HttpContext.Current.Cache将数据缓存,这样就没必要每次都去访问数据库
      

  5.   

                DropDownList ddlXXX = e.Row.FindControl("下拉控件名") as DropDownList;
    e.Row已经表示是当前行了,你是在当前行里面找某个下拉控件
      

  6.   

    你的列值应该错了,cells[0]指的 是编号那一列啊!
      

  7.   

    额 去掉cell【0】已没有成功绑定啊 我是新手 求大神详细讲下 
    这是前台代码
     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                onrowdatabound="GridView1_RowDataBound">
                <Columns>
                    <asp:BoundField DataField="ID" HeaderText="编号" />
                    <asp:BoundField DataField="SenderName" HeaderText="发送人" />
                    <asp:BoundField DataField="SendDepartment" HeaderText="发送部门" />
                    <asp:BoundField DataField="SendToDepartment" HeaderText="接受部门" />
                    <asp:BoundField DataField="ReportID" HeaderText="报告编号" />
                    <asp:BoundField DataField="Type" HeaderText="报告类型" />
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True">
                                <asp:ListItem>是</asp:ListItem>
                                <asp:ListItem>否</asp:ListItem>
                            </asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
    这是我平常绑定gridview的代码
    protected void GridView1databind()
            {
                string sql = "select * from EP_TblMailbox where SendToDepartment = '维修部'";
                GridView1.DataSource = data.getDataSet(sql);
                GridView1.DataKeyNames = new string[] { "ID" };
                GridView1.DataBind();
            }protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {                
                        string sql = "select * from EP_TblMailbox where SendToDepartment = '维修部'";
                        SqlDataReader sdr = dataOperate.getRow(sql);
                        sdr.Read();
                        if (sdr["IsVisted"].ToString() =="true")
                        {
                            DropDownList ddl = (DropDownList)e.Row.Cells[0].FindControl("DropDownList2");
                            ddl.SelectedValue = "是";
                        }
                        if (sdr["IsVisted"].ToString() == "false")
                        {
                            DropDownList ddl = (DropDownList)e.Row.Cells[0].FindControl("DropDownList2");
                            ddl.SelectedValue="否";
                        }                
                }            
            }
    [EP_TblMailbox]表名称
    表属性:
    [SenderName]
          ,[SendDepartment]
          ,[SendToDepartment]
          ,[ReportID]
          ,[Type]
          ,[IsVisted] bool
          ,[Isolved] bool
    大家讲的很多 有点没明白 我本意是 用GridView1databind 给gridview进行绑定 其中IsVisted,Isolved这两个属性用dropdownlist来显示。rowdatabound的语句我测试了 确实从数据库里取得了数据 但是却没有成功绑定。那么我现在需要在在GridView1databind  或者rowdatabound事件里 添加什么代码。望大神指导
      

  8.   

    既然这样处理不好,你也可以换一个处理方式.
    给你一个呆滞的..
    for(i=0;i<=Grid.Rows.Count;i++)
    {
       string sql ="";
       sqldatareader sdr=dataoperate.getrows(sql); 
       sdr.read();
       if(sdr["IsVisted"].ToString()=="true")
       {
        
       }else
       {   }
       sdr.close();
    }记得要释放Read句柄.for循环 放在绑定Grid之后就ok了.
      

  9.   

    循环里面是 (Type)Grid.Rows[i].FindControl("id")其他的跟你的一样.