我们可以有一个列表框在GridView的一个领域,也可以使一个GridView内可编辑字段..? [英] Can we have a listbox as a field in Gridview, and also can be make editable fields inside a Gridview..?
问题描述
我要创建为医生,其中药物是自动从数据库截取的prescription,然后医生可以写入将要采取自己的药物的类型,效力和频率。
I have to create a prescription for doctor, where medicines are automatically taken from database, and doctors can then write the type, potency and frequency of the medicine to be taken themselves.
所以我想检索的GridView
中的药品,一个列表框里面
键入领域。这可能吗。
So I want to retrieve medicines within a Gridview
, inside a Listbox
type field. Is this possible.
其次我也想,如果药是不是在数据库present,医生可以编辑列表框
添加它。最后,我想一些直接编辑字段,如文本框
的效力,频率和类型。
Secondly I also want that if a medicine is not present in the database, doctors can edit a Listbox
to add it. Finally I want some directly editable fields like Textboxes
for potency, frequency and type.
推荐答案
当然,你可以做到这一点。您只需将一个文本框
或的ListBox
或任何你选择的GridView内部控制。你还可以用一些值初始化它们,或者,你可以在其中插入值。
Of course, you can do that. you just place a TextBox
or ListBox
or whatever control of your choice inside GridView. And you can initialize them with some value or, you can insert value in it.
这是你需要的全部,就是找到在GridView里面的控制,因为它是一个数据中继器控制,所以假设你将一个文本框在GridView里面,你有5个项目,然后5列表束缚,这样的文本框将被渲染,所以你需要能够准确地找到您所需的文本框。
All that you are required to is to find those controls inside the GridView, as it is a data-repeater control, so suppose you place one textbox inside the GridView, and you bound it with a List having 5 items, then 5 such textboxes will be rendered, so you need to be able to exactly locate your required textBox.
下面我给你一个超级简单的例子。
Below am giving you a super simple example.
你的GridView的标记:
your GridView markup:
<asp:GridView ID="Review_grid" runat="server" AllowPaging="True" PageSize="10"
AutoGenerateColumns="False" onrowdatabound="Review_grid_RowDataBound"
>
<Columns>
<asp:TemplateField HeaderText="ListBox">
<ItemTemplate>
<asp:ListBox ID="lstBox" runat="server"></asp:ListBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TextBox">
<ItemTemplate>
<asp:TextBox ID="txtBox" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
您codebehind code。
your codebehind code.
///create datasource for your GridView.
public void BindGrid()
{
DataTable db = new DataTable();
db.Columns.Add("col1");
db.Columns.Add("col2");
db.Columns.Add("col3");
db.Rows.Add("1", "2", "3");
db.Rows.Add("1", "2", "3");
db.Rows.Add("1", "2", "3");
Review_grid.DataSource = db;
Review_grid.DataBind();
}
在的RowDataBound
GridView控件
的事件陷阱控制,将它们与一些值初始化:
trap controls in RowDataBound
event of GridView
, to initialize them with some value:
protected void Review_grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//find ListBox
ListBox lst = (ListBox)e.Row.FindControl("lstBox");
lst.Items.Add(new ListItem("item1"));
lst.Items.Add(new ListItem("item2"));
lst.Items.Add(new ListItem("item3"));
//find textBox
TextBox txt = (TextBox)e.Row.FindControl("txtBox");
txt.Text = "test";
}
}
致电 BindGrid()
在的Page_Load
页面。
您需要探索更多关于的GridView 以及其他< A HREF =http://www.asp.net/web-forms/overview/aspnet-data-controls相对=nofollow> dataControls 。
You need to explore more about GridView and Other dataControls.
这篇关于我们可以有一个列表框在GridView的一个领域,也可以使一个GridView内可编辑字段..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!