首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

gridview 的dropdownlist绑定数据有关问题

2013-05-02 
gridview 的dropdownlist绑定数据问题我在gridview里放了个下拉菜单 现在从数据库里读取一行数据绑定在gri

gridview 的dropdownlist绑定数据问题
我在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>


[解决办法]


既然这样处理不好,你也可以换一个处理方式.
给你一个呆滞的..
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了.

热点排行