Welcome to WebmasterWorld Guest from 23.22.17.192

Forum Moderators: ocean10000

Message Too Old, No Replies

VB.NET DataGrid to ASP.NET

   
4:52 am on May 12, 2006 (gmt 0)

5+ Year Member



Hi everyone,

I wrote a Windows VB.NET application that uses SQL Server 2000. The application displays database records in a Data Grid element.

I would like to turn this into a Web application, and again, use Data Grid to display the records in the browser.

I have it working properly using a List Box element, but when I use the Data Grid nothing is displayed.

Am I missing some parameters, declarations or..?

6:32 am on May 12, 2006 (gmt 0)

10+ Year Member



Can you paste the code that you are having problems with? It would be a lot easier to help you with this.
8:07 am on May 12, 2006 (gmt 0)

5+ Year Member



Inherits System.Web.UI.Page
Dim connstr As String = "data source=SERVER;initial catalog=DB;user id=USER;password=PASSWORD;"
Dim sqlStr As String = "SELECT * FROM Users"
Dim dt As New DataTable

Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
dt.Clear()
Dim dataAdapter As New SqlClient.SqlDataAdapter(sqlStr, connstr)
dataAdapter.Fill(dt)
dataAdapter.Dispose()
dgDisplay.DataSource = dt
End Sub

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim changes As Integer
Dim dataAdapter As New SqlClient.SqlDataAdapter(sqlStr, connstr)
Dim commandBuilder As New SqlClient.SqlCommandBuilder(dataAdapter)
changes = dataAdapter.Update(dt)
dataAdapter.Dispose()
If changes > 0 Then
MsgBox(changes & " changed rows were stored in the database.")
Else
MsgBox("no changes made.")
End If
End Sub

9:44 pm on May 12, 2006 (gmt 0)

10+ Year Member



I believe you need to databind the datasource.

dgDisplay.Databind

11:51 pm on May 12, 2006 (gmt 0)

5+ Year Member



Thanks Nige! It displays the records properly, but it doesn't allow for direct editing of a particular record through the Data Grid.

Is it possible to make the Web Application Data Grid identical to the Windows Application Data Grid(ie. to have the same functionality)?

If so, would it require a change in the code or is it a configuration of the Data Grid element's parameters?

12:47 pm on May 15, 2006 (gmt 0)

10+ Year Member



Well one thing that you would have to do is add a textbox control or whatever you want to the datagrid. (I don't have vs.net 2003 installed on this computer, sorry) Then you would iterate through all the items of the datagrid on save click.
Datagrid Iteration [xmlpitstop.com] has a good expample on how to do that.

I was looking at the code, and at the bottom when the example shows how to use the findcontrol method, I like to use DirectCast instead of the older CType, it is a bit faster, IMO.

8:25 pm on May 15, 2006 (gmt 0)

10+ Year Member



I suggest doing a search on the web for 4GuysFromRolla web site. They have something like a 17 part tutorial for the DataGrid control.

The web control is similar in name to the Forms control, but they are a little different in what they do and how it works.