databinding for dropdown list in the ItemTemplate Column

databinding for dropdown list in the ItemTemplate Column

Post by Michell » Wed, 11 Jun 2003 01:34:17



If I put a dropdown list in the ItemTemplate Column, can I bind the dropdown
list with the record in a lookup table?

For example, assuming I have a Direction lookup table with 4 records, i..e.
East, West, North, South. I also have a Trip table, which is displayed on
the page using datagrid. The datagrid has a Direction dropdown list in
ItemTemplate Column. As existing records in Trip table have the Direction
field entered, so when I do database query, I want the Direction dropdown
list bound to the Direction lookup table with the proper Direction field
selected. The selection is based on the Direction field in Trip table.

Is this do-able? Any idea? and is there any web site/link/snippet for
reference in this issue?

Thanks a lot.

 
 
 

databinding for dropdown list in the ItemTemplate Column

Post by Rajesh Tiwar » Wed, 11 Jun 2003 02:00:06


yes,its possible.

try something similar as the code below to find out if it can be of some
help.

specifically you need to call a function OnItemDataBound event of the
datagrid and in that function u can populate ur combo from database table
and set something selected.

code example

<script language="C#" runat=server>
SqlConnection con;
DataView dv;
void Page_Load(Object sender,EventArgs e)
{
con=new SqlConnection("Server=rajesh;database=test;uid=sa;pwd=sa");
if(!IsPostBack)
BindGrid();

Quote:}

public void BindGrid()
{
----------------

Quote:}

public void FuncBound(Object Sender,DataGridItemEventArgs e)
{
 ListItemType itemType=e.Item.ItemType;
 if(itemType == ListItemType.EditItem)
 {
     DropDownList myList = (DropDownList)(e.Item.FindControl("TxtName"));
     DataRowView myRow = (DataRowView)(e.Item.DataItem);
     String myRole= myRow["au_name"].ToString();
     myList.DataTextField = "au_name";
            myList.DataValueField = "au_name";
     myList.DataSource = dv;
     myList.DataBind();
     myList.Items.FindByValue(myRole).Selected = true;
     }
Quote:}

</script>
<form id=frm1 runat=server>
<asp:datagrid id=grd runat=server  OnItemDataBound="FuncBound">
<columns>
 <asp:TemplateColumn HeaderText="Name" runat=server>
  <ItemTemplate>
   <asp:Label id=LabelName
Text='<%#DataBinder.Eval(Container.DataItem,"au_name")%>' runat=server/>
  </ItemTemplate>
  <EditItemTemplate><asp:DropDownList id="TxtName"
runat=server/></EditItemTemplate>
 </asp:TemplateColumn>
</columns>
</asp:datagrid>

--
Rajesh Tiwari


Quote:> If I put a dropdown list in the ItemTemplate Column, can I bind the
dropdown
> list with the record in a lookup table?

> For example, assuming I have a Direction lookup table with 4 records,
i..e.
> East, West, North, South. I also have a Trip table, which is displayed on
> the page using datagrid. The datagrid has a Direction dropdown list in
> ItemTemplate Column. As existing records in Trip table have the Direction
> field entered, so when I do database query, I want the Direction dropdown
> list bound to the Direction lookup table with the proper Direction field
> selected. The selection is based on the Direction field in Trip table.

> Is this do-able? Any idea? and is there any web site/link/snippet for
> reference in this issue?

> Thanks a lot.


 
 
 

databinding for dropdown list in the ItemTemplate Column

Post by Michell » Wed, 11 Jun 2003 04:51:04


Rajesh,

Thanks for your reply.

My thought is a little different from yours. You put a Label for display in
ItemTemplate, and dropdown list in EditItemTemplate for editing. What I am
thinking is to put the dropdown list in ItemTemplate, so the dropdown list
can be used for display and editing.

In this case, I wonder whether the OnItemDataBound still works for me. As in
FuncBound() function, I can't use DataRowView myRow =
(DataRowView)(e.Item.DataItem);  to get my row number. Also,
myList.Items.FindByValue(myRole).Selected = true; always get me error
message "Object reference not set to an instance of an object. ".

Any ideas? Thanks.


> yes,its possible.

> try something similar as the code below to find out if it can be of some
> help.

> specifically you need to call a function OnItemDataBound event of the
> datagrid and in that function u can populate ur combo from database table
> and set something selected.

> code example

> <script language="C#" runat=server>
> SqlConnection con;
> DataView dv;
> void Page_Load(Object sender,EventArgs e)
> {
> con=new SqlConnection("Server=rajesh;database=test;uid=sa;pwd=sa");
> if(!IsPostBack)
> BindGrid();
> }

> public void BindGrid()
> {
> ----------------
> }

> public void FuncBound(Object Sender,DataGridItemEventArgs e)
> {
>  ListItemType itemType=e.Item.ItemType;
>  if(itemType == ListItemType.EditItem)
>  {
>      DropDownList myList = (DropDownList)(e.Item.FindControl("TxtName"));
>      DataRowView myRow = (DataRowView)(e.Item.DataItem);
>      String myRole= myRow["au_name"].ToString();
>      myList.DataTextField = "au_name";
>             myList.DataValueField = "au_name";
>      myList.DataSource = dv;
>      myList.DataBind();
>      myList.Items.FindByValue(myRole).Selected = true;
>      }
> }
> </script>
> <form id=frm1 runat=server>
> <asp:datagrid id=grd runat=server  OnItemDataBound="FuncBound">
> <columns>
>  <asp:TemplateColumn HeaderText="Name" runat=server>
>   <ItemTemplate>
>    <asp:Label id=LabelName
> Text='<%#DataBinder.Eval(Container.DataItem,"au_name")%>' runat=server/>
>   </ItemTemplate>
>   <EditItemTemplate><asp:DropDownList id="TxtName"
> runat=server/></EditItemTemplate>
>  </asp:TemplateColumn>
> </columns>
> </asp:datagrid>

> --
> Rajesh Tiwari



> > If I put a dropdown list in the ItemTemplate Column, can I bind the
> dropdown
> > list with the record in a lookup table?

> > For example, assuming I have a Direction lookup table with 4 records,
> i..e.
> > East, West, North, South. I also have a Trip table, which is displayed
on
> > the page using datagrid. The datagrid has a Direction dropdown list in
> > ItemTemplate Column. As existing records in Trip table have the
Direction
> > field entered, so when I do database query, I want the Direction
dropdown
> > list bound to the Direction lookup table with the proper Direction field
> > selected. The selection is based on the Direction field in Trip table.

> > Is this do-able? Any idea? and is there any web site/link/snippet for
> > reference in this issue?

> > Thanks a lot.

 
 
 

databinding for dropdown list in the ItemTemplate Column

Post by Rajesh Tiwar » Wed, 11 Jun 2003 06:09:30


Michelle,
the code sample that i gave u was just intended to give u an idea of the place where ur problem cud be and needed to be manipulated to fit in ur case.you can very easily use dropdown list for editing and display both.
the error that u are getting is due to the fact that  Condition    if(itemType == ListItemType.EditItem)  is not tailored according to ur case.actually ListItemType is an Enumeration and can have 8 different values viz.AlternatingItem,EditItem,Footer,Header,Item,Pager,SelectedItem,Separator  according to the part of the datagrid that is to be manipulated.

so try to use
if(itemType == ListItemType.Item || itemType == ListItemType.AlternatingItem)
  if u are using ur dropdownlist in Itemtemplate
--
Rajesh Tiwari


> Rajesh,

> Thanks for your reply.

> My thought is a little different from yours. You put a Label for display in
> ItemTemplate, and dropdown list in EditItemTemplate for editing. What I am
> thinking is to put the dropdown list in ItemTemplate, so the dropdown list
> can be used for display and editing.

> In this case, I wonder whether the OnItemDataBound still works for me. As in
> FuncBound() function, I can't use DataRowView myRow =
> (DataRowView)(e.Item.DataItem);  to get my row number. Also,
> myList.Items.FindByValue(myRole).Selected = true; always get me error
> message "Object reference not set to an instance of an object. ".

> Any ideas? Thanks.



> > yes,its possible.

> > try something similar as the code below to find out if it can be of some
> > help.

> > specifically you need to call a function OnItemDataBound event of the
> > datagrid and in that function u can populate ur combo from database table
> > and set something selected.

> > code example

> > <script language="C#" runat=server>
> > SqlConnection con;
> > DataView dv;
> > void Page_Load(Object sender,EventArgs e)
> > {
> > con=new SqlConnection("Server=rajesh;database=test;uid=sa;pwd=sa");
> > if(!IsPostBack)
> > BindGrid();
> > }

> > public void BindGrid()
> > {
> > ----------------
> > }

> > public void FuncBound(Object Sender,DataGridItemEventArgs e)
> > {
> >  ListItemType itemType=e.Item.ItemType;
> >  if(itemType == ListItemType.EditItem)
> >  {
> >      DropDownList myList = (DropDownList)(e.Item.FindControl("TxtName"));
> >      DataRowView myRow = (DataRowView)(e.Item.DataItem);
> >      String myRole= myRow["au_name"].ToString();
> >      myList.DataTextField = "au_name";
> >             myList.DataValueField = "au_name";
> >      myList.DataSource = dv;
> >      myList.DataBind();
> >      myList.Items.FindByValue(myRole).Selected = true;
> >      }
> > }
> > </script>
> > <form id=frm1 runat=server>
> > <asp:datagrid id=grd runat=server  OnItemDataBound="FuncBound">
> > <columns>
> >  <asp:TemplateColumn HeaderText="Name" runat=server>
> >   <ItemTemplate>
> >    <asp:Label id=LabelName
> > Text='<%#DataBinder.Eval(Container.DataItem,"au_name")%>' runat=server/>
> >   </ItemTemplate>
> >   <EditItemTemplate><asp:DropDownList id="TxtName"
> > runat=server/></EditItemTemplate>
> >  </asp:TemplateColumn>
> > </columns>
> > </asp:datagrid>

> > --
> > Rajesh Tiwari



> > > If I put a dropdown list in the ItemTemplate Column, can I bind the
> > dropdown
> > > list with the record in a lookup table?

> > > For example, assuming I have a Direction lookup table with 4 records,
> > i..e.
> > > East, West, North, South. I also have a Trip table, which is displayed
> on
> > > the page using datagrid. The datagrid has a Direction dropdown list in
> > > ItemTemplate Column. As existing records in Trip table have the
> Direction
> > > field entered, so when I do database query, I want the Direction
> dropdown
> > > list bound to the Direction lookup table with the proper Direction field
> > > selected. The selection is based on the Direction field in Trip table.

> > > Is this do-able? Any idea? and is there any web site/link/snippet for
> > > reference in this issue?

> > > Thanks a lot.

 
 
 

1. Databinding a dropdown List

Please help!! I'm tearing my hair out with this!!!

I'm trying to do a bind a datatset to a dropdownlist! I have seen that
this is should be very simple and basic, but for the life of me I
can't get it to work!!!

This is my error!
###########################################

An invalid data source is being used for dlCompSetupType. A valid data
source must implement either IListSource or IEnumerable.

Exception Details: System.ArgumentException: An invalid data source is
being used for dlCompSetupType. A valid data source must implement
either IListSource or IEnumerable.

Source Error:

Line 67:             dlCompSetupType.DataBind()
Line 68:         End Sub
Line 69:
 ##############################################

Here is a code snippet
//////////////////////////////////////
 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

            Dim oSegments As New DataSet()
            With New bus_Company()
                oSegments = .GetCCSegmentData
            End With
            'dlCompSetupType.DataSource = oSegments ''''Tried this
didn't work
                                                    ''''so created a
dataview...

            Dim dv As DataView = New DataView(oSegments.Tables(0))
            dlCompSetupType.DataSource = dv 'This is falling over too
           dlCompSetupType.DataBind()
        End Sub
////////////////////////////////////////////////

The Function GetCCSegmentData returns a dataset
Please help!

Thanks in Advance
Paul

2. Assembly help on OSX

3. databinding to a dropdown list

4. Cisco 7912g ip phone and SIP

5. Datagrid w/dropdown list column?

6. rearrnage toolbar icons

7. Fill dropdown or list box into two data columns

8. Importing Genealogy files

9. multi column dropdown list

10. Databinding DataTable's Column to a drop down list

11. Databinding - ITEMTEMPLATE problem

12. databinding in ItemTemplate