A
A P
Hi!
I have created a sample datagrid that can update data on a database. Please
help me solve the problem, I have attached both aspx and code behind
(aspx.vb):
______________________
datagrid.aspx
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="DataGrid.aspx.vb"
Inherits="vs_2310.DataGrid"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>DataGrid</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<aspataGrid id=DataGrid1 style="Z-INDEX: 101; LEFT: 51px; POSITION:
absolute; TOP: 51px" runat="server" DataSource="<%# DataSet21 %>"
DataKeyField="NounID" BorderColor="#CC9966" BorderStyle="None"
BorderWidth="1px" BackColor="White" CellPadding="4" Font-Size="10pt"
AutoGenerateColumns="False" Font-Names="Arial" AllowPaging="True">
<SelectedItemStyle Font-Bold="True" ForeColor="#663399"
BackColor="#FFCC66"></SelectedItemStyle>
<ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#FFFFCC"
BackColor="#990000"></HeaderStyle>
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="NounID" SortExpression="NounID"
HeaderText="ID"></asp:BoundColumn>
<asp:BoundColumn DataField="NounDesc" SortExpression="NounDesc"
HeaderText="Noun"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Action">
<ItemTemplate>
<asp:LinkButton runat="server" Text="Edit" CommandName="Edit"
CausesValidation="false"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton runat="server" Text="Update"
CommandName="Update"></asp:LinkButton>
<asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel"
CausesValidation="false"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="#330099"
BackColor="#FFFFCC"></PagerStyle>
</aspataGrid>
<asp:Label id="Label1" style="Z-INDEX: 102; LEFT: 51px; POSITION:
absolute; TOP: 17px" runat="server">Label</asp:Label>
</form>
</body>
</HTML>
________________________
datagrid.aspx.vb
Imports System.Data.OleDb
Public Class DataGrid
Inherits System.Web.UI.Page
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
OleDbDataAdapter1.Fill(DataSet21)
DataGrid1.DataBind()
End Sub
Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.EditCommand
DataGrid1.EditItemIndex() = e.Item.ItemIndex
DataGrid1.DataBind()
End Sub
Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.UpdateCommand
Dim NounTable As DataSet2.tbltsNounDataTable
NounTable = DataSet21.tbltsNoun
Dim rowToUpdate As DataSet2.tbltsNounRow
rowToUpdate = NounTable.Rows(e.Item.ItemIndex)
rowToUpdate.NounDesc = CType(e.Item.Cells(1).Controls(0),
TextBox).Text
OleDbDataAdapter1.Update(DataSet21)
Label1.Text = rowToUpdate.NounDesc
DataGrid1.EditItemIndex = -1
DataGrid1.DataBind()
End Sub
Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal e
As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles
DataGrid1.PageIndexChanged
DataGrid1.CurrentPageIndex = e.NewPageIndex
DataGrid1.DataBind()
End Sub
Private Sub DataGrid1_CancelCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.CancelCommand
DataGrid1.EditItemIndex = -1
DataGrid1.DataBind()
End Sub
End Class
___________________
I have created a sample datagrid that can update data on a database. Please
help me solve the problem, I have attached both aspx and code behind
(aspx.vb):
______________________
datagrid.aspx
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="DataGrid.aspx.vb"
Inherits="vs_2310.DataGrid"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>DataGrid</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<aspataGrid id=DataGrid1 style="Z-INDEX: 101; LEFT: 51px; POSITION:
absolute; TOP: 51px" runat="server" DataSource="<%# DataSet21 %>"
DataKeyField="NounID" BorderColor="#CC9966" BorderStyle="None"
BorderWidth="1px" BackColor="White" CellPadding="4" Font-Size="10pt"
AutoGenerateColumns="False" Font-Names="Arial" AllowPaging="True">
<SelectedItemStyle Font-Bold="True" ForeColor="#663399"
BackColor="#FFCC66"></SelectedItemStyle>
<ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#FFFFCC"
BackColor="#990000"></HeaderStyle>
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="NounID" SortExpression="NounID"
HeaderText="ID"></asp:BoundColumn>
<asp:BoundColumn DataField="NounDesc" SortExpression="NounDesc"
HeaderText="Noun"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Action">
<ItemTemplate>
<asp:LinkButton runat="server" Text="Edit" CommandName="Edit"
CausesValidation="false"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton runat="server" Text="Update"
CommandName="Update"></asp:LinkButton>
<asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel"
CausesValidation="false"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="#330099"
BackColor="#FFFFCC"></PagerStyle>
</aspataGrid>
<asp:Label id="Label1" style="Z-INDEX: 102; LEFT: 51px; POSITION:
absolute; TOP: 17px" runat="server">Label</asp:Label>
</form>
</body>
</HTML>
________________________
datagrid.aspx.vb
Imports System.Data.OleDb
Public Class DataGrid
Inherits System.Web.UI.Page
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
OleDbDataAdapter1.Fill(DataSet21)
DataGrid1.DataBind()
End Sub
Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.EditCommand
DataGrid1.EditItemIndex() = e.Item.ItemIndex
DataGrid1.DataBind()
End Sub
Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.UpdateCommand
Dim NounTable As DataSet2.tbltsNounDataTable
NounTable = DataSet21.tbltsNoun
Dim rowToUpdate As DataSet2.tbltsNounRow
rowToUpdate = NounTable.Rows(e.Item.ItemIndex)
rowToUpdate.NounDesc = CType(e.Item.Cells(1).Controls(0),
TextBox).Text
OleDbDataAdapter1.Update(DataSet21)
Label1.Text = rowToUpdate.NounDesc
DataGrid1.EditItemIndex = -1
DataGrid1.DataBind()
End Sub
Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal e
As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles
DataGrid1.PageIndexChanged
DataGrid1.CurrentPageIndex = e.NewPageIndex
DataGrid1.DataBind()
End Sub
Private Sub DataGrid1_CancelCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.CancelCommand
DataGrid1.EditItemIndex = -1
DataGrid1.DataBind()
End Sub
End Class
___________________