以编程方式将下拉列表加载到datagrid中 [英] Programmatically load dropdownlist into datagrid

查看:48
本文介绍了以编程方式将下拉列表加载到datagrid中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个数据网格,在不同的行中我需要

以编程方式绑定不同类型的控件并加载

数据进入这些控制。例如,在第一行中我需要将数据绑定到文本框中,而在第二行中我需要将数据绑定到下拉列表...这一切都是取决于

我从数据库中选择的数据。


我不能使用TemplateColumn因为它必须是相同的

类型一栏的控制权。


非常感谢,

Julia

解决方案

Julia,

尝试这样的事情:


< asp:TemplateColumn HeaderText ="酒店类型">

< ItemTemplate>

< asp:PlaceHolder ID =" plc" RUNAT = QUOT;服务器" />

< / ItemTemplate>

< / asp:TemplateColumn>


我不确定是否你正在使用c#或vb.net,以及你绑定的内容,

但假设你在vb.net并绑定到数据集/表格,你的

ItemDatabound事件应该类似于:


Private Sub Grid_ItemDataBound(ByVal sender As Object,ByVal e As

DataGridItemEventArgs)处理grid.ItemDataBound

如果e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType =

ListItemType.AlternatingItem则

Dim plc As PlaceHolder = CType(e.Item。 FindControl(" plc"),PlaceHolder)

如果不是plc什么都没有那么

Dim dr As DataRowView = CType(e.Item.DataItem,DataRowView)

选择Case CInt(dr(type))

案例1

Dim txt作为新TextBox

txt。 Text = CStr(dr(" value"))

plc.Controls.Add(txt)

案例2

Dim ddl如新DropDownList

plc.Controls.Add(ddl)

结束选择

结束如果

结束如果

结束子


如你所见,我们试图找到占位符控件,如果它存在,我们得到

a参考我们正在使用的当前行,我们可以使用

行中的值(这里,我使用了一个神秘的类型行)。列)找出什么类型的

控件进入占位符。


Karl


" Julia胡" < H6 ** @ yahoo.ca>在留言中写道

新闻:a8 **************************** @ phx.gbl ... < blockquote class =post_quotes>
我有一个数据网格,在不同的行中我需要以编程方式绑定不同类型的控件并将数据加载到这些控件中。例如,在第一行中我需要将数据绑定到文本框中,而在第二行中我需要将数据绑定到下拉列表中......这一切都取决于数据的数据我从数据库中选择。

我不能使用TemplateColumn,因为它必须与一列的控件类型相同。

非常感谢,

Julia



Karl,


感谢您的快速回复并感谢您的帮助!

我会尝试并让你知道它是否有效。


Julia

-----原帖-----
朱莉娅,
尝试这样的事情:

< asp:TemplateColumn HeaderText ="酒店类型">
< ItemTemplate>
< asp:PlaceHolder ID =" plc" RUNAT = QUOT;服务器" />
< / ItemTemplate>
< / asp:TemplateColumn>

我不确定你是否使用c#或vb.net,以及你是什么
也是绑定,但假设你在vb.net并绑定到
数据集/表,yourItemDatabound事件应该类似于:

Private Sub Grid_ItemDataBound(ByVal sender As对象,
ByVal和AsDataGridItemEventArgs)处理grid.ItemDataBound
如果e.Item.ItemType = ListItemType.Item OrElse
e.Item.ItemType = ListItemType.AlternatingItem然后
Dim plc As PlaceHolder = CType(e.Item.FindControl
(" plc"),PlaceHolder)如果Not plc什么都没有那么
Dim dr As DataRowView = CType
(e.Item.DataItem,DataRowView )选择Case CInt(dr(type))
案例1
Dim txt As New TextBox
txt.Text = CStr(dr(value))
plc.Controls.Add(txt)
案例2
Dim ddl作为新的DropDownList
plc.Controls.Add(ddl)
结束选择
结束如果结束如果
结束子

正如你所看到的,我们试图找到占位符控件,
如果它存在,我们geta引用我们正在使用的当前行,我们可以使用
therow中的值(这里,我用了一个神话般的类型列)找出
什么类型的控件进入占位符。

Karl

Julia Hu < H6 ** @ yahoo.ca>在消息中写道
新闻:a8 **************************** @ phx.gbl ...


我有一个数据网格,在不同的行中,我需要以编程方式绑定不同类型的控件,并将
加载数据到这些控件中。例如,在第一行我需要将数据绑定到文本框中,而在第二行
我需要将数据绑定到下拉列表...这一切都取决于数据上的
我从数据库中选择。

我不能使用TemplateColumn,因为它必须是一列的
相同类型的控件。

非常感谢,

Julia





Karl,

代码有效。现在我需要在

中检索嵌入占位符

控件的下拉列表中的数据。以下是我的代码执行此操作,但是它

什么都不返回。


plcAI = CType(grid.Items(i).Cells(1).FindControl

(" plcAI"),PlaceHolder)

Dim ddl As DropDownList = CType(plcAI.FindControl

(" ddlAI")), DropDownList)


再次感谢

-----原帖-----
Julia,
尝试这样的事情:

< asp:TemplateColumn HeaderText ="酒店类型">
< ItemTemplate>
< asp:PlaceHolder ID =" plc" RUNAT = QUOT;服务器" />
< / ItemTemplate>
< / asp:TemplateColumn>

我不确定你是否使用c#或vb.net,以及你是什么
也是绑定,但假设你在vb.net并绑定到
数据集/表,yourItemDatabound事件应该类似于:

Private Sub Grid_ItemDataBound(ByVal sender As
Object,ByVal e AsDataGridItemEventArgs)处理grid.ItemDataBound
如果e.Item.ItemType = ListItemType.Item OrElse
e.Item.ItemType = ListItemType.AlternatingItem那么
Dim plc As PlaceHolder = CType(e.Item.FindControl
(" plc"),PlaceHolder)如果Not plc什么都没有那么
Dim dr As DataRowView = CType
(e.Item.DataItem,DataRowView )选择Case CInt(dr(type))
案例1
Dim txt As New TextBox
txt.Text = CStr(dr(value))
plc.Controls.Add(txt)
案例2
Dim ddl作为新的DropDownList
plc.Controls.Add(ddl)
结束选择
结束如果结束如果
结束子

正如你所看到的,我们试图找到占位符控件,
如果它存在,我们geta引用我们正在使用的当前行,我们可以使用
therow中的值(这里,我用了一个神话般的类型如图所示:
将什么类型的控件输入占位符。

Karl

Julia Hu < H6 ** @ yahoo.ca>在消息中写道
新闻:a8 **************************** @ phx.gbl ...


我有一个数据网格,在不同的行中,我需要以编程方式绑定不同类型的控件,并将
加载数据到这些控件中。例如,在第一行
我需要将数据绑定到文本框中,而在第二行
行中我需要将数据绑定到下拉列表...这一切都取决于数据上的
我从数据库中选择。

我不能使用TemplateColumn,因为它必须是一列的
相同类型的控件。

非常感谢,

朱莉娅





Hi,
I have a datagrid, and in different rows I need to
programmatically bind different type of controls and load
data into these controls. For example,in the first row I
need to bind data into a textbox, and in the second row I
need to bind data into a dropdownlist...It all depends on
the data I select from the database.

I cannot use TemplateColumn because it has to be the same
type of control for one column.

Thanks a lot,

Julia

解决方案

Julia,
Try something like this:

<asp:TemplateColumn HeaderText="Hotel Type">
<ItemTemplate>
<asp:PlaceHolder ID="plc" Runat="server" />
</ItemTemplate>
</asp:TemplateColumn>

I''m not sure if you are using c# or vb.net, and what you are binding too,
but assuming you are in vb.net and binding to a dataset/table, your
ItemDatabound event should look something like:

Private Sub Grid_ItemDataBound(ByVal sender As Object, ByVal e As
DataGridItemEventArgs) Handles grid.ItemDataBound
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType =
ListItemType.AlternatingItem Then
Dim plc As PlaceHolder = CType(e.Item.FindControl("plc"), PlaceHolder)
If Not plc Is Nothing Then
Dim dr As DataRowView = CType(e.Item.DataItem, DataRowView)
Select Case CInt(dr("type"))
Case 1
Dim txt As New TextBox
txt.Text = CStr(dr("value"))
plc.Controls.Add(txt)
Case 2
Dim ddl As New DropDownList
plc.Controls.Add(ddl)
End Select
End If
End If
End Sub

As you can see, we try to find the placeholder contorl, if it exists, we get
a reference to the current row we are using, we can use the values in the
row (here, I used a mythical "Type" column) to figure out what type of
control goes into the placeholder.

Karl

"Julia Hu" <h6**@yahoo.ca> wrote in message
news:a8****************************@phx.gbl...

Hi,
I have a datagrid, and in different rows I need to
programmatically bind different type of controls and load
data into these controls. For example,in the first row I
need to bind data into a textbox, and in the second row I
need to bind data into a dropdownlist...It all depends on
the data I select from the database.

I cannot use TemplateColumn because it has to be the same
type of control for one column.

Thanks a lot,

Julia



Karl,

Thank you for your quick response and appreciate your help!
I will try it and let you know if it works.

Julia

-----Original Message-----
Julia,
Try something like this:

<asp:TemplateColumn HeaderText="Hotel Type">
<ItemTemplate>
<asp:PlaceHolder ID="plc" Runat="server" />
</ItemTemplate>
</asp:TemplateColumn>

I''m not sure if you are using c# or vb.net, and what you are binding too,but assuming you are in vb.net and binding to a dataset/table, yourItemDatabound event should look something like:

Private Sub Grid_ItemDataBound(ByVal sender As Object, ByVal e AsDataGridItemEventArgs) Handles grid.ItemDataBound
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType =ListItemType.AlternatingItem Then
Dim plc As PlaceHolder = CType(e.Item.FindControl ("plc"), PlaceHolder) If Not plc Is Nothing Then
Dim dr As DataRowView = CType (e.Item.DataItem, DataRowView) Select Case CInt(dr("type"))
Case 1
Dim txt As New TextBox
txt.Text = CStr(dr("value"))
plc.Controls.Add(txt)
Case 2
Dim ddl As New DropDownList
plc.Controls.Add(ddl)
End Select
End If
End If
End Sub

As you can see, we try to find the placeholder contorl, if it exists, we geta reference to the current row we are using, we can use the values in therow (here, I used a mythical "Type" column) to figure out what type ofcontrol goes into the placeholder.

Karl

"Julia Hu" <h6**@yahoo.ca> wrote in message
news:a8****************************@phx.gbl...

Hi,
I have a datagrid, and in different rows I need to
programmatically bind different type of controls and load data into these controls. For example,in the first row I
need to bind data into a textbox, and in the second row I need to bind data into a dropdownlist...It all depends on the data I select from the database.

I cannot use TemplateColumn because it has to be the same type of control for one column.

Thanks a lot,

Julia


.



Karl,
The code works. And now I need to retrieve the data in
the dropdownlist which embeded in the placeholder
control. The following is my code does this, however it
returns nothing.

plcAI = CType(grid.Items(i).Cells(1).FindControl
("plcAI"), PlaceHolder)
Dim ddl As DropDownList = CType(plcAI.FindControl
("ddlAI"), DropDownList)

thanks again

-----Original Message-----
Julia,
Try something like this:

<asp:TemplateColumn HeaderText="Hotel Type">
<ItemTemplate>
<asp:PlaceHolder ID="plc" Runat="server" />
</ItemTemplate>
</asp:TemplateColumn>

I''m not sure if you are using c# or vb.net, and what you are binding too,but assuming you are in vb.net and binding to a dataset/table, yourItemDatabound event should look something like:

Private Sub Grid_ItemDataBound(ByVal sender As Object, ByVal e AsDataGridItemEventArgs) Handles grid.ItemDataBound
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType =ListItemType.AlternatingItem Then
Dim plc As PlaceHolder = CType(e.Item.FindControl ("plc"), PlaceHolder) If Not plc Is Nothing Then
Dim dr As DataRowView = CType (e.Item.DataItem, DataRowView) Select Case CInt(dr("type"))
Case 1
Dim txt As New TextBox
txt.Text = CStr(dr("value"))
plc.Controls.Add(txt)
Case 2
Dim ddl As New DropDownList
plc.Controls.Add(ddl)
End Select
End If
End If
End Sub

As you can see, we try to find the placeholder contorl, if it exists, we geta reference to the current row we are using, we can use the values in therow (here, I used a mythical "Type" column) to figure out what type ofcontrol goes into the placeholder.

Karl

"Julia Hu" <h6**@yahoo.ca> wrote in message
news:a8****************************@phx.gbl...

Hi,
I have a datagrid, and in different rows I need to
programmatically bind different type of controls and load data into these controls. For example,in the first row I need to bind data into a textbox, and in the second row I need to bind data into a dropdownlist...It all depends on the data I select from the database.

I cannot use TemplateColumn because it has to be the same type of control for one column.

Thanks a lot,

Julia


.



这篇关于以编程方式将下拉列表加载到datagrid中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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