Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Programmatically setting gridview selected value

Posted on 2011-03-21
18
Medium Priority
?
586 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

636 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