Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder
    Join Date
    Dec 2002
    Location
    Arlington, Texas USA
    Posts
    1,065
    Thanks
    4
    Thanked 8 Times in 8 Posts

    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.


    Code:
    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

    Code:
    <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>

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    something like this:

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

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    Senior Coder
    Join Date
    Dec 2002
    Location
    Arlington, Texas USA
    Posts
    1,065
    Thanks
    4
    Thanked 8 Times in 8 Posts
    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.

  • #4
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    You could do something like this to set the focus.

    Code:
    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
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •