Update all records in the DataGrid at one time in Asp.net

Source: Internet
Author: User
In Asp.net, how does one update all records in the DataGrid at a time? You can use the following method. First,

Create a template column for The columns to be updated in the DataGrid, for example:

ASP: DataGrid id = "dgpopularfaqs" runat = "server"

Autogeneratecolumns = "false"

...>

<Columns>

<Asp: boundcolumn datafield = "faqid" itemstyle-width = "10%"

Itemstyle-horizontalalign = "center" headertext = "faq id"/>

<Asp: boundcolumn datafield = "categoryname" headertext = "category"/>



<Asp: templatecolumn headertext = "Question">

<Itemtemplate>

<Asp: textbox runat = "server" id = "txtdescription" columns = "75"

TEXT = '<% # container. dataitem ("Description") %>'/>

</Itemtemplate>

</ASP: templatecolumn>



<Asp: templatecolumn headertext = "submitted by">

<Itemtemplate>

<Asp: textbox runat = "server" id = "txtsubmittedby"

TEXT = '<% # container. dataitem ("submittedbyname") %>'/>

</Itemtemplate>

</ASP: templatecolumn>

</Columns>

</ASP: DataGrid>

Then, traverse the DataGrid once,

Dim myconnection as new sqlconnection (Connection string)

Dim mycommand as new sqlcommand (strsql, myconnection)

dim DGI as datagriditem
for each DGI in dgpopularfaqs. items
'read in the primary key field
dim ID as integer = convert. toint32 (dgpopularfaqs. datakeys (DGI. itemindex)
dim question as string = ctype (DGI. findcontrol ("txtdescription"), textbox ). text
dim submittedby as string = ctype (DGI. findcontrol ("txtsubmittedby"), textbox ). text
'issue an update statement...
dim updatesql as string = "Update tablename set question = @ question, "& _
" submittedbyname = @ submittedbyname where faqid = @ ID "
mycommand. parameters. clear ()
mycommand. parameters. add ("@ question", Question)
mycommand. parameters. add ("@ submittedbyname", submittedby)
mycommand. executenonquery ()
next

Related Article

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.