我想将数据绑定到下拉列表并将其放入数据网格 [英] i want to bind data to a dropdownlist and put it in a datagrid

查看:129
本文介绍了我想将数据绑定到下拉列表并将其放入数据网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想自动从表格中的字段中获取值并将其放入dropdonlist中.我已经能够很好地做到这一点,但是我的问题是将以前做过的同样的事情放入数据网格控件中.当我没有将其放在数据网格中并且可以正常工作时,这就是我的代码.



strsql ="select * from sch_name =''"&的主题lblschool.text&''"
strcomm ="server =(local); database = osr; uid = godwin; pwd = godesky;"
data2 =新的sqldataadapter(strsql,strcomm)
data2.fill(display3,"subjects")
drpsubject3.datasource = display.tables("subjects").defaultview
drpsubject3.datatextfield ="sbj_name"
drpsubject3.datavaluefield ="sbj_name"
drpsubject3.databind()


请我就如何将其放入数据网格方面寻求帮助.

Hello, i want to automatically take values from a field in my table and put it in a dropdonlist. I have been able to do this very well but my problem is putting the same thing i have doing before into a datagrid control. This is my code when i have not put it in my datagrid and it work.



strsql="select * from subjects where sch_name=''"& lblschool.text &"''"
strcomm="server=(local);database=osr;uid=godwin;pwd=godesky;"
data2=new sqldataadapter (strsql,strcomm)
data2.fill(display3,"subjects")
drpsubject3.datasource=display.tables("subjects").defaultview
drpsubject3.datatextfield="sbj_name"
drpsubject3.datavaluefield="sbj_name"
drpsubject3.databind()


Please i need help on this on how to put it in a datagrid.

推荐答案

写道:

strsql ="select * from sch_name ="''& lblschool.text&''"

strsql="select * from subjects where sch_name=''"& lblschool.text &"''"



如果您使用数字ID进行搜索,则数据库的性能会更好.理想情况下,您的学校名称应在带有ID的表格中,您可以在其他地方使用.这也意味着您不重复数据,并且很容易更改学校名称.

我看到您在这里使用标签,但是如果您这样构建SQL以便用户可以输入文本,则您的数据库将不安全,并且很容易被删除或损坏.这称为SQL注入.

我不确定为什么您发布了有效的代码.我可以看到它会起作用.您破坏了什么代码?您所需要做的就是将数据源设置为SQL请求中的表.我不确定默认情况下datagrid是否会生成列.一个gridview可以.如果没有数据网格,则可能必须在ASPX中定义模板以定义如何显示数据,这与在放置列表中设置文本和值字段类似.网格显示许多值,没有值和文本字段,aspx中有一个模板,用于定义要显示的所有数据及其显示方式.当然,您仍然需要进行数据绑定.

我建议您购买一本基本的ASP.NET书籍并阅读,因为它们都深入介绍了这些内容.



Your database will perform better if you use numeric ids for your searches. Your school names would ideally be in a table, with ids, which you use elsewhere. This also means you don''t duplicate data, and it''s easy to change a school name.

I see you''re using a label here, but if you build SQL like this where the user can enter the text, your database will not be secure, and can easily be deleted, or corrupted. This is called SQL Injection.

I am not sure why you posted your code that works. I can see it will work. What is your code that is broken ? All you need to do, is set your datasource to be the table from your SQL request. I am not sure if a datagrid generates columns by default. A gridview does. If a datagrid does not, you may well have to define templates in your ASPX to define how the data will be shown, which is analagous to your setting the text and value field in your drop list. As a grid shows many values, there is no value and text field, there''s a template in the aspx that defines all of the data to show, and how it is shown. Of course, you still need to databind as well.

I would suggest you buy a basic ASP.NET book and read it, as they all cover this stuff in depth.


这篇关于我想将数据绑定到下拉列表并将其放入数据网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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