Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie help with datagrid 1

Status
Not open for further replies.

TimothyP

Programmer
Aug 6, 2002
135
US
Application: ASP.NET
DB: SQL Serer 2000
IDE: VS 2003

I am trying to write a simple application to Select/Insert/Update/Delete records against a SQL server table. I am trying to accomplish this task using a Datagrid.

I can display my table with the Edit & Update buttons.
However, I have to double-click the Edit button before I see the Update button. I just want this button to be a single click. Below is my code. Any ides?

Thanks in advance!
Tim

Code:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="index.aspx.vb" Inherits="MySpecialEventsCost2.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
	<HEAD>
		<title>WebForm1</title>
		<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
		<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
		<meta content="JavaScript" name="vs_defaultClientScript">
		<meta content="[URL unfurl="true"]http://schemas.microsoft.com/intellisense/ie5"[/URL] name="vs_targetSchema">
	</HEAD>
	<body>
		<form id="Form1" method="post" runat="server">
			<asp:DataGrid id="dgStatusMaster" runat="server" DataKeyField="Status_id" AutoGenerateColumns="False">
				<Columns>
					<asp:EditCommandColumn ButtonType="PushButton" UpdateText="Update" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
					<asp:ButtonColumn Text="Delete" ButtonType="PushButton" CommandName="Delete"></asp:ButtonColumn>
					<asp:TemplateColumn SortExpression="status_id" HeaderText="Status ID">
						<ItemTemplate>
							<asp:Label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.status_id") %>'>
							</asp:Label>
						</ItemTemplate>
					</asp:TemplateColumn>
					<asp:TemplateColumn SortExpression="status_description" HeaderText="status_description">
						<ItemTemplate>
							<asp:Label id=Label3 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.status_description") %>'>
							</asp:Label>
						</ItemTemplate>
						<EditItemTemplate>
							<asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ErrorMessage="The status description is empty."
								Display="None" ControlToValidate="StatusDescription">Product name is empty.</asp:RequiredFieldValidator>
							<asp:TextBox id="StatusDescription" Width="110" MaxLength="40" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.status_description") %>'>
							</asp:TextBox>
						</EditItemTemplate>
					</asp:TemplateColumn>
				</Columns>
			</asp:DataGrid></form>
	</body>
</HTML>
 
Yes.

Here you go.

Code:
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Data.Odbc


Public Class WebForm1
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents pnlStatusMaster As System.Web.UI.WebControls.Panel
    Protected WithEvents dgStatusMaster As System.Web.UI.WebControls.DataGrid

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not IsPostBack Then
            Call LoadStatusMasterGrid()
        End If
    End Sub

    Public Sub LoadStatusMasterGrid()
        Dim connection As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("sqlConn"))
        connection.Open()
        Try

            Dim command As SqlCommand = _
                New SqlCommand("usp_Select_Status_Master", connection)
            Command.CommandType = CommandType.StoredProcedure

            Dim adapter As SqlDataAdapter = New SqlDataAdapter(Command)

            Dim table As DataTable = New DataTable
            adapter.Fill(table)
            dgStatusMaster.DataSource = table
            dgStatusMaster.DataKeyField = "status_id"
            dgStatusMaster.DataBind()

        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw
        Finally
            connection.Close()
        End Try
    End Sub

    Private Sub dgStatusMaster_EditCommand(ByVal source As Object, _
    ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgStatusMaster.EditCommand

        dgStatusMaster.EditItemIndex = e.Item.ItemIndex

    End Sub

    Private Sub dgStatusMaster_CancelCommand(ByVal source As Object, _
    ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgStatusMaster.CancelCommand

        dgStatusMaster.EditItemIndex = -1

    End Sub

    'Private Sub dgStatusMaster_UpdateCommand(ByVal source As Object, _
    'ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgStatusMaster.UpdateCommand

    'End Sub

End Class
 
In the EditCommand and UpdateCommand, you need to rebind the grid after your set the index.

Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top