Solved

Programmatically setting gridview selected value

Posted on 2011-03-21
18
580 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

840 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