Asked by:
How to spruce up, enhance, and add row update, item delete to standard GrivView?

Question
-
User2142845853 posted
hi,
Have a VB.net Webforms with standard Gridview. Works just fine. But how to add features to it such as filter on TextBox.Text, add paging to begin on N rows of data?
thanks
Friday, January 3, 2020 11:26 PM
All replies
-
User475983607 posted
Have a VB.net Webforms with standard Gridview. Works just fine. But how to add features to it such as filter on TextBox.Text, add paging to begin on N rows of data?
The official GridView documentation covers filtering, sorting, and paging fundamentals.
Friday, January 3, 2020 11:44 PM -
User2142845853 posted
Thanks but sadly? this is a VISUAL BASIC project. Something hated apparently? Why is VB not welcome? The link to the project doesnt work, it redirects to documents and nothing but C# not a show stopper but close.
Need to really see that original project! thanks
That info in vb would answer the question.... but this is my luck! incredible
Saturday, January 4, 2020 12:32 AM -
User2142845853 posted
Exception: Both DataSource and DataSourceID are defined on GridView1. Remove one definition
https://docs.microsoft.com/en-us/aspnet/web-forms/overview/presenting-and-managing-data/model-binding/sorting-paging-and-filtering-data would be a great reference, but the original solution is not available, Microsoft seems to have fewer VB
Looking for a reference on VB (Visual Basic) .net and the Gridview, framework is version 4.5.2
Saturday, January 4, 2020 4:20 PM -
User475983607 posted
Sorting and paging is configured in the markup and nothing to do with C# or VB.NET. There are quite a lot of examples floating around the web.
Plus you can take a look at the GridView reference document. It shows the different GridView options and explains each.
That leaves executing a parameter query filtered by user entry which is very common and straight forward. Share your code if you are having trouble setting up the query and binding the results.
Saturday, January 4, 2020 7:37 PM -
User288213138 posted
Hi rogersbr,
https://docs.microsoft.com/en-us/aspnet/web-forms/overview/presenting-and-managing-data/model-binding/sorting-paging-and-filtering-data would be a great reference, but the original solution is not available, Microsoft seems to have fewer VB
Looking for a reference on VB (Visual Basic) .net and the Gridview, framework is version 4.5.2
C # code can be converted into VB code and vice versa. you can use this tool: http://converter.telerik.com/
Exception: Both DataSource and DataSourceID are defined on GridView1. Remove one definitionThis error is usually because you have bound 2 data sources to the GridView, one is the SqlDataSource control, and the other is bound in the code behind. You just need to use one of them.
This demo for you as a reference:
Search Customer: <asp:TextBox ID="txtSearch" runat="server"></asp:TextBox> <asp:Button Text="Search" runat="server" OnClick="Search" /> <hr /> <asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" AllowPaging="true" OnPageIndexChanging="gvCustomers_PageIndexChanging" PageSize="3"> <Columns> <asp:BoundField DataField="CustomerId" HeaderText="CustomerId" ItemStyle-Width="150" /> <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" /> <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" /> </Columns> </asp:GridView> Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) If Not IsPostBack Then SearchCustomers() End If End Sub Private Sub SearchCustomers() Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString Using con As SqlConnection = New SqlConnection(constr) Using cmd As SqlCommand = New SqlCommand() Dim sql As String = "SELECT CustomerId, Name, Country FROM Customers" If Not String.IsNullOrEmpty(txtSearch.Text.Trim()) Then sql += " WHERE Name LIKE @Name + '%'" cmd.Parameters.AddWithValue("@Name", txtSearch.Text.Trim()) End If cmd.CommandText = sql cmd.Connection = con Using sda As SqlDataAdapter = New SqlDataAdapter(cmd) Dim dt As DataTable = New DataTable() sda.Fill(dt) gvCustomers.DataSource = dt gvCustomers.DataBind() End Using End Using End Using End Sub Protected Sub Search(ByVal sender As Object, ByVal e As EventArgs) SearchCustomers() End Sub Protected Sub gvCustomers_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs) gvCustomers.PageIndex = e.NewPageIndex SearchCustomers() End Sub
The result:
If you still have questions, please share your code.
Best regards,
Sam
Monday, January 6, 2020 3:21 AM -
User2142845853 posted
Hi Samwu
Hey thats great, thanks! And yes looking at the code today, was able to quickly figure out I had set it up to fill within PageLoad()
With the Gridview I made a sql source, with the wizard, found the connection string, table; But to automatically generate the Insert/Edit/Update? It needs to have an ID field as a primary key. Made a copy of the db and put the id/pk and it works.
But the real sql database table? is one of about 2 dozen other connected tables. So while I quickly got the automatic code to put in Edit and all? Its not usable.
And yes thanks Ive used Telerik, that wouldve solved the entire problem to use one of their grids. The converter they have is great. So now will try your examples, but to be clear the table has no Id w/PK
Monday, January 6, 2020 6:20 PM -
User288213138 posted
Hi rogersbr,
Hey thats great, thanks! And yes looking at the code today, was able to quickly figure out I had set it up to fill within PageLoad()
With the Gridview I made a sql source, with the wizard, found the connection string, table; But to automatically generate the Insert/Edit/Update? It needs to have an ID field as a primary key. Made a copy of the db and put the id/pk and it works.
But the real sql database table? is one of about 2 dozen other connected tables. So while I quickly got the automatic code to put in Edit and all? Its not usable.
And yes thanks Ive used Telerik, that wouldve solved the entire problem to use one of their grids. The converter they have is great. So now will try your examples, but to be clear the table has no Id w/PK
According to your description, I am not very clear about your requirement.
Have you encountered any problems? if so, please describe your problem in detail.
About how to generate the Insert/Edit/Update of GridView you can refer to this video:https://www.youtube.com/watch?v=vZIOI136IKY
Best regards,
Sam
Tuesday, January 7, 2020 2:36 AM -
User2142845853 posted
Hi rogersbr,
According to your description, I am not very clear about your requirement.
Have you encountered any problems? if so, please describe your problem in detail.
About how to generate the Insert/Edit/Update of GridView you can refer to this video:https://www.youtube.com/watch?v=vZIOI136IKY
Best regards,
Sam
Hi Sam
So the youtube video there, yes Venkat's videos are great; But notice that when the new Gridview is there he connects the SQL data source, pulls the connection string, table? When you click the Advanced button in MY case these 2 items are not available. Only there if the table has an ID field set as Primary Key. If its just a table of items without the standard Int ID (not null)? it does not work.
So I cannot use this video as a reference.
The question is, can I add an MVC page to this vb webforms project? And create a db Context class?
Tuesday, January 7, 2020 4:51 PM -
User-821857111 posted
this is a VISUAL BASIC project. Something hated apparently? Why is VB not welcome?Hardly anyone uses VB in web application development. The documentation team used to provide examples in C# and VB, but from traffic analysis, learned that the VB versions were not referred to often enough to justify the investment in maintenance.Tuesday, January 7, 2020 4:58 PM -
User475983607 posted
But notice that when the new Gridview is there he connects the SQL data source, pulls the connection string, table? When you click the Advanced button in MY case these 2 items are not available. Only there if the table has an ID field set as Primary Key. If its just a table of items without the standard Int ID (not null)? it does not work.I unique identifier is required to update a record. You might have to fallback to ADO.NET if a table does not have a primary key.
The question is, can I add an MVC page to this vb webforms project? And create a db Context class?MVC is not required to create a DbContext class. Share you code if you need community assistance.
Tuesday, January 7, 2020 5:15 PM -
User2142845853 posted
rogersbr
this is a VISUAL BASIC project. Something hated apparently? Why is VB not welcome?Hardly anyone uses VB in web application development. The documentation team used to provide examples in C# and VB, but from traffic analysis, learned that the VB versions were not referred to often enough to justify the investment in maintenance.
WOW... I assumed that but thanks for confirming. Guessing the use of mixed C#/VB was dropped too? I cannot change their use of VB here, they use Excel alot and it was started many years ago.
Is it flat out possible to put an MVC page in and create a db context so I can use the entity framework perhaps?
;
The main thing is a functional grid here, MVC would make it easier
Tuesday, January 7, 2020 5:34 PM -
User2142845853 posted
rogersbr
But notice that when the new Gridview is there he connects the SQL data source, pulls the connection string, table? When you click the Advanced button in MY case these 2 items are not available. Only there if the table has an ID field set as Primary Key. If its just a table of items without the standard Int ID (not null)? it does not work.I unique identifier is required to update a record. You might have to fallback to ADO.NET if a table does not have a primary key.
rogersbr
The question is, can I add an MVC page to this vb webforms project? And create a db Context class?MVC is not required to create a DbContext class. Share you code if you need community assistance.
The table being used is part of a set of tables. It seems easier IMO to utilize MVC to make a page who's sole purpose is to view, filter, add or modify sql table data. If I add an MVC page the code seems to flow better with a db context
Some questions are answered like nobody ever codes it that way; or yes mixing this code can be done
Tuesday, January 7, 2020 5:40 PM -
User475983607 posted
rogersbr
Is it flat out possible to put an MVC page in and create a db context so I can use the entity framework perhaps?Yes, scaffold an MVC controller similar to your other thread where you added Web API.
Tuesday, January 7, 2020 6:01 PM -
User2142845853 posted
So it can be done? I mean some of these items can be 'dropped in' but find out later they dont work right/obsolete.
Wednesday, January 8, 2020 5:29 PM -
User475983607 posted
So it can be done? I mean some of these items can be 'dropped in' but find out later they dont work right/obsolete.
Yes, an MVC controller can be added to a Web Forms application since both frameworks are part of ASP.NET. Web Forms is an obsolete framework so you are already working in an obsolete framework.
Wednesday, January 8, 2020 6:47 PM -
User2142845853 posted
The info I read says that to add mvc I have to FIRST create a new project for webforms/vb and then add all of the existing project folders. is that right?
Or the easier route, simply create a parallel universe where the project already has MVC installed?
Wednesday, January 8, 2020 6:49 PM -
User475983607 posted
rogersbr
The info I read says that to add mvc I have to FIRST create a new project for webforms/vb and then add all of the existing project folders. is that right?
Or the easier route, simply create a parallel universe where the project already has MVC installed?
I just right clicked the project selected Add -> New Scaffolded Item... -> MVC Controller. you can place the Class wherever you like. For example a Controller folder.
You have to add the routing. My project has a App_Start/RouteConfig.vb file. I just added the route to the RegisterRoutes method.
routes.MapRoute( name:="Default", url:="{controller}/{action}/{id}", defaults:=New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional}
To create the View I just right clicked a controller action and select Add View. I had to add an inherits and imports statement to the top of the layout and View files.
@inherits System.Web.Mvc.WebViewPage @imports System.Web.Mvc.Html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>@ViewBag.Title - My ASP.NET Application</title> @Styles.Render("~/Content/css") @Scripts.Render("~/bundles/modernizr") </head> <body> <div class="navbar navbar-inverse navbar-fixed-top"> <div class="container"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> @Html.ActionLink("Application name", "Index", "Home", New With {.area = ""}, New With {.class = "navbar-brand"}) </div> <div class="navbar-collapse collapse"> <ul class="nav navbar-nav"> </ul> </div> </div> </div> <div class="container body-content"> @RenderBody() <hr /> <footer> <p>© @DateTime.Now.Year - My ASP.NET Application</p> </footer> </div> @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/bootstrap") @RenderSection("scripts", required:=false) </body> </html>
@inherits System.Web.Mvc.WebViewPage @Code ViewData("Title") = "About" End Code <h2>About</h2>
I'm sure there's a web.config setting that you need if you do not want to add inherits and imports.
Forgot the _ViewStart
@inherits System.Web.WebPages.StartPage @Imports System.Web.MVC.Html @Code Layout = "~/Views/Shared/_Layout.vbhtml" End Code
Wednesday, January 8, 2020 7:36 PM