So I have a gridview in my webpage, At the moment the table returns values from an SQL table.
Inside my table I have 'Allowed Editing/Updates' which is updating values in my SQL table from the gridview in the webpage, this functionality works fine, my only problem is... In one of the columns on the gridview I would like to have the option to Select from a drop down box (as oppose to typing in a text box like the other columns),
The drop down box would be linked to list that I have in SQL, I have tried all sorts of techniques and have searched endlessly Online to try and do this, yet I have no luck,
I was was wondering if I could get some assistance with this....
Thankyou in advance. Here is my aspx code for my gridview at the moment:
<asp:GridView ID="GridView2" runat="server" DataKeyNames="ID" AutoGenerateColumns="False" border="1" DataSourceID="SqlDataSource3" style="font-size: 0.9em; margin-left: 2%;" Width="85%">
<Columns>
<asp:BoundField DataField="contactName" HeaderText="contactName" SortExpression="contactName">
</asp:BoundField>
<asp:BoundField DataField="contactEmail" HeaderText="contactEmail" SortExpression="contactEmail">
</asp:BoundField>
<asp:BoundField DataField="Telephone" HeaderText="Telephone" SortExpression="Telephone">
</asp:BoundField>
<asp:BoundField DataField="jobRole" HeaderText="jobRole" SortExpression="jobRole">
</asp:BoundField>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
</Columns>
</asp:GridView>
<br />
<asp:SqlDataSource ID="SqlDataSource3"
runat="server" ConnectionString="<%$ ConnectionStrings:saleConnectionString %>"
DeleteCommand="Delete from IMContactEmails where ID = @ID"
SelectCommand="SELECT contactName, contactEmail, Telephone, jobRole, ID FROM
IMContactEmails WHERE
(ProjectNo = @PID)" UpdateCommand="Update IMContactEmails Set
contactName=@contactName,contactEmail=CASE WHEN
@contactEmail IS NULL THEN contactEmail Else @contactEmail END,Telephone=CASE
WHEN
@Telephone IS NULL THEN Telephone ELSE @Telephone END,jobRole=CASE WHEN
@jobRole IS NULL THEN jobRole ELSE @jobRole END where ID=@ID">
<DeleteParameters>
<asp:Parameter Name="ID" Type="String" />
</DeleteParameters>
<SelectParameters>
<asp:ControlParameter
ControlID="PIDvalTextBox" Name="PID" PropertyName="Text" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="contactName" />
<asp:Parameter Name="contactEmail" />
<asp:Parameter Name="Telephone" />
<asp:Parameter Name="jobRole" />
<asp:Parameter Name="ID" />
</UpdateParameters>
</asp:SqlDataSource>
UPDATE:
Here is the code inside my columns with the TemplateField
:
<asp:GridView ID="GridView2" runat="server" DataKeyNames="ID" AutoGenerateColumns="False" border="1" DataSourceID="SqlDataSource3" style="font-size: 0.9em; margin-left: 2%;" Width="85%">
<Columns>
<asp:BoundField DataField="contactName" HeaderText="contactName" SortExpression="contactName">
</asp:BoundField>
<asp:BoundField DataField="contactEmail" HeaderText="contactEmail" SortExpression="contactEmail">
</asp:BoundField>
<asp:BoundField DataField="Telephone" HeaderText="Telephone" SortExpression="Telephone">
</asp:BoundField>
<asp:TemplateField HeaderText="jobRole">
<EditItemTemplate>
<asp:DropDownList ID="DDLjobRole" runat="server"
DataSourceID="SqlDataSource1"
DataTextField="jobRole" DataValueField="jobRole"
SelectedValue='<%# Bind("jobRole")%>'>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
</Columns>
</asp:GridView>
Put a TemplateField
in your Columns and add a dropdown list over there.
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1"
DataTextField="Name" DataValueField="Name"
SelectedValue='<%# Bind("Name") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label id="Label1" runat="server" Text=''<%# Bind("Name") %>''
</ItemTemplate>
</asp:TemplateField>
Add a sql data source and write the query to bind your drop down list something as below:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Your connection string"
SelectCommand="SELECT Name FROM dbo.Table"></asp:SqlDataSource>
Try it. Hope it helps.