Populate Drop. Down.List with Selected Value in Edit.Item. Template of Grid.View in ASP. Net.In this article I will explain with an example how to use ASP.Net Drop. Down. List control in the Edit.Item. Template of ASP.X(1)S(xovbhh45jnvoc4jlybfb4rrc))/GetFile.aspx?File=/doc-images/html/htmlint-newproject.png' alt='Visual Studio Gridview Enable Editing Word' title='Visual Studio Gridview Enable Editing Word' />Net Grid.View control. Database and Connection string For this article as usual I have used my favorite North.Wind database which you can get by clicking on the link below.Below is the connection string from the Web.Config filelt connection.Strings lt addnamecon.Stringconnection.StringData Source.SQLExpress databaseNorthwind Integrated Securitytrue lt connection.Strings Grid. View Markup.Below I have a simple Grid.View ASP. Net Grid.View control populated from the Customers table of Northwind database.MDGx AXCEL216 MAX Speed Performance Windows 10 2012 8.Vista 2003 XP SP1 SP2 SP3 ME 2000 98 SE OSR2 OSR1 95 NT4 NT 3.DOS 6 Tricks Secrets Tips.It displays 2 columns Contact Name and City of which city is editable via ASP.Net Drop. Down. List control.The identifier column Customer Id is bind to the Data.Key. Names property.Grid. View. IDgv.CustomersData. Key.NamesCustomer. IdrunatserverAuto.Generate. ColumnsfalseOn. 6 Second Self Defense System Video Course Execution . Row. EditingEdit.CustomerOn. Row. Data.BoundRow. Data. BoundOn.Row. UpdatingUpdate.CustomerOn. Row. Canceling.EditCancel. Edit lt Columns lt asp Bound.Field. Data. FieldContact.NameHeader. TextContact Name lt asp Template.Field. Header. TextCity lt Item.Template lt asp Label.IDlbl. CityrunatserverTextlt EvalCity lt asp Label lt Item.Template lt Edit.Item. Template lt asp Label.IDlbl. CityrunatserverTextlt EvalCity Visiblefalse lt asp Label lt asp Drop.Down. List. IDddl.Citiesrunatserver lt asp Drop.Down. List lt Edit.Item. Template lt asp Template.Field lt asp Command.Field Show. Edit.ButtonTrue lt Columns lt asp Grid.View Binding the Grid.View. Below is the code to Bind the Grid.View control with data.Cprotectedvoid PageLoadobject sender, Event.Args e if Is. Post.Back this.Bind. Data privatevoid Bind.Data string query SELECT top 1.FROM Customers Sql.Command cmd new. Sql.Commandquery gv.Customers. Data. Source Get.Datacmd gv. Customers.Data. Bind private.Data. Table Get. DataSql.Command cmd string str.Conn. String Configuration.Manager. Connection.Stringscon. String.Connection. String using Sql.Connection con new.Sql. Connectionstr.Conn. String using Sql.Data. Adapter sda new.Sql. Data. Adapter cmd.Connection con sda.Select. Command cmd using Data.Table dt new. Data.Table sda.Filldt return dt VB.Net. Protected. Sub PageLoadsender As.Object, e As. Event.Args Handles. Me.Load If. Not Is.Post. Back Then Me.Bind. Data End.If. End. Sub. Private.Sub Bind. Data Dim query As.String SELECT top 1.FROM Customers Dim cmd As.New. Sql. Commandquery gv.Customers. Data. Source Get.Datacmd gv. Customers.Data. BindEnd. Sub.Private. Function Get.Datacmd As. Sql. Command As.Data. Table Dim str.Conn. String As. String Configuration.Manager. Connection.Stringscon. String.Connection. String Using con As.New. Sql. Connectionstr.Conn. String Using sda As.New. Sql. Data. Adapter cmd.Connection con sda.Select. Command cmd Using dt As.New. Data. Table sda.Filldt Return dt End.Using End. Using End.Using. End. Function.Screenshot. Below is the screenshot of Grid.View with data. Editing the Grid.View Row. The below events handle the Grid.View Row Edit and Cancel Edit Events.Cprotectedvoid Edit.Customerobject sender, Grid.View. Edit. Event.Args e gv. Customers.Edit. Index e. New.Edit. Index Bind.Data protectedvoid Cancel.Editobject sender, Grid.View. Cancel. Edit.Event. Args e gv.Customers. Edit. Index 1 Bind.Data VB. Net. Protected.Sub Edit. Customersender As.Object, e As. Grid.View. Edit. Event.Args gv. Customers.Edit. Index e. New.Edit. Index Me.Bind. DataEnd. Sub.Protected. Sub Cancel.Editsender As. Object, e As.Grid. View. Cancel.Edit. Event. Args gv.Customers. Edit. Index 1 Bind.DataEnd. Sub. Binding the Drop.Down. List. The Drop.Down. List has to be bind in the Row.Data. Bound event of the ASP.Net Grid. View control in the following way.I have kept an invisible Label in order to get the previously stored City.Cprotectedvoid Row.Data. Boundobject sender, Grid.View. Row. Event.Args e if e. Row.Row. Type Data. Control.Row. Type. Data. Row gv.Customers. Edit. Index e.Row. Row. Index Drop.Down. List ddl. Cities Drop.Down. Liste. Row.Find. Controlddl.Cities string query select distinct city from customers Sql.Command cmd new. Sql.Commandquery ddl.Cities. Data. Source Get.Datacmd ddl.Cities. Data. Text.Field city ddl.Cities. Data. Value.Field city ddl.Cities. Data. Bind ddl.Cities. Items. Find.By. Valuee. Row. Find.Controllbl. City as.Label. Text. Selected true VB.Net. Protected. Sub Row.Data. Boundsender As.Object, e As. Grid.View. Row. Event.Args If e. Row.Row. Type Data. Control.Row. Type. Data. Row And.Also gv. Customers.Edit. Index e. Row.Row. Index Then Dim ddl.Cities As. Drop. Down.List Direct. Caste.Row. Find. Controlddl.Cities, Drop. Down.List Dim query As.String select distinct city from customers Dim cmd As.New. Sql. Commandquery ddl.Cities. Data. Source Get.Datacmd ddl.Cities. Data. Text.Field city ddl.Cities. Data. Value.Field city ddl.Cities. Data. Bind ddl.Cities. Items. Find.By. ValueTry. Caste.Row. Find. Controllbl.City, Label. Text.Selected True End.If. End. Sub. Screenshot.The below screenshot displays the Grid.View with row being edited.Updating the Grid.View Row. Finally heres the code to update the record with the new selected value from the ASP.Net Drop. Down. List control.Cprotectedvoid Update.Customerobject sender, Grid.View. Update. Event.Args e string city gv.Customers. Rowse.Row. Index. Find.Controlddl. Cities as Drop.Down. List. Selected.Item. Value string customer.Id gv. Customers.Data. Keyse. Row.Index. Value. To.String string str.Conn. String Configuration.Manager. Connection.Stringscon. String.Connection. String using Sql.Connection con new Sql.Connectionstr. Conn.String string query update customers set city city where customer.Id customer. Id using Sql.Command cmd new Sql.Commandquery cmd.Connection con cmd.Parameters. Add. With.Valuecity, city cmd.Parameters. Add. With.Valuecustomer. Id, customer.Id con.Open cmd.Execute. Non. Query con.Close Response.RedirectRequest. Url.Absolute. Uri VB.Net. Protected. Sub Update.Customersender As.Object, e As. Grid.View. Update. Event.Args Dim city As.String Try. Castgv.Customers. Rowse.Row. Index. Find.Controlddl. Cities, Drop.Down. List. Selected.Item. Value Dim customer.Id As. String gv.Customers. Data. Keyse.Row. Index. Value.To. String Dim str.Conn. String As. String Configuration.Manager. Connection.Stringscon. String.Connection. String Using con As.New. Sql. Connectionstr.Conn. String Dim query As.String update customers set city city where customer.Id customer. Id Using cmd As.New. Sql. Commandquery cmd.Connection con cmd.Parameters. Add. With.Valuecity, city cmd.Parameters. Add. With.Valuecustomer. Id, customer.Id con.Open cmd.Execute. Non. Query con.Close Response.RedirectRequest. Url.Absolute. Uri End.Using End. Using.End. Sub. Downloads.You can download the full source code of this article in C and VB.Net using the download link provided below.Dropdownlistin. Edit.Item. Template. Grid.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
September 2018
Categories |