Editable rows....... for datagrid....

N

n newton

Heres one
<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1"
%>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OLeDb" %>
<script runat="server">

dim Conn as new
OleDbConnection(ConfigurationSettings.AppSettings("connection_users"))

sub Page_Load(Sender as Object, e as EventArgs)

FillDataGrid()

end sub



sub dgData_Edit(sender as Object, e as DataGridCommandEventArgs)
FillDataGrid(e.Item.ItemIndex)
end sub

sub dgData_Delete(sender as Object, e as DataGridCommandEventArgs)
dim strSQL as string = "DELETE FROM users WHERE userid = " &
Ctype(e.Item.Cells(0).Controls(1), Label).text



ExecuteStatement(strSQL)

FillDataGrid()
end sub

sub dgData_Update(Sender as Object, e as DataGridCommandEventArgs)
if UpdateDataStore(e) then
FillDataGrid(-1)
end if
end sub

sub dgData_Cancel(Sender as Object, e as DataGridCommandEventArgs)
FillDataGrid(-1)
end sub

sub dgData_PageIndexChanged(Sender as Object, e as
DataGridPageChangedEventArgs)
dgData.CurrentPageIndex = e.NewPageIndex
dgData.DataBind()
end sub

function UpdateDataStore(e as DataGridCommandEventArgs) as boolean

dim i, j as integer
dim params(9) as string
dim strText as string
dim blnGo as boolean = True

j = 0

for i = 1 to e.Item.Cells.Count - 3
strText = Ctype(e.Item.Cells(i).Controls(0),
TextBox).Text
if strText <> "" then
params(j) = strText
j = j + 1
else
blnGo = False
lblMessage.Text = lblMessage.Text & "You
forgot to enter a value<p>"
end if

next

if not blnGo then
return false
exit function
end if

dim objUser as new console.User
dim objUserDetails as new console.UserDetails
objUser.Connection_String =
ConfigurationSettings.AppSettings("connection_users")


with objUserDetails
.userid = Ctype(e.Item.Cells(0).Controls(1), Label).Text
.username = params(0)
.password = params(1)
.firstname = params(2)
.lastname = params(3)
.email = params(4)
.telephone = params(5)
.organisation = params(6)
.choice = params(7)
.active = params(8)
end with
objUser.UpdateDetails(objUserDetails)
return blnGo

end function

sub FillDataGrid(Optional EditIndex as integer =- 1)
dim objCmd as new OleDbCommand("select * from
users", Conn)



objCmd.Connection.Open()
Dim myAdapter as New OleDbDataAdapter(objcmd)
Dim ds as New DataSet()
myAdapter.Fill(ds)



dgData.DataSource = ds
if not EditIndex.Equals(Nothing) then
dgData.EditItemIndex = editIndex
end if

dgData.DataBind()


objCmd.Connection.Close()

end sub

function ExecuteStatement(strSQL)

dim objCmd as new OleDbCommand(strSQL, Conn)

try
objCmd.Connection.Open()
objCmd.ExecuteNonQuery()
catch ex as exception
lblMessage.Text = "Error updating the database"
end try

objCmd.Connection.Close()

end function

</script>
<html>
<head>
<title>The Knowledge Network</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="../images/emraf.css">
</head>
<body bgcolor="#FFFFFF">

<!--Main content cell-->
<table width="100%" height="100%" border="0" cellspacing="10">
<!--DWLayoutTable-->
<tr>
<td width="33%" height="282" valign="top">
<font face="arial" size="3"><b>User Administration</b></font>
<br><a href="admin.aspx">Back to Admin Control Panel</a>
<!--News Cell-->
<asp:Label id="lblMessage" runat="server"/>
<form runat="server">

<asp:DataGrid id="dgData" runat="server"
BorderColor="Black" Gridlines="Both"
AllowPaging="true" PageSize="5" PagerStyle-Mode="NumericPages"
ItemStyle-Font-Name="Verdana" ItemStyle-Font-Size="8"
EditItemStyle-Font-Name="Verdana" EditItemStyle-Font-Size="8"
cellpadding="4" CellSpacing="0" width="100%"
AutoGenerateColumns="False"
OnDeleteCommand="dgData_Delete"
OnEditCommand="dgData_Edit"
OnCancelCommand="dgData_Cancel"
OnUpdateCommand="dgData_Update"
OnPageIndexChanged="dgData_PageIndexChanged">

<Columns>
<asp:templatecolumn HeaderText="ID">
<Itemtemplate>
<asp:label id="Name" runat="server"
Text='<%# Container.DataItem("userid") %>' />
</ItemTemplate>
</asp:templateColumn>
<asp:BoundColumn HeaderText="Username"
DataField="username" />
<asp:boundcolumn HeaderText="Password"
DataField="password" />
<asp:boundcolumn HeaderText="First Name"
DataField="firstname" />
<asp:boundcolumn HeaderText="Last Name"
DataField="lastname" />
<asp:boundcolumn HeaderText="E-mail"
DataField="email" />
<asp:boundcolumn HeaderText="Telephone"
DataField="telephone" />
<asp:boundcolumn HeaderText="Organisation"
DataField="organisation" />
<asp:boundcolumn HeaderText="Choices"
DataField="choice" />
<asp:boundcolumn HeaderText="Active"
DataField="active" />
<asp:EditCommandColumn
EditText="Edit"
CancelText="Cancel"
UpdateText="Update"
HeaderText="Edit" />
<asp:buttoncolumn HeaderText="" text="Delete"
CommandName="delete" /> </Columns>
</asp:DataGrid>

</form></td>
</tr>
</table>
<font size="1" face="Courier New, Courier, mono">
<!--end main content cell-->
</font><font size="1" face="Geneva, Arial, Helvetica,
san-serif">&nbsp; </font></td>
</tr>
</table>

</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,079
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top