Adding row to top

sofabulousx3

New Member
Is there a way to when I add a row to a datatable to have it appear on the top. It appears on the bottom even if I use InsertAt. Here is the code I am using to create my row.<BR><BR>MyConnection.Open()<BR> <BR> Dim myRow As DataRow <BR> Dim DS As DataSet<BR> DS = new DataSet()<BR> <BR> MyCommand.Fill(DS, "empphone")<BR> <BR> MyDataGrid.DataSource=DS.Tables("empphone").DefaultView<BR> <BR> myRow = Ds.Tables("empphone").NewRow()<BR> myRow("build") = 0<BR> myRow("exten") = 0<BR> DS.Tables("empphone").Rows.Add(myRow)<BR> <BR> MyDataGrid.DataBind()<BR> <BR> MyConnection.Close()You have absolutely no control over how the database places your record.<BR><BR>Just order it when you SELECT it out.The following example may help you.<BR><BR>It adds a new row to the datatable during Page_load and places it in edit mode in the data grid. When you enter text in the edit row, it loops back (ie adds another blank row and places it in edit mode). This keeps the blank row of your datagrid at the top.<BR><BR>Copy & paste it then save it as a regular aspx file. You should be able to run it in your browser with no problems.<BR><BR>Good luck,<BR>Tom T<BR><BR><%@ Page Language="VB" Debug = "True" %><BR><%@ Import NameSpace="System.Data" %><BR><%@ Import NameSpace="System.Xml" %><BR><BR><BR><SCRIPT RUNAT="SERVER"><BR>' ================================================== ======<BR>' GOOD DEMO OF WORKING WITH DATA IN A DISCONNECTED STATE, <BR>' THEN SAVING CHANGES<BR>' BACK TO SOURCE DATABASE WHEN YOU'RE READY.<BR>'<BR>' CREATE DATASET, PRESERVE IN VIEWSTATE,<BR>' ADD ENTRIES OR BLANK ROWS, BIND TO DATAGRID,<BR>' EDIT DATAGRID, PRESERVE EDITS. <BR>' POST EDITS BACK TO DATABASE WITH BUTTON CLICK.<BR>' ================================================== =======<BR><BR>Dim objDataSet As New DataSet("objDataSet")<BR><BR>Dim xmlTestTable As DataTable = objDataSet.Tables.Add("xmlTestTable")<BR><BR>Dim strXML As String<BR><BR>' === PAGE_LOAD SUB.<BR><BR>Sub Page_Load(obj as Object, e as EventArgs)<BR><BR> ' === NEED TO SPECIFY COLUMNS SINCE A BLANK ROW CAN BE ADDED.<BR><BR> With objDataSet.Tables("xmlTestTable") <BR><BR> .Columns.Add("FName", System.Type.GetType("System.String"))<BR><BR> .Columns.Add("LName", System.Type.GetType("System.String"))<BR><BR> End With<BR><BR> ' === FIRST TIME THRU, SAVE A COPY OF DATASET TO VIEWSTATE.<BR><BR> If Not Page.IsPostBack Then<BR><BR> FirstRow()<BR> objDGrid.EditItemIndex = 0<BR> BindIt()<BR><BR> End If<BR><BR> GetIt()<BR><BR>End Sub<BR><BR><BR>' === SaveIt SUB CONVERTS DATASET TO XML AND PUTS IN VIEWSTATE.<BR><BR>Sub SaveIt()<BR><BR> strXML = objDataSet.GetXml()<BR><BR> ViewState("strData")=strXML <BR> <BR>End Sub<BR><BR><BR>' === GetIt SUB GET XMLSTRING FROM VIEWSTATE & RECREATES DATASET.<BR><BR>Sub GetIt()<BR><BR> If Not ViewState("strData") Is Nothing then<BR><BR> objDataSet.Clear()<BR><BR> strXML = ViewState("strData").ToString() <BR><BR> Dim strReader As New System.IO.StringReader(strXML) <BR><BR> objDataSet.Readxml(strReader) <BR><BR> End If<BR><BR>End Sub<BR><BR><BR>' === ADD A NEW ROW WHEN PAGE IS FIRST LOADED.<BR><BR>Sub FirstRow()<BR><BR> GetIt() <BR><BR> Dim AddaRow As DataRow = objDataSet.Tables("xmlTestTable").NewRow()<BR><BR> AddaRow.Item("FName") = txtFName.Text<BR> AddaRow.Item("LName") = txtLName.Text<BR><BR> objDataSet.Tables("xmlTestTable").Rows.Add(AddaRow)<BR><BR> SaveIt() <BR> <BR> 'BindIt() <BR><BR>End Sub<BR><BR><BR>' === ADD A NEW ROW TO DATASET. BLANK ROW ENTRY IS OK.<BR><BR>Sub AddRow(obj as Object, e as EventArgs)<BR><BR> GetIt() <BR><BR> Dim AddaRow As DataRow = objDataSet.Tables("xmlTestTable").NewRow()<BR><BR> AddaRow.Item("FName") = txtFName.Text<BR> AddaRow.Item("LName") = txtLName.Text<BR><BR> objDataSet.Tables("xmlTestTable").Rows.Add(AddaRow)<BR><BR> SaveIt() ' SAVE REVISED DS TO VIEWSTATE.<BR> <BR> BindIt() ' REBIND REVISED DS TO GRID.<BR><BR> txtFName.Text = "" ' CLEAR TEXTBOXES.<BR> txtLName.Text = ""<BR><BR>End Sub<BR><BR><BR>' === STANDARD SUB FOR BINDING DS TO DATAGRID.<BR><BR>Sub BindIt()<BR><BR> objDGrid.DataSource = objDataSet.Tables("xmlTestTable")<BR><BR> objDGrid.DataBind()<BR><BR>End Sub<BR><BR><BR>' === EDIT LINK CLICKED ON DATAGRID. PUT GRID IN EDIT MODE.<BR><BR>Sub EditGrid(obj as object, e as DataGridCommandEventArgs)<BR><BR> GetIt()<BR> objDGrid.EditItemIndex = e.Item.ItemIndex<BR> SaveIt()<BR> BindIt()<BR><BR>End Sub<BR><BR><BR>' === CANCEL LINK CLICKED ON DATAGRID. <BR><BR>Sub CancelGrid(obj as object, e as DataGridCommandEventArgs)<BR> <BR> GetIt()<BR> objDGrid.EditItemIndex = -1<BR> SaveIt()<BR> BindIt()<BR><BR>End Sub<BR><BR><BR>' === UPDATE LINK CLICKED ON DATAGRID. UPDATE DATASET WITH EDITS.<BR><BR>Sub UpDateGrid(obj As object, e As DataGridCommandEventArgs)<BR><BR> Dim gRow As Integer = objDGrid.EditItemIndex ' ROW IN EDIT MODE.<BR><BR> Dim txtFName As TextBox = e.Item.Cells(0).Controls(0)<BR> Dim txtLName As TextBox = e.Item.Cells(1).Controls(0)<BR><BR> GetIt() ' GET COPY OF DS<BR><BR> objDataSet.Tables("xmlTestTable").Rows(gRow)(0) = txtFName.text<BR> objDataSet.Tables("xmlTestTable").Rows(gRow)(1) = txtLName.text<BR><BR> objDGrid.EditItemIndex = -1 ' TAKE GRID OUT OF EDIT MODE.<BR><BR> SaveIt() ' SAVE CHANGES TO DS<BR><BR> BindIt() ' RE-BIND GRID.<BR><BR>End Sub<BR><BR><BR>' === SUB TO SAVE DATASET CHANGES BACK TO SOURCE DATABASE.<BR><BR>Sub SaveAll(obj as Object, e as EventArgs)<BR><BR> Response.Write("Your DataBase Update Sub Would Run Now")<BR><BR>End Sub <BR><BR></SCRIPT><BR><BR><BR><BR><HTML><BODY><BR><BR> <FORM Runat="Server" EnableViewState = "True" ><BR><BR> <BR /><BR> <BR> <ASP:Label id="lblMessage" <BR> runat="server" /><BR> <BR /><BR><BR> First Name:?<BR><BR> <ASP:TextBox id="txtFName" <BR> Width="114" <BR> Runat="Server" /><BR><BR> ? Last Name:?<BR><BR> <ASP:TextBox id="txtLName"<BR> Width="114" <BR> Runat="Server" /><BR><BR> <BR/><BR/><BR><BR><BR> <ASP:Button ID="btnSubmit" <BR> Text="Add New Row" <BR> Runat="Server" <BR> OnClick="AddRow" /><BR><BR> <ASP:Button ID="btnSave" <BR> Text="Save All" <BR> Runat="Server" <BR> OnClick="SaveAll" /><BR><BR> <BR/><BR/><BR><BR><BR> <ASP:DataGrid ID="objDGrid"<BR> Runat="Server"<BR> OnEditCommand = "EditGrid"<BR> OnCancelCommand = "CancelGrid"<BR> OnUpdateCommand = "UpdateGrid"<BR> Width = "350" <BR> AutoGenerateColumns = "False"><BR><BR> <Columns><BR><BR> <ASP:BoundColumn HeaderText = "First Name" <BR> DataField = "FName"><BR> <ItemStyle Width = "150"/><BR><BR> </ASP:BoundColumn><BR><BR> <ASP:BoundColumn HeaderText = "Last Name" <BR> DataField = "LName"><BR> <ItemStyle Width = "150"/><BR><BR> </ASP:BoundColumn><BR><BR><BR> <ASP:EditCommandColumn<BR> EditText = "Add/Edit"<BR> CancelText = "Cancel"<BR> UpdateText = "Enter"<BR> ItemStyle-Wrap = "False"<BR> HeaderText = "Add/Edit" ><BR> <ItemStyle Font-Size = "8"/><BR><BR> </ASP:EditCommandColumn><BR><BR> </Columns><BR><BR> </ASP:DataGrid><BR><BR> </FORM><BR><BR></BODY></HTML>You might want to try adding it to the datagrid after the bind. I've done something similar with a dropdownlist box while adding the default item to the top of the list after binding.
 
Back
Top