Results 1 to 2 of 2

Thread: datagrid help

  1. #1

    Thread Starter
    Frenzied Member EyeTalion's Avatar
    Join Date
    Jul 2000
    Location
    New York
    Posts
    1,075

    datagrid help

    What I need to do is just display some read only data from a database and/or another form. Does anyone have some sample code that will get me started?

    thanks,
    eye

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    this is for the asp .net datagrid, but the main code is exactly the ame, just remove the html and asp stuff, and remove the .DataBind method

    Code:
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.OleDb"%> 
    
    <html>
    	<script language="VB" runat="server">
    		' ---------------------------------
    		' Code by Chris Andersen
    		' This example shows how to show data from an access database in the DataGrid control
    		' and how to write a new record to the database.
    		' ---------------------------------
    		
    		' Recordset no longer exists for ADO .NET. Create DataSets and populate it from the data you get from
    		' a DataAdapter or other source. In this case we are getting ADO DataSet using the System.Data.OleDb.OleDbDataAdapter Class
    		Dim myDS As New DataSet
    		Dim myConn As OleDbConnection
    		Dim myCommand As OleDbDataAdapter
    		Dim strConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" & Server.Mappath("/candersen/db/test.mdb")
    		Dim strSQL As String
    		
    		Sub Page_Load(Sender As Object, E As EventArgs)
    			' This sub is called everytime the Page Loads
    		End Sub
    		
    		Sub Go_Click(Sender As Object, E As EventArgs)
    			Dim strName As String = Name.Text
    			Dim strAge As String = Age.Text
    			
    			' Write posted data to the lables
    			lblName.Text = strName
    			lblAge.Text = strAge
    
    			' Insert Posted Data to database
    			strSQL = "Insert Into Test(Name,Age) VALUES('" & strName & "','" & strAge & "')" 
    			
    			' Populate DataSet
    			FillDataSet()
    			
    			' Update DataGrid
    			UpdateGrid()
    		End Sub
    		
    		Sub UpdateGrid()
    			' Updates the DataGrid
    			strSQL = "Select * FROM Test"
    			
    			'Populate DataSet
    			FillDataSet()
    			
    			' Now set the DataGrid's source to the Dataset and bind it
    			MyDataGrid.DataSource = myDS.Tables("Test").DefaultView
    			MyDataGrid.DataBind()
    			
    		End Sub
    		
    		Sub FillDataSet()
    			' Reusable code to populate the Dataset from the SQL statement
    			'Start Connection to ADO source
    			myConn = New OleDbConnection (strConnString)
    		
    			' Run the Sql statement and get the Data into the DataAdapter
    			myCommand = New OleDbDataAdapter(strSQL, myConn)
    
    			' Fill the DataSet and name the table
    			myCommand.Fill(myDS, "Test")
    			         
    			myConn.Close()
    		End Sub
    		
    	</script>
    	<body>
    		<form action="aspnet1.aspx" method="post" runat="server">
    			<!-- ASP .NET server control textbox  -->
    			<table>
    				<tr>
    					<td>Name</td>
    					<td><asp:textbox BackColor="#ccccff" id="Name" 
    						BorderColor="black"
    						runat="server"/></td>
    				</tr>
    				<tr>
    					<td>Age</td>
    					<td><asp:textbox BackColor="#ccccff" id="Age"
    						BorderColor="black"
    						runat="server"/></td>
    				</tr>
    			</table>
    					
    			<!-- ASP .NET sever control button -->
    			<asp:button id="btnGo" text="Go" OnClick="Go_Click" runat="server"/><br/>
    			
    			<!-- ASP .NET server control Label-->
    			Your name is <asp:label id="lblName" runat="server"/><br/>
    			Your age is <asp:label id="lblAge" runat="server"/><br/>
    			
    			<!-- ASP .NET server control DataGrid -->
    			<asp:DataGrid id="MyDataGrid" runat="server"
    			    Width="700"
    			    BackColor="#ccccff" 
    			    BorderColor="black"
    			    ShowFooter="false" 
    			    CellPadding=3 
    			    CellSpacing="0"
    			    Font-Name="Verdana"
    			    Font-Size="8pt"
    			    HeaderStyle-BackColor="#aaaadd"
    			    EnableViewState="false">
    			  
    			  	<Columns>
    				<asp:HyperLinkColumn
    				            DataNavigateUrlField="Name"
    				            DataNavigateUrlFormatString="test_details.aspx?id={0}"
    				            Text="View"
    				          />
    				</Columns>
    			  </asp:DataGrid>
    		</form>
    	</body>
    </html>
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width