Do not use on any
shared computer
May 17, 2008 03:52am pdt
12.15.2003 at 11:50AM PST, ID: 20826376
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

asp.net - DataGrid:  Highlight a selected row
Zone: .NET
Tags: datagrid, row, selected
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(DataGrid1.EditItemIndex).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.IsStartupScriptRegistered("Startup")) 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).Controls(0), textbox).Text
                        Dim CommentBox As String = CType(e.Item.Cells(7).Controls(0), textbox).Text
   
                        ' TODO: update the Command value for your application
                        Dim myConnection As New SqlConnection(ConnectionString)
                        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.Add("@ContNo", SqlDbType.VarChar, 8000).Value = Control
                        UpdateCommand.Parameters.Add("@CommentBox", SqlDbType.VarChar, 99).Value = CommentBox
   
                        ' execute the command
                        Try
                            myConnection.Open()
                            UpdateCommand.ExecuteNonQuery()
   
                        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="ComputeSum" Font-Size="10pt" AutoGenerateColumns="False" width="90%" CellSpacing="2" GridLines="None" HorizontalAlign="Center" CellPadding="3" BackColor="White" ForeColor="Black" OnPageIndexChanged="DataGrid_Page" PageSize="12" AllowPaging="true" OnCancelCommand="DataGrid_Cancel" OnUpdateCommand="DataGrid_Update" OnEditCommand="DataGrid_Edit" OnItemCommand="DataGrid_ItemCommand" DataKeyField="Control#">
            <HeaderStyle font-bold="True" forecolor="white" backcolor="#4A3C8C"></HeaderStyle>
            <PagerStyle horizontalalign="Right" backcolor="#C6C3C6" mode="NumericPages" font-size="smaller"></PagerStyle>
            <ItemStyle backcolor="#DEDFDE"></ItemStyle>
            <FooterStyle backcolor="#C6C3C6"></FooterStyle>
            <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="smaller" ItemStyle-Width="7%" onclick="javascript:HighlightRow(this);"></asp:EditCommandColumn>
                        
            </Columns>
        </asp:datagrid>
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: jay-are
Solution Provided By: x_com
Participating Experts: 6
Solution Grade: A
Views: 632
Translate:
Loading Advertisement...
12.15.2003 at 12:09PM PST, ID: 9944393

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.15.2003 at 12:12PM PST, ID: 9944414

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.15.2003 at 02:02PM PST, ID: 9945278

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.15.2003 at 09:10PM PST, ID: 9947179

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.16.2003 at 06:59AM PST, ID: 9949585

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.16.2003 at 06:23PM PST, ID: 9954071

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.28.2004 at 06:06AM PDT, ID: 11415213

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.28.2004 at 09:54AM PDT, ID: 11417524

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.26.2004 at 01:25AM PDT, ID: 11900544

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
08.26.2004 at 02:15PM PDT, ID: 11907614

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
09.16.2004 at 08:12AM PDT, ID: 12075542

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
09.16.2004 at 10:18AM PDT, ID: 12077008

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
09.17.2004 at 12:24AM PDT, ID: 12082040

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
12.15.2003 at 12:09PM PST, ID: 9944393

Rank: Master

       Dim oInfo As DataGrid.HitTestInfo

        oInfo = Me.dgOrders.HitTest(e.X, e.Y)
       
            Me.dgOrders.Select(oInfo.Row)
 
12.15.2003 at 12:12PM PST, ID: 9944414
Dranizz can you go into a little more detail please.  I'm new to this.
 
12.15.2003 at 02:02PM PST, ID: 9945278
You can do that from the propertied of datagrid.

RIGHT click on the datagrid, take Property builder.
Select 'Format' from the left panel
Expand 'Items' from the right panel and select 'Edit Mode Items'
Select The 'Back Color'

This will change color of the row that you are editing and this will go away after update/cancel
 
12.15.2003 at 09:10PM PST, ID: 9947179

Rank: Master

jay-are,
add <EditItemStyle Width="100%" BackColor="yellow"></EditItemStyle> inside your datagrid property. Change whatever colour your like.
eg:
<asp:datagrid id="yourdg" runat="server">
...
<EditItemStyle Width="100%" BackColor="yellow"></EditItemStyle>
....
</asp:datagrid>

Regards
x_com
Accepted Solution
 
12.16.2003 at 06:59AM PST, ID: 9949585
bny:  If I knew how to use VS.Net properly then I'm sure I could follow your instructions.  I have yet to read through the walkthroughs manual and get familiar with the program.

x_com:  This worked perfectly.  Soon I'll have to send you a check for all your work!  :)

Thanks!
 
12.16.2003 at 06:23PM PST, ID: 9954071

Rank: Master

Glad to help, jay-are.:-)

Regards
x_com
 
06.28.2004 at 06:06AM PDT, ID: 11415213
Hey hey, just wondering how you change the size of a textbox in a datagrid when you click on the edit command?? I change the EditItemIndex to what is selected, but the default size of the textbox is way too big. How do I change this?? Thanks alot
 
06.28.2004 at 09:54AM PDT, ID: 11417524
To big as in width or height?
ItemStyle-Height?
ItemStyle-Width?

Try adding that to your editcommandcolumn tag and put in a "pt" value I think.  See if that changes the size of the textbox.  I've never changed the size of the default textbox.

 
08.26.2004 at 01:25AM PDT, ID: 11900544
Im trying to change the size of the default textboxes too (The width), but can't get anything to work.

jdbviper - If you got it to work please respond... Thanks...

stefan_v
 
08.26.2004 at 02:15PM PDT, ID: 11907614
jdbviper & stefan v

I think I've found the solution.  I played with this code today:

<asp:TemplateColumn HeaderText="Comments">
     <ItemTemplate>
      <asp:Label ID="CommentBoxLabel" Runat="server" Text='<%#DataBinder.Eval(Container, "DataItem.CommentBox") %>'>
      </asp:Label>
     </ItemTemplate>
<EditItemTemplate>
      <asp:TextBox width="400px" ID="CommentBox" Runat="server"  Text='<%# DataBinder.Eval(Container, "DataItem.CommentBox") %>'>
      </asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>

This allows you to adjust the size of the edit textbox all you want.
 
09.16.2004 at 08:12AM PDT, ID: 12075542
I am curious about your initial answer which uses a property called "HitTest" and a method called "Select" to select a row in the ASP.NET DataGrid control.  I have the new version of VS and my ASP.NET DataGrid control does not have these properties and methods.
 
09.16.2004 at 10:18AM PDT, ID: 12077008
RasinJ, if you are just trying to accomplish highlighting the selected row than use xcom's answer as its the easiest to implement.
 
09.17.2004 at 12:24AM PDT, ID: 12082040
I wound up using this...

public void MyGrid_OnItemDataBound(Object sender, DataGridItemEventArgs e)
{
      e.Item.Cells[e.Item.Cells.Count-1].Visible = false; //Don't show the last collumn (eg. an ID-collumn
      e.Item.Cells[0].Width = Unit.Pixel(50); //Set the width of column 1 to 50px
}

This dude does it all...
"Thanks 'OnItemDataBound'-function!"
"Thats all right, wasn't doing anything, anyway..."
 
 
20080206-EE-VQP-25