Modificar registro en el datagrid

25/06/2004 - 22:22 por Dayana | Informe spam
Hola, amigos
por favor queria saber como puedo mostrar un registro que modifique en el
datagrid. me explico
cuando el usuario modifique el registro se pueda actualizar y de una vez
mostrar en el datagrid.

Muchas Gracias.
 

Leer las respuestas

#1 Franco Figún
25/06/2004 - 22:30 | Informe spam
Te envio un ejemplo completo de eso:

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OLEDB" %>
<HTML>
<HEAD>
<title>Agenda</title>
<script language="VB" runat="server">

Dim SortField As String = ""
Protected Sub BindGrid()
Dim mySQL As String = "SELECT * FROM comentariolibros order by id"
Dim myConnection As New
OleDbConnection("PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=" &
Server.MapPath("base.mdb"))
Dim myDataAdapter As New OleDbDataAdapter(mySQL, myConnection)
Dim myDataSet As New DataSet()
Try
myDataAdapter.Fill(myDataSet, "comentariolibros")
Dim myDataView As New DataView(myDataSet.Tables("comentariolibros"))
myDataView.Sort = SortField
DataGrid1.DataSource = myDataView
DataGrid1.DataBind()
Catch Ex As Exception: Response.Write("Exception: " &
Ex.Message.ToString())
Catch OleDbEx As OleDbException: Response.Write("OleDb Exception: " &
OleDbEx.Message.ToString())
Finally
If (myConnection.State = ConnectionState.Open) Then
myConnection.Close()
End If
End Try
End Sub

Private Sub DataGrid1_EditCommand(Sender As Object, E As
DataGridCommandEventArgs)
DataGrid1.EditItemIndex = e.Item.ItemIndex
BindGrid()
End Sub

Private Sub DataGrid1_UpdateCommand(Sender As Object, E As
DataGridCommandEventArgs)
Dim strConn as string ="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" &
server.mappath("base.mdb") & ";"
Dim conn As OLEDBConnection
Dim cmd As OLEDBCommand
conn = New OLEDBConnection(strConn)
conn.Open()
cmd = New OLEDBCommand()

Dim titulolibro, autor, tema, isbn, comentario, puntaje as String
Dim id As Integer

id = DataGrid1.DataKeys(CInt(E.Item.ItemIndex))
titulolibro = CType(e.Item.FindControl("titulolibro"), TextBox).Text
autor = CType(e.Item.FindControl("autor"), TextBox).Text
tema = CType(e.Item.FindControl("tema"), TextBox).Text
isbn = CType(e.Item.FindControl("isbn"), TextBox).Text
comentario = CType(e.Item.FindControl("comentario"), TextBox).Text
puntaje = CType(e.Item.FindControl("puntaje"), TextBox).Text
cmd.CommandText = "update comentariolibros set titulolibro='" & titulolibro
& "', autor='" & autor & "', tema='" & tema & "', isbn='" & isbn & "',
comentario='" & comentario & "', puntaje='" & puntaje & "' where id=" & id

cmd.Connection = conn
cmd.ExecuteNonQuery()
cmd.connection.close
DataGrid1.EditItemIndex = -1
BindGrid()
End Sub

Private Sub DataGrid1_CancelCommand(Sender As Object, E As
DataGridCommandEventArgs)
DataGrid1.EditItemIndex = -1
BindGrid()
End Sub

Sub DataGrid1_Delete(Sender As Object, E As DataGridCommandEventArgs)
Dim strConn as string ="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" &
server.mappath("base.mdb") & ";"
Dim conn As OLEDBConnection
Dim cmd As OLEDBCommand
conn = New OLEDBConnection(strConn)
conn.Open()
cmd = New OLEDBCommand()
Dim id As Integer
id = DataGrid1.DataKeys(CInt(E.Item.ItemIndex))
cmd.CommandText = "Delete from comentariolibros where id=" & id
cmd.Connection = conn
cmd.ExecuteNonQuery()
cmd.connection.close
DataGrid1.EditItemIndex = -1
BindGrid()
End Sub

sub Insert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim strConn as string ="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" &
server.mappath("base.mdb") & ";"
Dim conn As OLEDBConnection
conn = New OLEDBConnection(strConn)
Dim cmdinsert As OLEDBCommand

cmdInsert = New OLEDBCommand("Insert Into comentariolibros (titulolibro,
autor, tema, isbn, comentario, puntaje)
values('"&request.form("titulolibro")&"', '"&request.form("autor")&"',
'"&request.form("tema")&"','"&request.form("isbn")&"','"&request.form("comen
tario")&"','"&request.form("puntaje")&"')", conn)

conn.Open()
cmdInsert.ExecuteNonQuery()
conn.close
Response.Redirect("abmlibros.aspx")
end sub

Sub Page_Load(Sender As Object, e As Eventargs)
If Not IsPostBack Then
DataGrid1.VirtualItemCount = 100
DataGrid1.PageSize = 5
DisplayCustomerCount()
BindGrid()
End If
End Sub

Sub DataGrid1_PageIndexChanged(ByVal sender As Object, ByVal e As
DataGridPageChangedEventArgs)
DataGrid1.CurrentPageIndex = e.NewPageIndex
BindGrid()
End Sub

Sub DisplayCustomerCount()
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " &
Server.MapPath("base.mdb")
Dim cmd As New OleDbCommand("Select Count(id) From comentariolibros", New
OleDbConnection(strConn))
cmd.Connection.Open()
lbCount.Text = cmd.ExecuteScalar()
cmd.Connection.Close()
End Sub
</script>
</HEAD>
<body>
<form runat="server">
<asp:Label id="Label1" runat="server">Total de registros:</asp:Label>
<asp:label id="lbCount" runat="server"></asp:label><br/>
<ASP:DataGrid id="DataGrid1" runat="server" Width="400px" border="0"
bgcolor="#ccFFFF" showheader="False"
CellPadding="3" CellSpacing="1" Font-Name="Verdana" Font-Size="8pt"
Autogeneratecolumns="False"
Headerstyle-BackColor="lightblue" Headerstyle-Font-Size="10pt"
Headerstyle-Font-Style="bold" MaintainState="false"
OnEditCommand="DataGrid1_EditCommand"
OnCancelCommand="DataGrid1_CancelCommand"
OnUpdateCommand="DataGrid1_UPDATECommand" OnDeleteCommand="DataGrid1_Delete"
DataKeyField="id"
ItemStyle-VerticalAlign="Top" BorderStyle="Ridge" GridLines="None"
BorderWidth="2px" BorderColor="White"
BackColor="White" Font-Names="Verdana"
AllowPaging="True"
PageSize=2
PagerStyle-Mode=NumericPages
PagerStyle-PageButtonCount = 10
OnPageIndexChanged="DataGrid1_PageIndexChanged"
AllowSorting="True">
<FooterStyle ForeColor="Black" BackColor="#C6C3C6"></FooterStyle>
<HeaderStyle Font-Size="10pt" Font-Bold="True" ForeColor="#E7E7FF"
BackColor="#4A3C8C"></HeaderStyle>
<PagerStyle HorizontalAlign="Right" ForeColor="Black"
BackColor="#C6C3C6"></PagerStyle>
<SelectedItemStyle Font-Bold="True" ForeColor="White"
BackColor="#9471DE"></SelectedItemStyle>
<ItemStyle ForeColor="Black" VerticalAlign="Top"
BackColor="#DEDFDE"></ItemStyle>
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
<asp:ButtonColumn Text="Delete"
CommandName="Delete"></asp:ButtonColumn>
<asp:BoundColumn Visible="False" DataField="id"
ReadOnly="True"></asp:BoundColumn>
<asp:TemplateColumn>
<ItemTemplate>
<font face="Verdana" size="1" color="maroon">Titulo
libro;&nbsp;</font>
<asp:Label runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "titulolibro") %>'/><br>
<font face="Verdana" size="1" color="maroon">Autor;&nbsp;</font>
<asp:Label runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "autor") %>'/><br>
<font face="Verdana" size="1" color="maroon">Tema;&nbsp;</font>
<asp:Label runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "tema", "{0:d}") %>'/><br>
<font face="Verdana" size="1" color="maroon">ISBN;&nbsp;</font>
<asp:Label runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "isbn") %>'/><br>

<font face="Verdana" size="1" color="maroon">Comentario;&nbsp;</font>
<asp:Label runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "comentario") %>'/><br>

<font face="Verdana" size="1" color="maroon">Puntaje;&nbsp;</font>
<asp:Label runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "puntaje") %>'/><br>

</ItemTemplate>
<EditItemTemplate>
<font face="Verdana" size="1" color="maroon">Titulo libro</font><br>
<asp:TextBox runat="server" id="titulolibro" Text='<%#
DataBinder.Eval(Container.DataItem, "titulolibro") %>' borderwidth="1pt"
bordercolor="#000000" /><br>
<font face="Verdana" size="1" color="maroon">Autor</font><br>
<asp:TextBox runat="server" id="autor" Text='<%#
DataBinder.Eval(Container.DataItem, "autor") %>' borderwidth="1pt"
bordercolor="#000000" /><br>
<font face="Verdana" size="1" color="maroon">Tema</font><br>
<asp:TextBox runat="server" id="tema" Text='<%#
DataBinder.Eval(Container.DataItem, "tema", "{0:d}") %>' borderwidth="1pt"
bordercolor="#000000" /><br>
<font face="Verdana" size="1" color="maroon">ISBN</font><br>
<asp:TextBox runat="server" id="isbn" Text='<%#
DataBinder.Eval(Container.DataItem, "isbn") %>' borderwidth="1pt"
bordercolor="#000000" /><br>

<font face="Verdana" size="1" color="maroon">Comentario</font><br>
<asp:TextBox runat="server" id="comentario" Text='<%#
DataBinder.Eval(Container.DataItem, "comentario") %>' borderwidth="1pt"
bordercolor="#000000" /><br>

<font face="Verdana" size="1" color="maroon">Puntaje</font><br>
<asp:TextBox runat="server" id="puntaje" Text='<%#
DataBinder.Eval(Container.DataItem, "puntaje") %>' borderwidth="1pt"
bordercolor="#000000" /><br>

</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</ASP:DataGrid>
<br>
<table bgcolor="#ffffff">
<tr>
<td>
<a name="bbbb"><font face="Verdana" size="3"
color="teal"><b>Agregar</b></a> nuevo</FONT></td>
</tr>
<tr>
<td bgcolor="#ffffff">
<font face="Verdana" size="1" color="teal">Titulo libro:</font><br>
<asp:textbox id="titulolibro" runat="server" /><br>
<font face="Verdana" size="1" color="teal">Autor:</font><br>
<asp:textBox id="autor" runat="server" /><br>
<font face="Verdana" size="1" color="teal">Tema:</font><br>
<asp:textBox id="tema" runat="server" /><br>
<font face="Verdana" size="1" color="teal">ISBN:</font><br>
<asp:textBox id="isbn" runat="server" /><br>
<font face="Verdana" size="1" color="teal">Comentario:</font><br>
<asp:textBox id="comentario" runat="server" /><br>
<font face="Verdana" size="1" color="teal">Puntaje:</font><br>
<asp:textBox id="puntaje" runat="server" /><br>
<asp:Button Text="Save" OnClick="Insert_Click" Runat="server"
id="Button1" />
</td>
</tr>
</table>
</form>
<br/><a href="abmlibros.aspx">Reload</a> - <a
href="../default.aspx">Default</a><br/>
</body>
</HTML>


FF
www.francofigun.com.ar
www.microsofties.com.ar
MSN:
UIN: 314408886
Yahoo MSN:

"Dayana" wrote in message
news:
Hola, amigos
por favor queria saber como puedo mostrar un registro que modifique en el
datagrid. me explico
cuando el usuario modifique el registro se pueda actualizar y de una vez
mostrar en el datagrid.

Muchas Gracias.


Preguntas similares