gridview中的dropdownlist在数据绑定后默认值显示数据库中的数据报错
我的gridview中有两个dropdownlist,其中一个可以实现绑定,并让数据库的数据显示成默认值,但是另外一个在绑定后,设置默认值的时候就报未将对象设置引用到设置...
我的gridview中有两个dropdownlist,其中一个可以实现绑定,并让数据库的数据显示成默认值,但是另外一个在绑定后,设置默认值的时候就报未将对象设置引用到设置的实例,具体的代码如下,卸载rowdatabind事件中的
#region 讲解员下拉菜单数据绑定
Model.Users usermodel = new Model.Users();
if ((DropDownList)e.Row.FindControl("JJY") != null)
{
DropDownList Userddl = (DropDownList)e.Row.FindControl("JJY");
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataSet UsersDs = userbll.GetAllUsers();
Userddl.DataSource = UsersDs.Tables[0];
Userddl.DataTextField = "UserName";
Userddl.DataValueField = "UserID";
Userddl.DataBind();
Userddl.SelectedValue = ((HiddenField)e.Row.FindControl("HidJJY")).Value;//未将对象引用设置到对象的实例。这里是主要报错的
}
}
else
{
DropDownList Userddl = (DropDownList)e.Row.FindControl("JJY");
Userddl.Items.Add("请选择讲解员");//这里也报错
int UserddlIndex= Userddl.Items.IndexOf(Userddl.Items.FindByText("请选择讲解员"));
string text=Userddl.Items[0].Text;
string Value=Userddl.Items[0].Value;
Userddl.Items[UserddlIndex].Text=text;
Userddl.Items[UserddlIndex].Value=Value;
}
另一个不报错的空间的方法是这样写的
if ((DropDownList)e.Row.FindControl("CgAddress") != null)
{
DropDownList dropDownList = (DropDownList)e.Row.FindControl("CgAddress");
if (e.Row.RowType == DataControlRowType.DataRow)
{
绑定的方法和上面的一样,这里写不下了
dropDownList.SelectedValue = ((HiddenField)e.Row.FindControl("HidAddress")).Value;
}
} 展开
#region 讲解员下拉菜单数据绑定
Model.Users usermodel = new Model.Users();
if ((DropDownList)e.Row.FindControl("JJY") != null)
{
DropDownList Userddl = (DropDownList)e.Row.FindControl("JJY");
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataSet UsersDs = userbll.GetAllUsers();
Userddl.DataSource = UsersDs.Tables[0];
Userddl.DataTextField = "UserName";
Userddl.DataValueField = "UserID";
Userddl.DataBind();
Userddl.SelectedValue = ((HiddenField)e.Row.FindControl("HidJJY")).Value;//未将对象引用设置到对象的实例。这里是主要报错的
}
}
else
{
DropDownList Userddl = (DropDownList)e.Row.FindControl("JJY");
Userddl.Items.Add("请选择讲解员");//这里也报错
int UserddlIndex= Userddl.Items.IndexOf(Userddl.Items.FindByText("请选择讲解员"));
string text=Userddl.Items[0].Text;
string Value=Userddl.Items[0].Value;
Userddl.Items[UserddlIndex].Text=text;
Userddl.Items[UserddlIndex].Value=Value;
}
另一个不报错的空间的方法是这样写的
if ((DropDownList)e.Row.FindControl("CgAddress") != null)
{
DropDownList dropDownList = (DropDownList)e.Row.FindControl("CgAddress");
if (e.Row.RowType == DataControlRowType.DataRow)
{
绑定的方法和上面的一样,这里写不下了
dropDownList.SelectedValue = ((HiddenField)e.Row.FindControl("HidAddress")).Value;
}
} 展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励10(财富值+成长值)+提问者悬赏40(财富值+成长值)
1个回答
展开全部
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex == GridView1.EditIndex)
{
DataRowView rowItem = (DataRowView)e.Row.DataItem;
DropDownList clsName = (DropDownList)e.Row.FindControl("uClassName");
if (rowItem["ClassName"] != DBNull.Value)
{
clsName.Items.FindByText(rowItem["ClassName"].ToString()).Selected = true;
}
}
}
}
<EditItemTemplate>
<asp:DropDownList ID="uClassName" runat="server">
....
</asp:DropDownList>
</EditItemTemplate>
我自定义了2个DataTable你将他换成你的就行了
1: .cs
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView drv = e.Row.DataItem as DataRowView;
int rid = Convert.ToInt32(((DataRowView)e.Row.DataItem)["Rid"]);
DropDownList dd = (DropDownList)e.Row.FindControl("DropDownList2");
DataTable table = dd.DataSource as DataTable;
dd.SelectedIndex = table.Rows.IndexOf(table.Rows.Find(rid));
}
}
public DataTable GridSource
{
get
{
DataTable table = new DataTable();
table.Columns.Add("ID", typeof(int));
table.Columns.Add("Rid", typeof(int));
for (int i = 1; i < 3; i++)
{
DataRow row = table.NewRow();
row["ID"] = i;
row["Rid"] = i;
table.Rows.Add(row);
}
return table;
}
}
public DataTable DropSource
{
get
{
DataTable table = new DataTable();
DataColumn col=new DataColumn("Rid", typeof(int));
table.Columns.Add(col);
table.PrimaryKey = new DataColumn[] { col };
table.Columns.Add("Rname", typeof(string));
for (int i = 1; i < 3; i++)
{
DataRow row = table.NewRow();
row["Rid"] = i;
row["Rname"] = i+"aa";
table.Rows.Add(row);
}
return table;
}
}
2:页面
<asp:GridView ID="GridView1" DataSource="<%# GridSource %>" runat="server" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="ID" />
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="DropDownList2" DataSource="<%#DropSource %>" DataTextField="Rname" DataValueField="Rid" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
DropSource换成你的RoleDownList,就是角色表
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex == GridView1.EditIndex)
{
DataRowView rowItem = (DataRowView)e.Row.DataItem;
DropDownList clsName = (DropDownList)e.Row.FindControl("uClassName");
if (rowItem["ClassName"] != DBNull.Value)
{
clsName.Items.FindByText(rowItem["ClassName"].ToString()).Selected = true;
}
}
}
}
<EditItemTemplate>
<asp:DropDownList ID="uClassName" runat="server">
....
</asp:DropDownList>
</EditItemTemplate>
我自定义了2个DataTable你将他换成你的就行了
1: .cs
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView drv = e.Row.DataItem as DataRowView;
int rid = Convert.ToInt32(((DataRowView)e.Row.DataItem)["Rid"]);
DropDownList dd = (DropDownList)e.Row.FindControl("DropDownList2");
DataTable table = dd.DataSource as DataTable;
dd.SelectedIndex = table.Rows.IndexOf(table.Rows.Find(rid));
}
}
public DataTable GridSource
{
get
{
DataTable table = new DataTable();
table.Columns.Add("ID", typeof(int));
table.Columns.Add("Rid", typeof(int));
for (int i = 1; i < 3; i++)
{
DataRow row = table.NewRow();
row["ID"] = i;
row["Rid"] = i;
table.Rows.Add(row);
}
return table;
}
}
public DataTable DropSource
{
get
{
DataTable table = new DataTable();
DataColumn col=new DataColumn("Rid", typeof(int));
table.Columns.Add(col);
table.PrimaryKey = new DataColumn[] { col };
table.Columns.Add("Rname", typeof(string));
for (int i = 1; i < 3; i++)
{
DataRow row = table.NewRow();
row["Rid"] = i;
row["Rname"] = i+"aa";
table.Rows.Add(row);
}
return table;
}
}
2:页面
<asp:GridView ID="GridView1" DataSource="<%# GridSource %>" runat="server" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="ID" />
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="DropDownList2" DataSource="<%#DropSource %>" DataTextField="Rname" DataValueField="Rid" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
DropSource换成你的RoleDownList,就是角色表
追问
不能从我的代码中改么?我的代码中已经实现了一半,但是还是有问题,所以希望能用我的代码改过
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询