Hello Experts:
I'm trying to highlight a row inside a editable datagrid. When the user clicks "edit" I'd like to highlight that row until they click "update" or "cancel". I'll include my code below. I'm looking for a solution in VB.Net. I find a lot of solutions online that are for C#.
Thanks for your help!
Here are my subs for editing and updating:
Edit:
Sub DataGrid_Edit(Sender As Object, E As DataGridCommandEventArgs)
' turn on editing for the selected row
Dim CommentBox As Textbox
DataGrid1.EditItemIndex = e.Item.ItemIndex
Dim scriptJs As String
BindGrid()
CommentBox = CType(DataGrid1.Items(Data
Grid1.Edit
ItemIndex)
.Cells(7).
Controls(0
), TextBox)
scriptJs = "<script language=javascript>" & vbCrLf
scriptJs &= "document.getElementById('
" & CommentBox.UniqueID & "').focus();" & vbCrLf
scriptJs &= "document.getElementById('
" & CommentBox.UniqueID & "').select();" & vbCrLf
scriptJs &= "<" & "/script>"
If (Not Me.IsStartupScriptRegister
ed("Startu
p")) Then
Me.RegisterStartupScript("
Startup", scriptJs)
End If
End Sub
Update:
Sub DataGrid_Update(Sender As Object, E As DataGridCommandEventArgs)
' update the database with the new values
' get the edit text boxes
Dim Control As String = CType(e.Item.Cells(0).Cont
rols(0), textbox).Text
Dim CommentBox As String = CType(e.Item.Cells(7).Cont
rols(0), textbox).Text
' TODO: update the Command value for your application
Dim myConnection As New SqlConnection(ConnectionSt
ring)
Dim UpdateCommand As SqlCommand = new SqlCommand()
UpdateCommand.Connection = myConnection
If AddingNew = True Then
UpdateCommand.CommandText = "INSERT INTO comments1130(ControlNo, CommentBox) VALUES (@Contno, @CommentBox)"
Else
UpdateCommand.CommandText = "UPDATE comments1130 SET CommentBox = @CommentBox WHERE controlNo = @ContNo"
End If
UpdateCommand.Parameters.A
dd("@ContN
o", SqlDbType.VarChar, 8000).Value = Control
UpdateCommand.Parameters.A
dd("@Comme
ntBox", SqlDbType.VarChar, 99).Value = CommentBox
' execute the command
Try
myConnection.Open()
UpdateCommand.ExecuteNonQu
ery()
Catch ex as Exception
Message.Text = ex.ToString()
Finally
myConnection.Close()
End Try
' Resort the grid for new records
If AddingNew = True Then
DataGrid1.CurrentPageIndex
= 0
AddingNew = false
End If
' rebind the grid
DataGrid1.EditItemIndex = -1
BindGrid()
End Sub
And finally the datagrid:
asp:datagrid id="DataGrid1" runat="server" enableviewstate="true" ShowFooter="true" OnItemDataBound="ComputeSu
m" Font-Size="10pt" AutoGenerateColumns="False
" width="90%" CellSpacing="2" GridLines="None" HorizontalAlign="Center" CellPadding="3" BackColor="White" ForeColor="Black" OnPageIndexChanged="DataGr
id_Page" PageSize="12" AllowPaging="true" OnCancelCommand="DataGrid_
Cancel" OnUpdateCommand="DataGrid_
Update" OnEditCommand="DataGrid_Ed
it" OnItemCommand="DataGrid_It
emCommand"
DataKeyField="Control#">
<HeaderStyle font-bold="True" forecolor="white" backcolor="#4A3C8C"></Head
erStyle>
<PagerStyle horizontalalign="Right" backcolor="#C6C3C6" mode="NumericPages" font-size="smaller"></Page
rStyle>
<ItemStyle backcolor="#DEDFDE"></Item
Style>
<FooterStyle backcolor="#C6C3C6"></Foot
erStyle>
<Columns>
<asp:BoundColumn DataField="Control#" ReadOnly="False" HeaderText="Cust. Number" />
<asp:BoundColumn DataField="Reference#" ReadOnly="True" HeaderText="Reference#" />
<asp:BoundColumn DataField="Document Date" DataFormatString="{0:d}" ReadOnly="True" HeaderText="Document Date" />
<asp:BoundColumn DataField="Last Name" ReadOnly="True" HeaderText="Last Name" />
<asp:boundColumn DataField="First Name" ReadOnly="True" HeaderText="First Name" />
<asp:BoundColumn DataField="SumAmt" DataFormatString="{0:c}" ReadOnly="True" HeaderText="Amount" />
<asp:BoundColumn DataField="daysold" ReadOnly="True" HeaderText="Days Old" />
<asp:BoundColumn DataField="CommentBox" HeaderText="Comments" ReadOnly="False" />
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit" ItemStyle-Font-Size="small
er" ItemStyle-Width="7%" onclick="javascript:Highli
ghtRow(thi
s);"></asp
:EditComma
ndColumn>
</Columns>
</asp:datagrid>
Start Free Trial