我在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>
{
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>
中的cell[0]去掉
this.DropDownList1.DataSource = new UserTypeManager().SelectUserType();
this.DropDownList1.DataValueField = "TypeId";
this.DropDownList1.DataTextField = "UserTypeName";
this.DropDownList1.DataBind();
SqlDataReader sdr = dataOperate.getRow(sql);
既然这个sql每次都是读取相同的数据行,就可以用 HttpContext.Current.Cache将数据缓存,这样就没必要每次都去访问数据库
e.Row已经表示是当前行了,你是在当前行里面找某个下拉控件
这是前台代码
<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事件里 添加什么代码。望大神指导
给你一个呆滞的..
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了.