Add a new row in the DataGrid

Source: Internet
Author: User

Add a new row in the DataGrid

Http: // lucky_elove.www1.dotnetplayground.com/

ASP. NET DataGrid provides the built-in record row editing function, but does not provide the built-in function of adding new rows. One method is to add a new row to the able and re-bind it to the DataGrid. This method is feasible, but confirmation is required before the update, which may lead to empty rows. Another solution is to use the DataGrid footer template to provide an empty row, which can increase the speed and avoid the disadvantages of other methods.

To provide a blank row for the viewer, we use the footer template of the DataGrid. We directly add a text box to the footer template to avoid unnecessary operations, such as clicking the "edit" button. This can also reduce the number of times the reciprocating data is submitted. Here, we still linkbutton (insert) and set the commandname attribute to "insert". In the itemcommand event of the DataGrid, make sure that the record is added only when the user clicks "insert" linkbutton. The method for adding data to a database is very simple.

The following example provides the function of quickly adding a new row to the DataGrid. The aspx code and the cohe behind code are respectively as follows. Note the following to change the data recording connection string:

View examples

Insertabledatagrid. aspx

<Meta name = "generator" content = "Microsoft Visual Studio. net 7.0 "> <meta name =" code_language "content =" Visual Basic 7.0 "> <meta name =" vs_defaultclientscript "content =" JavaScript "> <meta name =" vs_targetschema "content = "http://schemas.microsoft.com/intellisense/ie5">

Insertabledatagrid. aspx. VB

Imports system. data <br/> imports system. data. sqlclient <br/> public class insertabledatagrid <br/> inherits system. web. UI. page <br/> protected withevents datagrid1 as system. web. UI. webcontrols. dataGrid <br/> # region "Web Form Designer generated code" <br/> 'this call is required by the web form designer. <br/> <system. diagnostics. debuggerstepthrough> private sub initializecomponent () <br/> end sub <br/> private sub page_init (byval sender as system. object, byval e as system. eventargs) handles mybase. init <br/> 'codegen: This method call is required by the web form designer <br/> 'do not modify it using the code editor. <br/> initializecomponent () <br/> end sub <br/> # End Region <br/> dim connstr as string = "Integrated Security = sspi; user id = sa; initial catalog = northwind; Data Source =. /netsdk "<br/> private sub page_load (byval sender as system. object, byval e as system. eventargs) handles mybase. load <br/> if not page. ispostback then <br/> bindgrid () <br/> end if <br/> end sub <br/> sub bindgrid () <br/> dim CNN as new sqlconnection (connstr) <br/> dim da as new sqldataadapter ("select employeeid, lastname, firstname from employees", CNN) <br/> dim ds as new dataset () <br/> da. fill (DS, "employees") <br/> maid () <br/> () <br/> end sub <br/> private sub datagrid1_itemcommand (byval source as object, byval e as system. web. UI. webcontrols. maid) _ <br/> handles maid command <br/> If e. commandname = "insert" then <br/> dim CNN as new sqlconnection (connstr) <br/> dim T1 as textbox = E. item. findcontrol ("textbox2") <br/> dim T2 as textbox = E. item. findcontrol ("textbox4") <br/> CNN. open () <br/> dim cmd as new sqlcommand ("insert into employees (lastname, firstname) values ('" & amp; t1.text & amp ;"', '"& amp; t2.text & amp;"') ", CNN) <br/> cmd. executenonquery () <br/> CNN. close () <br/> bindgrid () <br/> end if <br/> end sub <br/> end class <br/> </system. diagnostics. debuggerstepthrough>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.