|
Resources » Code Snippets » DataGridView
Updating Using a DDL in Gridview
|
This code sample shows how to use a DropDownList inside a Gridview. The States which are listed in the DropDownList are actually pulled from a separate 'States' table, with the Selected Item chosen by the current record.
Along with that, we also show how an updata works with the same setup. As with a few other samples, this one loads the SelectCommand and the Updatecommand for the Gridview's SQLDataSource in the Page_Load event. Naturally, these properties could be in the SQLDataSource tag itself. However, the line on this screen would be very long, therefore, it's done this way to save horizontal the screen's real estate.
To be able to use this Update scenario with a Stored Procedure, only two lines need to be addressed. First, of course, change the UpdateCommand property to the actual name of the Stored Procedure. Then, assign 'Stored Procedure" to the 'UpdateCommandType' property:
UpdateCommand="procUpdateEmployees" UpdateCommandType="storedProcedure"
<script language="VB" Runat="server"> Sub Page_Load(Source as Object, E as EventArgs) sqlDS1.SelectCommand = "SELECT EmployeeID, LastName, FirstName, " & _ "Title, Address, City, Region, PostalCode From Employees" sqlDS1.UpdateCommand="UPDATE [Employees] SET [LastName] = " & _ "@LastName, [FirstName] = @FirstName, [City] = @City, [Region] = " & _ "@Region WHERE [EmployeeID] = @EmployeeID" End Sub </script> <html> <head> <meta name="GENERATOR" Content="ASP Express 5.0"> <title>DropDownList Inside a GridView</title> </head> <body> <div align="center"><b>DropDownList Inside a GridView</b></div> <form id="form1" Runat="server"> <div align="center"> <asp:GridView Runat="server" Id="gvEmp" GridLines="None" cellpadding="0" cellspacing="2" BackColor="#E0E0F6" Font-Name="Arial" Font-Size="10" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" Width="75%" AutogenerateColumns="False" DataSourceID="sqlDS1" DataKeyNames="Employeeid" AutoGenerateEditButton="True"> <HeaderStyle BackColor="#7988B7" Font-Names="Arial" Font-Bold="True" Font-Size="12" /> <AlternatingRowStyle BackColor="#DFDFDF" Font-Names="Arial" Font-Size="10" /><Columns> <asp:BoundField DataField="EmployeeID" HeaderText="ID" ReadOnly="True" /> <asp:BoundField DataField="FirstName" HeaderText="FirstName" /> <asp:BoundField DataField="LastName" HeaderText="LastName" /> <asp:BoundField DataField="Address" HeaderText="Address" /> <asp:BoundField DataField="City" HeaderText="City" /> <asp:TemplateField HeaderText="State"> <ItemTemplate> <asp:Label id="label1" runat="server" Text='<%# Container.DataItem("Region") %>'> </asp:Label> </ItemTemplate> <EditItemTemplate> <asp:DropDownList id="ddlStates" runat="server" DataSourceID="sqlDS2" BackColor="Pink" DataTextField="StAbbr" DataValueField="StAbbr" SelectedValue='<%# Bind("Region") %>'> </asp:DropDownList> </EditItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> <asp:SQLDataSource ID="sqlDS1" Runat="Server" SelectCommand="Select [FieldList] from [TableName]" UpdateCommand="Update [TableName] set [Field=@Field] Where EmployeeID=@EmployeeID" ConnectionString="<%$ ConnectionStrings:YouConnStringGoesHere %>"> </asp:SQLDataSource> <asp:SQLDataSource ID="sqlDS2" Runat="Server" SelectCommand = "SELECT STabbr From states" DataSourceMode="DataSet" ConnectionString="<%$ ConnectionStrings:YouConnStringGoesHere %>"> </asp:SQLDataSource> </form> </body> </html>
|
Did you like this resource? Share it with your friends and show your love!
|
|
|
No responses found. Be the first to respond...
|
 Follow us on Twitter: https://twitter.com/dotnetspider
|
|