我们如何从数据库中检索数据到列表框或gridview中? [英] How can we retrive data from database into listbox or gridview?

查看:70
本文介绍了我们如何从数据库中检索数据到列表框或gridview中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在从数据库检索数据到ListBox时遇到问题.
请给我一个我可以做到的想法.

谢谢.

Hi,

I am having problems in retrieving data from database to ListBox.
Please give me an idea through which i can do it.

Thanks.

推荐答案


试试这个会引导你
Hi,
Try this will guid you
DataClassesDataContext Db = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //retrieve data from Db through linq Ado.net ....etc
            var result = from x in Db.Genders
                         select new { x.gender1, x.test };
            //passing the datasource for control
            ListBox1.DataSource = result;
            // here the the value will dispaly
            ListBox1.DataTextField = "gender1";
            // value field
            ListBox1.DataValueField = "test";
            //bind control
            ListBox1.DataBind();
            //passing datasource
                GridView1.DataSource = result;
                GridView1.DataBind();
        }
    }







<div>
      <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
      <br />
      <br />
      <asp:GridView ID="GridView1" runat="server">
      </asp:GridView>
  </div>


最好的问候
M.mitwalli


Best Regards
M.mitwalli


嗨..

如果要绑定单个记录,则可以按以下方式直接使用列表框

listbox.datasource = bll.getdatafromserver();
listbox.databind();

但是,如果您将数据表绑定到列表bix,它将变成,",如
分隔在每一行中
迪帕克,图沙尔,莫希特

否则可以使用..
http://asp-net-example.blogspot.in/2009/03/how-to-data-bind-listbox.html [ http://stackoverflow.com/questions/1143639/binding-multiple- fields-to-listbox-in-asp-net [ ^ ]

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listbox.aspx [ ^ ]

希望对您有帮助

谢谢
Hi..

If you want to bind single record then it is fine the list box directly in following way

listbox.datasource=bll.getdatafromserver();
listbox.databind();

But if you bind data table to list bix it will become "," seprated in each row like

deepak,tushar,mohit

otherwise you can use..
http://asp-net-example.blogspot.in/2009/03/how-to-data-bind-listbox.html[^]

http://stackoverflow.com/questions/1143639/binding-multiple-fields-to-listbox-in-asp-net[^]

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listbox.aspx[^]

Hope it will help you

Thanks,


到目前为止,您尝试了什么?您遇到什么错误?

如果您需要有关如何绑定列表框的教程
]
What have you tried so far? What errors are you getting?

If you need tutorials on how to binding list box
Check this[^]


这篇关于我们如何从数据库中检索数据到列表框或gridview中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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