Solved

Programmatically setting gridview selected value

Posted on 2011-03-21
18
570 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
WPF issue with Trigger 2 69
GridDataView 6 45
System32Int Error 8 44
Get list of word ducuments in a folder 10 21
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

746 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

13 Experts available now in Live!

Get 1:1 Help Now