C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Updating Using a DDL in Gridview


Posted Date: 09 Jan 2008    Resource Type: Code Snippets    Category: DataGridView

Posted By: Mohan       Member Level: Gold
Rating:     Points: 5



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>






Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Gridview  .  Dropdown  .  Asp.net  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Making Grid View Row Editable when data binding through Code Behind
Previous Resource: Delete multiple row in checked datagrid
Return to Discussion Resource Index
Post New Resource
Category: DataGridView


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

internet fax

Contact Us    Privacy Policy    Terms Of Use