DataGrid with DropDownBox

M

Mark

G'day,


I'm having trouble switching from asp to asp.net, and i'm trying to
create a list from a database using a datagrid but i'm not sure how to
go about it.

i need a grid that displays two coloumns from a databasem, and three
other coloumsn that have dropdown boxs that the user can change. Then
hit save at the bottom and update the database.

| firstname | last name | property1 | property2 | property3 |

I can get the data from the database and update it, but not build the
datagrid itself. I'm used to the old style asp where you build the html
table manually.
But i can't work out how to bind two datagrid coloumns, put
dropdownboxes in the three other coloumns and then on click of the
button at the bottom, get these values for putting in the database!



Thanks for any help!
 
G

Guest

Hi Mark

For starters, check out this article, DataGrid Revisited by Peter van Ooijen: http://www.dotnetjunkies.com/Tutorial/B8550E4B-B8F5-4446-B065-0B19F4C739BC.dcik That will give you the foundation you need and make the rest (mostly) easy

Then, I suggest you start your grid simply, by creating your five columns (don't let the datagrid build them "automatically" for you; add each column yourself). In each column, you really only need the Heading (e.g. First Name) and the DataField (the field name from the datarow, e.g. xx_FirstName). At that point you should have a nice grid showing five columns from all your rows

To get the dropdowns, you'll convert your three columns to "template" columns, and in the "item" template of each one you'll drop a dropdownlist control (and get rid of the label that's there). Set the ID to something you recognize (e.g. ddlField1, etc.), because you'll need it later. In your code, you'll want to catch ItemDataBound for the grid, which happens as the data is bound to each row and is the last chance to catch it before it will be rendered. Because ItemDataBound gets *all* rows, you need to check for just the ones you want in e.item.itemtype (e.g. keep .item, .alternatingitem, and maybe .selecteditem, but ignore .header, .footer, etc.). For your row item, do FindControl on the ID of your ddl, add the list of possible values, and set your selected value/index from your data. Remember that e.item is the item in the *grid*, and e.item.dataitem is the *data* behind it (in your case, it will be a datarowview)

I think there might be some easier ways to handle the binding of your actual value to the dropdown, so hopefully you'll hear from others. I hope this gets you started. Have fun

Regards

Bil

P.S. My references are all to VB syntax, but gist is same for C#

----- Mark wrote: ----

G'day,


I'm having trouble switching from asp to asp.net, and i'm trying t
create a list from a database using a datagrid but i'm not sure how t
go about it

i need a grid that displays two coloumns from a databasem, and thre
other coloumsn that have dropdown boxs that the user can change. The
hit save at the bottom and update the database

| firstname | last name | property1 | property2 | property3

I can get the data from the database and update it, but not build th
datagrid itself. I'm used to the old style asp where you build the htm
table manually
But i can't work out how to bind two datagrid coloumns, pu
dropdownboxes in the three other coloumns and then on click of th
button at the bottom, get these values for putting in the database



Thanks for any help

*** Sent via Developersdex http://www.developersdex.com **
Don't just participate in USENET...get rewarded for it
 
M

Mark

hi thanks, that got us on the right track, but i have a few another
question.
I'm trying to get the value that is already in the database as the
"selected" option in the each dropdownlist in each coloumn. Is there
anyway to add one listitem after the databinding has occurred?

thanks for any help,



<asp:datagrid id="dgMarkingSheet" AutoGenerateColumns="False"
Runat="server">
<Columns>
<asp:BoundColumn DataField="FamilyName" ReadOnly="True"
HeaderText="LastName"></asp:BoundColumn>
<asp:BoundColumn DataField="PreferredName" ReadOnly="True"
HeaderText="FirstName"></asp:BoundColumn>
<asp:BoundColumn DataField="StudentID" ReadOnly="true"
HeaderText="Student ID"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Behaviour">
<ItemTemplate>
<asp:DropDownList Runat="server" ID="behaviour1"
DataValueField="Behaviour" DataTextField="Behaviour">
<asp:ListItem Value="Very Good">Very Good</asp:ListItem>
<asp:ListItem Value="Good">Good</asp:ListItem>
<asp:ListItem Value="Satisfactory">Satisfactory</asp:ListItem>
<asp:ListItem Value="Needs Attention">Needs
Attention</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Industry">
<ItemTemplate>
<asp:DropDownList Runat="server" ID="industry1">
<asp:ListItem Value="Very Good">Very Good</asp:ListItem>
<asp:ListItem Value="Good">Good</asp:ListItem>
<asp:ListItem Value="Satisfactory">Satisfactory</asp:ListItem>
<asp:ListItem Value="Needs Attention">Needs
Attention</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Interview">
<ItemTemplate>
<asp:DropDownList Runat="server" ID="interview1">
<asp:ListItem Value="Essential">Essential</asp:ListItem>
<asp:ListItem Value="Recommended">Recommended</asp:ListItem>
<asp:ListItem Value="Optional">Optional</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
 
G

Guest

Hi Mark, if I understand you right, you want to bind your database field to the SelectedValue (or maybe SelectedIndex) of the dropdownlist (although most often we're binding to the Text property of a control, you can bind data to *any* property, such as SelectedIndex, Width, etc.)

In the html of your control, it should read something like SelectedValue = '<%# databinder.eval(...) %>', where you want databinder.eval to evaluate to the particular column value from your datasource. Afaik, all that happens when you do the databind is that the system loops through all the controls on that container (which is the page if you do page.databind, or another container, like the grid if you do grid.databind). So, you just want what's between the %'s and after the # to evaluate to something that makes sense for the property you're trying to assign

Let me know if that helps

----- Mark wrote: ----

hi thanks, that got us on the right track, but i have a few anothe
question.
I'm trying to get the value that is already in the database as th
"selected" option in the each dropdownlist in each coloumn. Is ther
anyway to add one listitem after the databinding has occurred?

thanks for any help



<asp:datagrid id="dgMarkingSheet" AutoGenerateColumns="False
Runat="server"><Columns><asp:BoundColumn DataField="FamilyName" ReadOnly="True
HeaderText="LastName"></asp:BoundColumn><asp:BoundColumn DataField="PreferredName" ReadOnly="True
HeaderText="FirstName"></asp:BoundColumn><asp:BoundColumn DataField="StudentID" ReadOnly="true
HeaderText="Student ID"></asp:BoundColumn><asp:TemplateColumn HeaderText="Behaviour"><ItemTemplate><asp:DropDownList Runat="server" ID="behaviour1
DataValueField="Behaviour" DataTextField="Behaviour"><asp:ListItem Value="Very Good">Very Good</asp:ListItem><asp:ListItem Value="Good">Good</asp:ListItem><asp:ListItem Value="Satisfactory">Satisfactory</asp:ListItem><asp:ListItem Value="Needs Attention">Need
Attention</asp:ListItem></asp:DropDownList></ItemTemplate></asp:TemplateColumn><asp:TemplateColumn HeaderText="Industry"><ItemTemplate><asp:DropDownList Runat="server" ID="industry1"><asp:ListItem Value="Very Good">Very Good</asp:ListItem><asp:ListItem Value="Good">Good</asp:ListItem><asp:ListItem Value="Satisfactory">Satisfactory</asp:ListItem><asp:ListItem Value="Needs Attention">Need
Attention</asp:ListItem></asp:DropDownList></ItemTemplate></asp:TemplateColumn><asp:TemplateColumn HeaderText="Interview"><ItemTemplate><asp:DropDownList Runat="server" ID="interview1"><asp:ListItem Value="Essential">Essential</asp:ListItem><asp:ListItem Value="Recommended">Recommended</asp:ListItem><asp:ListItem Value="Optional">Optional</asp:ListItem></asp:DropDownList></ItemTemplate></asp:TemplateColumn></Columns></asp:datagrid

*** Sent via Developersdex http://www.developersdex.com **
Don't just participate in USENET...get rewarded for it
 
M

Mark

Thanks for the help, i did manage a work around but it's not the best
solution.

So if i use '<%# databinder.eval(...) in the SelectedValue of say a
listbox, it will output the text value of that database coloum. Score!
This is very much what i'm after, in that the dropdownlist of each
coloumn the values are pulled from one table, and the "selected" index
needs to the value from the table bound to the datalist.

I'm sure the more normal edit/update/delete functions of the datalist
are more useful in most situtations.

I'll certainly give this a try tomorrow to see if can get it working.
Thanks for the tips.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top