在GridView中从数据库绑定Dropdownlist [英] Bind Dropdownlist from db in gridview

查看:56
本文介绍了在GridView中从数据库绑定Dropdownlist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想从gridview中的db绑定一个dropdownlist.但是它没有被束缚.我的代码是这样,但是它不起作用.我只是将其绑定到gridview行更新命令.

Hi,

I want to bind a dropdownlist from db in a gridview. But its not getting bind. My code is this but it is not working. I am just binding this on gridview row updating command.

        SqlConnection con = new SqlConnection(constr);
        da = new SqlDataAdapter("sp_BindCourse", con);
        da.Fill(ds,"temp");
        if (ds.Tables[0].Rows.Count > 0)
        {
            ddlCourse.DataSource = ds;
            ddlCourse.DataTextField = "course";
            ddlCourse.DataValueField = "course";
            ddlCourse.DataBind();
            ddlCourse.Items.Insert(0, "Course");
}



请帮帮我.

[edit]已添加代码块,将我的内容作为纯文本..."选项已禁用-OriginalGriff [/edit]
[edit2]注释已更改,以反映处理我的内容..."选项上的新文本-OriginalGriff [/edit2]



Please help me.

[edit]Code block added, "Treat my content as plain text..." option disabled - OriginalGriff[/edit]
[edit2]Comment changed to reflect new text on "Treat my content..." option - OriginalGriff[/edit2]

推荐答案

尝试在GridView中使用代码" s DataBound事件
Try using the code in GridView''s DataBound event


string connString =ConfigurationManager.

ConnectionStrings["connectionstring"].ToString()

SqlConnection con = new SqlConnection(connString);

con.Open();

SqlCommand cmd = new SqlCommand();

cmd.Connection = con;

cmd.CommandText = "spChGetNames";

cmd.CommandType = CommandType.StoredProcedure;

DataSet ds = new DataSet();

SqlDataAdapter da = new SqlDataAdapter();

da.SelectCommand = cmd;

da.Fill(ds);

DropDownList1.DataSource = ds;

DropDownList1.DataTextField = "Name";

DropDownList1.DataValueField = "ID";

DropDownList1.DataBind();


这篇关于在GridView中从数据库绑定Dropdownlist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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