Solved

Programmatically setting gridview selected value

Posted on 2011-03-21
18
578 Views
Last Modified: 2012-05-11
I would ilke to set the selected value of my gridview based on the selected value of a dropdown control.

I added the attached code to my selectedindex changed  event for my dropdown.  However, it says that the 'Selected Value' for my gridview is read only.

How can I get around this?

If ddl_projectlist.SelectedValue <> -1 Then
            gvProjects.DataBind()
            gvProjects.SelectedValue = ddl_projectlist.SelectedValue

        End If

Open in new window

0
Comment
Question by:imstac73
  • 9
  • 8
18 Comments
 
LVL 13

Expert Comment

by:gamarrojgq
ID: 35184234
Hi,

The SELECTEDVALUE property of a GridView is REALONLY and you cannot change that. What are you trying to do? that the Row that have that Value shows as Selected?

If that is what you want, you have to assign the property SELECTEDINDEX with the Row Number for the Value Selected in your DropDown like hits

gbProjects.SelectedIndex  =  2 'Put here the Real Row Number  
0
 
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 35185893
Are you trying to update the database from DataGridView?

Check this: http://social.msdn.microsoft.com/forums/en-US/netfxcompact/thread/4db2b139-30ce-44a6-b195-9fad095f6b1c/
0
 

Author Comment

by:imstac73
ID: 35189720
Yes, I want to make the row that has the selected value of the dropdown list the selected row in the gridview.  How do I get the row number of the gridview that corresponds to the value selected in the dropdown list?

My dropdown list value is my projectid field which is also a datakey for the gridview.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 13

Expert Comment

by:gamarrojgq
ID: 35190349
You can iterate the grid Rows like this

  For n As Integer = 0 To gvProjects.DataKeys.Count - 1
            If gvProjects.DataKeys(n).Value = ddl_projectlist.SelectedValueThen
                gvProjects.SelectedIndex = n
                Exit for
            End If
        Next
0
 

Author Comment

by:imstac73
ID: 35191717
Thanks, this code worked to select the current row.  I also have a formview that needs to also go to the project record that is selected.  I tried to change the code above to use it for the formview as well but I'm a little confused on the correct code.

 For n As Integer = 0 To fvProjects2.DataItemCount - 1
                If fvProjects2.DataKey(n).Value = ddl_projectlist.SelectedValue Then
                    fvProjects [ Not sure what goes here]= n
                    Exit For
                End If
            Next
0
 
LVL 13

Expert Comment

by:gamarrojgq
ID: 35192992
ok, the FormView control would not allow you to do something like that since it does not have a property or method to jump to a specific record.

What kind of Datasource are you using to the FormView? a Datatable? a BindingSource?
0
 
LVL 13

Expert Comment

by:gamarrojgq
ID: 35193051
ok, if you are using PAGING in your formview you can use PageIndex to select the record you want to show like this


 For n As Integer = 0 To fvProjects2.DataItemCount - 1
                If fvProjects2.DataKey(n).Value = ddl_projectlist.SelectedValue Then
                    fvProjectsPageIndex =  n
                    Exit For
                End If
            Next

0
 

Author Comment

by:imstac73
ID: 35194007
On the line of code: If fvProjects2.DataKey(n).Value = ddl_projectlist.SelectedValue


I get this error:
Public member 'Value' on type 'Long' not found.
0
 
LVL 13

Expert Comment

by:gamarrojgq
ID: 35194091
Ok, sorry for that, what are you using as a DataSource of the FormView? a dataTable? a dataset? a SqlDatasource?

0
 

Author Comment

by:imstac73
ID: 35194098
a SqlDatasource
0
 
LVL 13

Expert Comment

by:gamarrojgq
ID: 35194368
ok try this, is a little odd but it should work


        Dim rowView As DataRowView
        ProductsFormView.DataBind()

        For n As Integer = 0 To ProductsFormView.DataItemCount - 1
            ProductsFormView.PageIndex = n
            ProductsFormView.DataBind()
            rowView = CType(ProductsFormView.DataItem, DataRowView)
            If rowView("Data").ToString = ddl_projectlist.SelectedValue.ToString Then
                Exit For
            End If
        Next
0
 

Author Comment

by:imstac73
ID: 35194459
It did not recognize the DataRowView so I changed it to Data.DataRowView but when I run it I get the following error:

Data is neither a DataColumn nor a DataRelation for table DefaultView.

on line If rowView("Data").ToString = ddl_projectlist.SelectedValue.ToString Then
0
 
LVL 13

Expert Comment

by:gamarrojgq
ID: 35194506
ok, sorry for ask, but did you change ProductsFormView for fvProjects2? and also change the Ctype call?

 Dim rowView As Data.DataRowView
        fvProjects2.DataBind()

        For n As Integer = 0 To fvProjects2.DataItemCount - 1
            fvProjects2.PageIndex = n
            fvProjects2.DataBind()
            rowView = CType(fvProjects2.DataItem, Data.DataRowView)
            If rowView("Data").ToString = ddl_projectlist.SelectedValue.ToString Then
                Exit For
            End If
        Next
0
 

Author Comment

by:imstac73
ID: 35199038
Yes
0
 
LVL 13

Expert Comment

by:gamarrojgq
ID: 35199182
ok, if the DataSource of your FormView is the same of the GridView, try to use the same n value that you use for the gridview in the FormView, so after this

For n As Integer = 0 To gvProjects.DataKeys.Count - 1
            If gvProjects.DataKeys(n).Value = ddl_projectlist.SelectedValueThen
                gvProjects.SelectedIndex = n
                Exit for
            End If
        Next

Add this
           'USE THE SAME N VALUE OF THE GRIDVIEW ON THE FORMVIEW
            fvProjects2.PageIndex = n
            fvProjects2.DataBind()

Try it and let me know
0
 

Author Comment

by:imstac73
ID: 35199459
My datasource is different.
0
 
LVL 13

Accepted Solution

by:
gamarrojgq earned 500 total points
ID: 35199500
ok, sorry my bad, in the previous code the error is that in my environment I used DATA as the column to check for the selectedvalue, but in your environment you have to put the column that you use, so it will be like this

 Dim rowView As Data.DataRowView
        fvProjects2.DataBind()

        For n As Integer = 0 To fvProjects2.DataItemCount - 1
            fvProjects2.PageIndex = n
            fvProjects2.DataBind()
            rowView = CType(fvProjects2.DataItem, Data.DataRowView)
            If rowView("TheNameOrYOURColumn").ToString = ddl_projectlist.SelectedValue.ToString Then
                Exit For
            End If
        Next
0
 

Author Closing Comment

by:imstac73
ID: 35217039
Thanks so much this worked great!
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now