Asp.net|datagrid|asp.net|datagrid in asp.net, how do you update all the records in the DataGrid at once? You can use the following method, first of all,
To create a template column for the column you want to update 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>
The DataGrid is then traversed 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