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