如何在gridview的编辑项模板上从数据库绑定下拉列表 [英] how to bind dropdownlist from database on edit item template of gridview

查看:63
本文介绍了如何在gridview的编辑项模板上从数据库绑定下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我必须在gridview的编辑项目模板中的表格后面的表中将dropdownlist绑定到后面的代码中.我不想使用SqlDataSource.

我使用了以下内容

受保护的void GRVAccess_RowDataBound(对象发送者,GridViewRowEventArgs e)
{

如果(GRVAccess.EditIndex == e.Row.RowIndex)
{
试试
{
数据表dt1;

dt1 = desg.BindDesignation();
如果(dt1.Rows.Count> 0)
{
DropDownList ddldesg =(DropDownList)e.Row.Cells [0] .FindControl("ddlDesg");
ddldesg.DataSource = dt1;
ddldesg.DataTextField =名称";
ddldesg.DataValueField ="DesgId";
ddldesg.DataBind();
//ddldesg.Items.Insert(0,"Select");
}
}
catch(ex ex例外)
{

lblErrorMessage.Visible = true;
lblErrorMessage.Text = ex.Message;
}
}
}

但这表明在ddldesg.DataSource = dt1;
上未将对象引用设置为对象的实例 请帮帮我.

谢谢你
mohd Wasif

Hi All,



I have to bind dropdownlist from a table in edit item template of gridview at code behind .I don''t want to use SqlDataSource .

I used something like below

protected void GRVAccess_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (GRVAccess.EditIndex == e.Row.RowIndex)
{
try
{
DataTable dt1;

dt1 = desg.BindDesignation();
if (dt1.Rows.Count > 0)
{
DropDownList ddldesg = (DropDownList)e.Row.Cells[0].FindControl("ddlDesg");
ddldesg.DataSource = dt1;
ddldesg.DataTextField = "Designation";
ddldesg.DataValueField = "DesgId";
ddldesg.DataBind();
//ddldesg.Items.Insert(0, "Select");
}
}
catch (Exception ex)
{

lblErrorMessage.Visible = true;
lblErrorMessage.Text = ex.Message;
}
}
}

But it''s showing Object reference not set to an instance of an object at ddldesg.DataSource = dt1;
Please help me.

Thanking You
mohd Wasif

推荐答案

http://msdn .microsoft.com/en-us/library/ms178294.aspx [ ^ ]


使用以下代码可实现此目的:

Use following code to achieve this:

<br />
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)<br />
        {<br />
            Control ctrl = e.Row.FindControl("ddlTest");<br />
            if (ctrl != null)<br />
            {<br />
                DropDownList ddl = (DropDownList)ctrl;<br />
                List<Users> ls = GetDropDownData();<br />
                ddl.DataSource = ls;<br />
                ddl.DataTextField = "Name";<br />
                ddl.DataValueField = "Id";<br />
                ddl.DataBind();<br />
            }<br />
        }<br />


<        EditItemTemplate>
                    <asp:DropDownList ID="ddlGender" runat="server" >
                    <asp:ListItem Text="male" Value="male" />
                     <asp:ListItem Text="female" Value="female" />
                     </asp:DropDownList>
                     </EditItemTemplate>





然后使用
<code>
DropDownList DdlGender =(DropDownList)row.FindControl("ddlGender");

字符串Gender = DdlGender.SelectedValue.ToString();

现在您可以使用性别





then use
<code>
DropDownList DdlGender = (DropDownList)row.FindControl("ddlGender");

string Gender = DdlGender.SelectedValue.ToString();

now you can use gender


这篇关于如何在gridview的编辑项模板上从数据库绑定下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆