dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online MembersSarfaraz M Bhat
sunny
dilip
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » Code Snippets » DataGridView

Updating Using a DDL in Gridview


Posted Date:     Category: DataGridView    
Author: Member Level: Gold    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>






Did you like this resource? Share it with your friends and show your love!


Responses to "Updating Using a DDL in Gridview"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Making Grid View Row Editable when data binding through Code Behind
    Previous Resource: Delete multiple row in checked datagrid
    Return to Resources
    Post New Resource
    Category: DataGridView


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Dropdown  .  Asp.net  .  Gridview  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.