how to add data...

avilarmannellaa

New Member
okie people... here is another killer question. <BR>i want to add a new blank row to my datagrid or datalist.... whichever you people think is best. <BR>when i click the "Add new row" button, a textbox will appear at the last row of the datagrid... which means if my datagrid has 5 rows of information, by clicking "add new row" i should get 6 row.. 5 rows of information and 1 row which is the textbox. <BR><BR>i dont save anything back to the database, untill i click the "save" button. i can continue to add new rows .. as many as i wish.. untill the "save" button is click then all information is posted to the database for inserting. can anyone out there help me with this? <BR>i'm very new to asp.net <BR>sorry for the spelling folks. <BR>Simon, <BR>Here is a demo that should get you started. It creates a dataset on page_load, preserves it as an xml string in ViewState between post events. You can add blank rows, make entries in the blank row, edit any previous entries. All of this is off-line independent of the source database. When you are ready, you can update the source by clicking the Save_All button. The Save_All button is shown, but the code to drive changes back to your database is up to you.<BR><BR>Copy and paste this code, then give it a try..........<BR><BR><BR>Good Luck,<BR>TT<BR><BR><%@ Page Language="VB" Debug = "True" %><BR><%@ Import NameSpace="System.Data" %><BR><%@ Import NameSpace="System.Xml" %><BR><BR><SCRIPT RUNAT="SERVER"><BR>' ===============================================<BR>' CREATE DATASET, PRESERVE IN VIEWSTATE,<BR>' ADD 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>' ===========================<BR><BR>Sub Page_Load(obj as Object, e as EventArgs)<BR><BR> With objDataSet.Tables("xmlTestTable")<BR> <BR> .Columns.Add("FName", System.Type.GetType("System.String"))<BR> .Columns.Add("LName", System.Type.GetType("System.String"))<BR><BR> End With<BR><BR><BR> If Not Page.IsPostBack Then<BR><BR> SaveIt()<BR><BR> End If<BR><BR>End Sub<BR><BR>' ===========================<BR><BR>Sub SaveIt()<BR><BR> strXML = objDataSet.GetXml()<BR><BR> ViewState("strData")=strXML <BR>End Sub<BR><BR>' ===========================<BR><BR>Sub GetIt()<BR><BR> If Not ViewState("strData") Is Nothing then<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><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() <BR> <BR> BindIt()<BR><BR> txtFName.Text = ""<BR> txtLName.Text = ""<BR><BR>End Sub<BR><BR>' ===========================<BR><BR>Sub BindIt()<BR><BR> objDGrid.DataSource = objDataSet.Tables("xmlTestTable")<BR><BR> objDGrid.DataBind()<BR><BR>End Sub<BR><BR>' ===========================<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><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><BR>Sub UpDateGrid(obj As object, e As DataGridCommandEventArgs)<BR><BR> Dim gRow As Integer = objDGrid.EditItemIndex<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()<BR><BR> objDataSet.Tables("xmlTestTable").Rows(gRow)(0) = txtFName.text<BR> objDataSet.Tables("xmlTestTable").Rows(gRow)(1) = txtLName.text<BR><BR> objDGrid.EditItemIndex = -1<BR><BR> SaveIt()<BR><BR> BindIt()<BR><BR>End Sub<BR><BR>' ===========================<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> <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> <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> </ASP:BoundColumn><BR><BR> <ASP:BoundColumn HeaderText = "Last Name" <BR> DataField = "LName"><BR> <ItemStyle Width = "150"/><BR> </ASP:BoundColumn><BR><BR><BR> <ASP:EditCommandColumn EditText = "Add/Edit"<BR> CancelText = "Cancel"<BR> UpdateText = "Enter"<BR> ItemStyle-Wrap = "False"<BR> HeaderText = "Add/Edit" ><BR> <ItemStyle Font-Size = "8"/><BR> </ASP:EditCommandColumn><BR><BR> </Columns><BR><BR> </ASP:DataGrid><BR><BR> </FORM><BR><BR></BODY></HTML><BR>
 
Back
Top