Solved

Programmatically setting gridview selected value

Posted on 2011-03-21
18
582 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

691 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