Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGrid - Selecting a Row

Status
Not open for further replies.

nevets2001uk

IS-IT--Management
Jun 26, 2002
609
0
0
GB
I'm trying to set my datagrid (asp.net - vb) so that the user can click anywhere on a given row in order to select it. I know you can select a row using a button or checkbox but can it be done just by clicking the actual row?

Cheers




Steve Gordon
 
You mean redirect the app to an specified URL after you click on a row?
If so, just go to the following DataGrid properties:

Header Text -> Title
Text Field -> Field Data (Depending on your data source)
URL field -> This is the data you will use to replace in the URL formatting string. i.e: UserID
URL Formatting String -> This is the actual URL to where you want to redirect your app and using the URL Field yo the DataGrid make the sustitution and creates the Hyperlink. i.e: UsersForm.aspx?ID={0}&ACTION=View, this will result in: <a href=&quot;UsersForm.aspx?ID=3&ACTION=View&quot;>Your User Name or something</a>

I hope this help

David
 
Sorry, I should have explained it better!

What I'm creating is a list of office locations which can be editted or modifed from the page. What I want is for the user to be able to select a row on the datagrid which will then be highlighted in a different colour. Then when they click one of the buttons on the page it will open up a modify page for that record or delete it. I know how to do everything except actually select the row so that it is highlighted.

Also if it's possible I would be quite nice to have a row highlight when the mouse is over it - but that's not as important as the actual selection!


Steve Gordon
 
Hi Steve

Not sure if this will work as is theoretical and untested! If you capture the itemcreated event of the datagrid and use a method something like this it should work...
Code:
public void myDatagrid_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e){
			if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem){
				e.Item.Attributes.Add(&quot;onmouseover&quot;, &quot;this.style.backgroundColor='#ff0000'&quot;);
				e.Item.Attributes.Add(&quot;onmouseout&quot;, &quot;this.style.backgroundColor='#ffffff'&quot;);
				e.Item.Attributes.Add(&quot;onclick&quot;, this.style.backgroundColor='#0000ff'&quot;);
			}
		}
Like i say i havent tried this but i don't see why it shouldn't work...

HTH

Rob

Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
Thanks. Fraid I'm not good at anything but vb code really!

I'll have a go at converting your idea to vb and see what I can come up with but if you have an example in vb that'd be a great help.

Cheers

Steve Gordon
 
ooohh - not very good with VB these days - seems to have all disappeared out of my head since I started with C#. Had to do some work on an old VBScript ASP project the other day and kept using C# syntax all over the place!!

Something like this maybe??
Code:
Sub myDatagrid_ItemCreated(sender As object,e As System.Web.UI.WebControls.DataGridItemEventArgs)
            If e.Item.ItemType = ListItemType.Item or e.Item.ItemType = ListItemType.AlternatingItem Then
                e.Item.Attributes.Add(&quot;onmouseover&quot;, &quot;this.style.backgroundColor='#ff0000'&quot;)
                e.Item.Attributes.Add(&quot;onmouseout&quot;, &quot;this.style.backgroundColor='#ffffff'&quot;)
                e.Item.Attributes.Add(&quot;onclick&quot;, this.style.backgroundColor='#0000ff'&quot;)
            End If
        End Sub
Rob


Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
Cool. Thanks, I'll give that a go and see what happens! I'll let you know how I get on.

<Off Topic> Is C# worth teaching myself? Will it be much more helpful or just offer most of the same stuff in a different syntax? I started with QBasic a few years back and never moved off that track!</Off Topic>

Steve Gordon
 
This is what I have so far...

Sub dgrdSites_ItemCreated ( s as Object, e as dataGridItemEventArgs )

If e.Item.ItemType = ListItemType.Item or e.Item.ItemType = ListItemType.AlternatingItem Then
e.Item.Attributes.Add(&quot;onmouseover&quot;, &quot;this.style.backgroundColor='#DCDCDC'&quot;)
e.Item.Attributes.Add(&quot;onmouseout&quot;, &quot;this.style.backgroundColor='#EAEAEA'&quot;)
'e.Item.Attributes.Add(&quot;onclick&quot;, &quot;this.style.backgroundColor='#DCDCDC'&quot;)
End If

dgrdSites.SelectedIndex = e.Item.ItemIndex

End Sub

It works great thanks! However I have a couple of other questions before you get really bored of me!

I'm trying to figure out how to reset all of the other rows to standard backcolor when a row is clicked so that only one row appears as selected. I thought about something like...

dgrdSites.Items.Backcolor=&quot;#EAEAEA&quot; but it won't work. Any better way?

Also with the mouseover thing which is pretty cool I'm trying to work out a way to do it so that it won't mouseover on the selected row since doing do resets the colour on mouseout. If there's an easy way to exclude the selected row form the mouseover code that would be great!

Thanks



Steve Gordon
 
Having worked with this for a little while now I'm still not able to fully achieve my plan.

At the moment the mouseover highlight is working for the rows (GREAT!)

However I'm still not sure how to actually implement a selection of the row. I need the selected row to be highlighted and also active (selectedindex). By that I mean I want to be able to tell which row is selected so that I can pull up a new page to display further details.

The grid is a summary of information. The user should be able to select an option and click on the More button to pull up a full detailed page. That I can do if I can tell which item is selected!

Any further ideas?

Thanks

Steve Gordon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top