Link to home
Start Free TrialLog in
Avatar of JB4375
JB4375Flag for United States of America

asked on

Making an ASP Datagrid Fully Editable

I've got a SQL database popluating from a form. Below that I have a datagrid that shows the contents of the database. This is simply being pulled using a sub on the VB side. I need to be able to edit the info within the datagrid.

I found plenty of examples, however some recommend a bound column, others a template column, and they're all using the itemtemplate within and that results in the following error: "validation xhtml 1.0 transitional element itemtemplate not supported"

I've removed the statement that was giving me the errors. I need someone to show me how to setup the columns so this will work.

Thanks in advance.
default.aspx:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Theme="Theme1" Inherits="add_account_excel._Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Zone Manager New Department Request Form </title>
    <link href="default.css" rel="stylesheet" type="text/css" />
    <meta content="True" name="vs_snapToGrid" />
	<meta content="True" name="vs_showGrid" />
    <meta content="JavaScript" name="vs_defaultClientScript" />
</head>
<body>
    <form id="form1" method="post" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    
        <div class="header_container">
            <asp:Image ID="Logo" runat="server" ImageUrl="~/images/wfubmclogo.jpg" />
            <asp:Label ID="lblGold" runat="server" Text="zone manager"></asp:Label>
            <asp:Label ID="lblBlack" runat="server" Text="New Department Request Form"></asp:Label>     
	    </div><!-- end #header-containter -->
	    
    <div id="wrapper">
 
    <div id="content">
        <div class="main-container">
            <div id="right"> 
                <asp:Label ID="Label2" runat="server" Text="Department Name:"></asp:Label><br />
                <asp:TextBox ID="TextBox2" tabindex="2" runat="server"></asp:TextBox><br />
                <asp:Label ID="label19" runat="server" Text="Example: Dermatology" Font-Size="Smaller"></asp:Label><br /><br />
                
                <asp:Label ID="Label4" runat="server" Text="Department OU:"></asp:Label><br />
                <asp:TextBox ID="TextBox4" tabindex="4" runat="server"></asp:TextBox><br />
                <asp:Label ID="label21" runat="server" Text="Example: Derm_1530_Dermatology" Font-Size="Smaller"></asp:Label><br /><br />
              
                <asp:Label ID="Label6" runat="server" Text="Groups:"></asp:Label><br />
                <asp:TextBox ID="TextBox6" tabindex="6" runat="server"></asp:TextBox><br />
                <asp:Label ID="label23" runat="server" Text="Example: Dermatology" Font-Size="Smaller"></asp:Label><br /><br />
                          
                <asp:Label ID="Label8" runat="server" Text="Admin Contact Phone:"></asp:Label><br />
                <asp:TextBox ID="TextBox8" tabindex="8" runat="server"></asp:TextBox><br /><br />
               
                <asp:Label ID="Label10" runat="server" Text="Zone Manager:"></asp:Label><br />
                <asp:TextBox ID="TextBox10" tabindex="10" runat="server"></asp:TextBox><br /><br />  
                <div class="right-container">Right</div>
            </div> <!-- end #right -->    
                        
             <div id="left">  
                <asp:Label ID="Label1" runat="server" Text="Department Number:"></asp:Label><br />
                <asp:TextBox ID="TextBox1" tabindex="1" runat="server"></asp:TextBox><br />
                <asp:Label ID="label18" runat="server" Text="Example: 153000" Font-Size="Smaller"></asp:Label><br /><br />
                
                <asp:Label ID="Label3" runat="server" Text="Institution OU:"></asp:Label><br />
                <asp:TextBox ID="TextBox3" tabindex="3" runat="server"></asp:TextBox><br />
                <asp:Label ID="label20" runat="server" Text="Example: School or Hospital" Font-Size="Smaller"></asp:Label><br /><br />
                
                <asp:Label ID="Label5" runat="server" Text="Home Directory Location:"></asp:Label><br />
                <asp:TextBox ID="TextBox5" tabindex="5" runat="server"></asp:TextBox><br />
                <asp:Label ID="label22" runat="server" Text="Example: \\csb1\csderm$" Font-Size="Smaller"></asp:Label><br /><br />
                
                <asp:Label ID="Label7" runat="server" Text="Admin Contact Name:"></asp:Label><br />
                <asp:TextBox ID="TextBox7" tabindex="7" runat="server"></asp:TextBox><br /><br />
                
                <asp:Label ID="Label9" runat="server" Text="Admin Contact Email:"></asp:Label><br />
                <asp:TextBox ID="TextBox9" tabindex="9" runat="server"></asp:TextBox><br /><br />    
                <div class="left-container">LEFT</div>   
            </div> <!-- end #left-->    
        </div> <!-- end class main-container -->   
        
        <div class="bottom-container">            
                <div class="label-container"> 
                    <asp:Label ID="lblResults" runat="server"></asp:Label>                
                </div> <!-- end class label-container -->
                                
                <div class="image-container"> 
                    <asp:Image ID="imgCheckMark" runat="server" 
                        ImageUrl="~/images/checkmark_Bold_Brush_Green.png" Height="30px"/>
                    <asp:Image ID="imgRedX" runat="server" ImageUrl="~/images/red_X.jpg" Height="30px"/>
                </div><!-- end class image container --> 			
            
                <div class="button-container">
                    <asp:Button ID="Clear" tabindex="-1" runat="server" Text="Clear" />
                    <asp:Button ID="Submit" tabindex="-1" runat="server" Text="Submit" />
                    <asp:Button ID="View" tabindex="-1" runat="server" Text="View File" />
                </div><!-- end button-container -->                
        </div> <!-- end class bottom-container -->          
            
        
   </div><!-- end #content -->
 
	        <div id="footer">
                <asp:Label ID="Label17" runat="server" Text="  Requested by:  "></asp:Label>
                <asp:Label ID="Label16" runat="server"></asp:Label><br /><br /><br />
	        </div> 
</div><!-- end #wrapper -->	        
	     <div class="datagrid-container">
	       <div>
                <asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 10px;"
				runat="server" ForeColor="Black" ShowFooter="True" CellPadding="4" 
                BackColor="White" BorderWidth="1px"
				BorderStyle="None" BorderColor="#CCCCCC" Height="135px" >
				<SelectedItemStyle Font-Bold="True" Wrap="False" ForeColor="White" BackColor="#CC3333"></SelectedItemStyle>
				<EditItemStyle Wrap="False"></EditItemStyle>
				<AlternatingItemStyle Font-Bold="True" Wrap="False" ForeColor="Black" BackColor="#CCCC99"></AlternatingItemStyle>
				<ItemStyle Wrap="False"></ItemStyle>
				<HeaderStyle Font-Bold="True" Wrap="False" ForeColor="White" BackColor="#333333"></HeaderStyle>
				<FooterStyle Wrap="False" ForeColor="White" BackColor="#333333"></FooterStyle>
				<Columns>
				    <asp:ButtonColumn Text="Edit" CommandName="Edit"></asp:ButtonColumn>
				</Columns>
				<PagerStyle HorizontalAlign="Right" ForeColor="Black" BackColor="White" Wrap="False"></PagerStyle>
		        </asp:datagrid>
		    </div> 
		  </div> <!-- end datagrid-container --> 
               
    </form>
</body>
</html>
default.aspx.vb
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.DirectoryServices
Imports System.Collections
Imports System.Collections.Specialized
 
Namespace add_account_excel
 
    Partial Public Class _Default
        Inherits System.Web.UI.Page
        Dim strUser As String
        Dim strDate As String
        Dim DS As System.Data.DataSet
 
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            lblResults.Visible = False
            imgCheckMark.Visible = False
            imgRedX.Visible = False
            strUser = (Request.ServerVariables("AUTH_USER"))
 
            If InStr(strUser, "domain\") Then
                strUser = Replace(strUser, "domain\", "")
            End If
 
            If InStr(strUser, "domain\") Then
                strUser = Replace(strUser, "domain\", "")
            End If
 
            Label16.Text = strUser
 
            Call getDatafromfile()
        End Sub
 
        Sub getDatafromfile()
            Dim sConn As SqlConnection
            Dim sComm As SqlCommand
            Dim sAdapt As SqlDataAdapter
 
            sConn = New Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("MyConnection").ConnectionString)
            sComm = New SqlCommand("select * from Automated_ZM_Dept_Request", sConn)
            sAdapt = New SqlDataAdapter(sComm)
            sConn.Open()
 
            DS = New DataSet
            sAdapt.Fill(DS)
            DataGrid1.DataSource = DS
            DataGrid1.DataBind()
            sConn.Close()
        End Sub
 
        Protected Sub Submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Submit.Click
            Dim strDate As String
            strDate = DateString & "@" & TimeString
 
            If TextBox1.Text = "" Then
                lblResults.Visible = True
                imgCheckMark.Visible = False
                imgRedX.Visible = True
                lblResults.Text = "Please enter 'Department ID'"
            ElseIf TextBox2.Text = "" Then
                lblResults.Visible = True
                imgCheckMark.Visible = False
                imgRedX.Visible = True
                lblResults.Text = "Please enter 'Department Name'"
            ElseIf TextBox3.Text = "" Then
                lblResults.Visible = True
                imgCheckMark.Visible = False
                imgRedX.Visible = True
                lblResults.Text = "Please enter 'Institution OU'"
            ElseIf TextBox4.Text = "" Then
                lblResults.Visible = True
                imgCheckMark.Visible = False
                imgRedX.Visible = True
                lblResults.Text = "Please enter 'Department OU'"
            ElseIf TextBox5.Text = "" Then
                lblResults.Visible = True
                imgCheckMark.Visible = False
                imgRedX.Visible = True
                lblResults.Text = "Please enter 'Home Directory Location'"
            ElseIf TextBox6.Text = "" Then
                lblResults.Visible = True
                imgCheckMark.Visible = False
                imgRedX.Visible = True
                lblResults.Text = "Please enter 'Groups'"
            ElseIf TextBox7.Text = "" Then
                lblResults.Visible = True
                imgCheckMark.Visible = False
                imgRedX.Visible = True
                lblResults.Text = "Please enter 'Admin Contact'"
            ElseIf TextBox8.Text = "" Then
                lblResults.Visible = True
                imgCheckMark.Visible = False
                imgRedX.Visible = True
                lblResults.Text = "Please enter 'Phone Number'"
            ElseIf TextBox9.Text = "" Then
                lblResults.Visible = True
                imgCheckMark.Visible = False
                imgRedX.Visible = True
                lblResults.Text = "Please enter 'Contact Email'"
            ElseIf TextBox10.Text = "" Then
                lblResults.Visible = True
                imgCheckMark.Visible = False
                imgRedX.Visible = True
                lblResults.Text = "Please enter 'Zone Manager'"
            Else
                lblResults.Visible = True
                imgCheckMark.Visible = True
                imgRedX.Visible = False
                lblResults.Text = "New Department Request Submitted"
                Call Add_SQL()
            End If
 
        End Sub
 
        Public Sub Add_SQL()
 
            Dim sConn As Data.SqlClient.SqlConnection
            Dim sComm As Data.SqlClient.SqlCommand
            Dim sAdapt As Data.SqlClient.SqlDataAdapter
 
            sConn = New Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("MyConnection").ConnectionString)
            sComm = New Data.SqlClient.SqlCommand("INSERT INTO Automated_ZM_Dept_Request (DeptID, Dept, DeptOU, InstOU, HomeDir, Groups, Contact, Phone, Email, ZoneMgr, Submitted, Requestor) VALUES ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "','" & TextBox6.Text & "','" & TextBox7.Text & "','" & TextBox8.Text & "','" & TextBox9.Text & "','" & TextBox10.Text & "','" & strDate & "','" & strUser & "')", sConn)
            sAdapt = New Data.SqlClient.SqlDataAdapter(sComm)
            sConn.Open()
            sComm.ExecuteNonQuery()
            sConn.Close()
 
            clrFields()
            getDatafromfile()
        End Sub
 
        Sub clrFields()
            TextBox1.Text = ""
            TextBox2.Text = ""
            TextBox3.Text = ""
            TextBox4.Text = ""
            TextBox5.Text = ""
            TextBox6.Text = ""
            TextBox7.Text = ""
            TextBox8.Text = ""
            TextBox9.Text = ""
            TextBox10.Text = ""
            lblResults.Visible = True
            imgCheckMark.Visible = True
            imgRedX.Visible = False
            lblResults.Text = "New Department Request Submitted"
 
        End Sub
 
        Protected Sub Clear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Clear.Click
            TextBox1.Text = ""
            TextBox2.Text = ""
            TextBox3.Text = ""
            TextBox4.Text = ""
            TextBox5.Text = ""
            TextBox6.Text = ""
            TextBox7.Text = ""
            TextBox8.Text = ""
            TextBox9.Text = ""
            TextBox10.Text = ""
            lblResults.Visible = False
            imgCheckMark.Visible = False
            imgRedX.Visible = False
 
        End Sub
 
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles View.Click
            lblResults.Text = Nothing
            getDatafromfile()
        End Sub
  End Class
End Namespace

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of samtran0331
samtran0331
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JB4375

ASKER

ASP. Net 2.0 is correct.
That's funny because I actually started with gridview, and then after some research, the examples I saw were using datagrid. I changed it after coming to the obviously incorrect conclusion that gridview meant just that: viewing, not accessing.
Thanks!!
When you say "Fully Editable"...do you want the entire grid (all rows) to update with one button click, or for each row to have an "edit/save" feature?...I might be able to help you find a better tutorial/walkthrough...
Avatar of JB4375

ASKER

I think each row should have the edit/save feature. The idea is that they will use the form to submit new departments. In the event that some information changes they will use the data grid to edit contact information, or whatever, for that department.