Helpful Information
 
 
Category: ASP.NET
Index of last row in datagrid

I have a datagrid where I have a template column. In the template column I have added a button to insert a new row. When I click on the button, textboxes with preliminary values appear. Currently I have them at the beginning of the datagrid by using
trailersDG.EditItemIndex = 0
But I would like to add them to the end of the datagrid. How can I get the EditItemIndex of the last row thus allowing me to use paging? Here is what I have so far.



Public Sub AddNewRow(sender As Object, e As EventArgs)
Dim queryString As String = "INSERT INTO trlrTypes(equipmentCode,equipmentType) VALUES('nt','New Type')"
Dim dbCommand As IDbCommand = New OleDbCommand(queryString, dbConnection)
dbConnection.Open
dbCommand.ExecuteNonQuery
Dim nNewItemIndex As Integer = trailersDG.Items.Count
If nNewItemIndex >= trailersDG.PageSize Then
trailersDG.CurrentPageIndex += 1
nNewItemIndex = 0
End If
trailersDG.EditItemindex = nNewItemIndex
dbConnection.close
UpdateView()
End Sub


Private Sub UpdateView()
trailersDG.EditItemIndex = 0
BindTrailers()
End Sub

Here is the datagrid's HTML


<asp:DataGrid id="trailersDG" runat="server" OnUpdateCommand="trailersDG_Update" OnCancelCommand="trailersDG_Cancel"
OnEditCommand="trailersDG_Edit" OnPageIndexChanged="NewPage" OnDeleteCommand="trailersDG_Delete"
OnItemDataBound="trailersDG_ItemDataBound" OnSortCommand="SortEventHandler" AllowCustomPaging="True" AllowPaging="True"
PageSize="15" DataKeyField="id" AutoGenerateColumns="False" AllowSorting="True" ShowFooter="True" GridLines="None">
<FooterStyle backcolor="DarkGray"></FooterStyle>
<HeaderStyle font-size="Larger" font-bold="True" backcolor="DarkGray"></HeaderStyle>
<PagerStyle verticalalign="Middle" font-bold="True" horizontalalign="Center" backcolor="White" mode="NumericPages"></PagerStyle>
<AlternatingItemStyle backcolor="#C0C0FF"></AlternatingItemStyle>
<Columns>
<asp:ButtonColumn Text="Delete" ButtonType="PushButton" CommandName="Delete"></asp:ButtonColumn>
<asp:BoundColumn DataField="equipmentCode" SortExpression="equipmentCode" HeaderText="Code">
<HeaderStyle width="150px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="equipmentType" SortExpression="equipmentType" HeaderText="Type">
<HeaderStyle width="150px"></HeaderStyle>
</asp:BoundColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Button runat="server" Text="Edit" CommandName="Edit" CausesValidation="false"></asp:Button>
</ItemTemplate>
<FooterTemplate>
<asp:linkbutton runat="server" OnClick="AddNewRow" Text="Add New Row.." Enabled="<%# IsLastPage() %>" />
</FooterTemplate>
<EditItemTemplate>
<asp:Button runat="server" Text="Update" CommandName="Update"></asp:Button>
<asp:Button runat="server" Text="Cancel" CommandName="Cancel" CausesValidation="false"></asp:Button>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn Visible="False" DataField="id"></asp:BoundColumn>
</Columns>
</asp:DataGrid>

something like this:

TotalRows = CType(YourDatagridId.DataSource, DataView).Table.Rows.Count

Eric

Thanks for the reply, I had fixed it myself already. All I needed to do was remove the call to UpdateView and instead make a call to BindTrailers(). Now when a new row is created in the datagrid, there are two textboxes for editing. Now I just have to figure out how to add some clientside javascript to set the focus onto the first textbox.

You could do something like this to set the focus.



function SetFocus(){
var dg = document.getElementById("DataGrid1");
var arrElems = dg.getElementsByTagName("input");
for(i=0;i<arrElems.length;i++){
if(arrElems[i].type.toLowerCase()=="text"){
arrElems[i].focus();
break;
}
}
}


Eric










privacy (GDPR)