Need advice, help, possible code example

A

Aaron

First, I am not sure if this is the correct NG. If not, please let me
know.

I have a working asp.net application which displays a datagrid. There
multiple columns in the datagrid. Each column has a corresponding
textbox located elsewhere on the form for editing. I am not editing in
the DataGrid itself. When a record is selected, the data in the columns
populates the textboxes to provide an interface for editing the record.

Here is a snippet of the code used:

Sub DataGrid_Select(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
FillTextBoxes(e)
End Sub



Sub FillTextBoxes(ByVal e As DataGridCommandEventArgs)
Dim Cell1 As String = e.Item.Cells(1).Text.ToString
Dim Cell2 As String = e.Item.Cells(2).Text.ToString
Dim Cell3 As String = e.Item.Cells(3).Text.ToString
If Cell1.Equals(" ") Then
txtFullname.Text = ""
Else
txtFullname.Text = Trim(Cell1)
End If

If e.Item.Cells(2).Text.Equals(" ") Then
txtCompany.Text = ""

Else
txtCompany.Text = Trim(e.Item.Cells(2).Text)

End If

If e.Item.Cells(3).Text.Equals(" ") Then
txtTitle.Text = ""

Else
txtTitle.Text = Trim(e.Item.Cells(3).Text)

End If
End sub



What I would like to accomplish is adding a "Next Record" button which
will save the current record and then move on to the next record in the
DATAGRID. Currently I have an "Update Record" button which updates the
DB and then clears all of the textboxes. I would like the "Next Record"
button to update the current record, then move to the next record in the
DATAGRID, even if that record is on the next page. I figure if I can get
that far, I might be able to add a "Previous Record" button.

1. Is the above code the best way to fill the textboxes using the
datagrid?

2. How could I implement the "Next Record" button?


I can post lots more code if needed.

Thanks,
Aaron
 
M

Mark Miller

Try using the DataGrid.EditItemIndex property.

Your code sould look something like this:
I tried to convert the code from C# but I don't use much VB and I didn't
test this code so I'm not sure if it works. You'll need to check the
databinding syntax for the ASPX file all it's doing is binding to the
corresponding columns in the DataGrid.DataSource.

---------------- Code Behind -------------------------



Sub EditCommand(sender As Object, e As DataGridCommandEventArgs )

'turn on edit template for selected row

dgContacts.EditItemIndex = e.Item.ItemIndex

End Sub



Sub UpdateCommand(sender As Object, e As DataGridCommandEventArgs)

'declare variables

txtFullName As TextBox

txtCompany As TextBox

txtTitle As TextBox



'get references to TextBox controls

txtFullName = CType(e.Item.FindControl("txtFullName"), TextBox)

txtCompany = CType(e.Item.FindControl("txtCompany"), TextBox)

txtTitle = CType(e.Item.FindControl("txtTitle"), TextBox)





' .... Update Record ....





'increment index to next row

dgContacts.EditItemIndex = dgContacts.EditItemIndex + 1

End Sub





------------------- ASPX File -----------------------------------



<asp:datagrid id="dgContacts" runat="server" OnUpdateCommand="UpdateCommand"
OnEditCommand="EditCommand" AutoGenerateColumns="False">

<Columns>

<asp:TemplateColumn HeaderText="Name">

<ItemTemplate>

<asp:Label ID="lblFullName"
Runat="server">

<%#
((DataRowView)Container.DataItem)["FullName"]%>

</asp:Label>

</ItemTemplate>

<EditItemTemplate>

<asp:TextBox ID="txtFullName"
Runat="server" Text='<%# ((DataRowView)Container.DataItem)["FullName"]%>' />

</EditItemTemplate>

</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="Company">

<ItemTemplate>

<asp:Label ID="lblCompany"
Runat="server">

<%#
((DataRowView)Container.DataItem)["Company"]%>

</asp:Label>

</ItemTemplate>

<EditItemTemplate>

<asp:TextBox ID="txtCompany"
Runat="server" Text='<%#((DataRowView)Container.DataItem)["Company"]%>'/>

</EditItemTemplate>

</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="Title">

<ItemTemplate>

<asp:Label id="lblTitle" Runat="server">

<%#
((DataRowView)Container.DataItem)["Title"] %>

</asp:Label>

</ItemTemplate>

<EditItemTemplate>

<asp:TextBox id="txtTitle"
Runat="server" Text='<%# ((DataRowView)Container.DataItem)["Title"] %>' />

</EditItemTemplate>

</asp:TemplateColumn>

<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Save
& Move Next" EditText="Edit" />

</Columns>

</asp:datagrid>
 

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

Top