Click here to Skip to main content
12,078,489 members (45,720 online)
Click here to Skip to main content
Add your own
alternative version

Stats

38.9K views
494 downloads
22 bookmarked
Posted

GridView selection by key

, 12 Feb 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Programmatically selecting an item on a GridView using only the DataKey value.

imgF.jpg

Introduction

When using declarative data binding, you will sometimes run into a situation where you need to programmatically set the selection on a GridView control. The problem you may encounter is that the GridView control can only be set by its row index value. Usually, you only possess the key value.

This article demonstrates using an Extension Method to allow you to programmatically set the selected value on a GridView using the key value on the GridView.

The Code

In this example, the dropdown control and the GridView control are bound to the same data source. When the dropdown control is changed, this line is called:

GridView1.SetRowValueValueByKey(DropDownList1.SelectedValue);

This calls the SetRowValueValueByKey Extension Method to set the selected value on the GridView. Below is the complete code for the code-behind:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace UsingDataKeys
{
    public partial class GridViewSelectionUsingDataKeys : System.Web.UI.Page
    {
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridView1.SetRowValueValueByKey(DropDownList1.SelectedValue);
        }
    }

    public static class GridViewExtensions
    {
        public static void SetRowValueValueByKey(this GridView GridView, 
                                                 string DataKeyValue)
        {
            int intSelectedIndex = 0;
            int intPageIndex = 0;
            int intGridViewPages = GridView.PageCount;
        
            // Loop thru each page in the GridView
            for (int intPage = 0; intPage < intGridViewPages; intPage++)
            {
                // Set the current GridView page
                GridView.PageIndex = intPage;
                // Bind the GridView to the current page
                GridView.DataBind();
                // Loop thru each DataKey in the GridView
                for (int i = 0; i < GridView.DataKeys.Count; i++)
                {
                    if (Convert.ToString(GridView.DataKeys[i].Value) == DataKeyValue)
                    {
                        // If it is a match set the variables and exit
                        intSelectedIndex = i;
                        intPageIndex = intPage;
                        break;
                    }
                }
            }

            // Set the GridView to the values found
            GridView.PageIndex = intPageIndex;
            GridView.SelectedIndex = intSelectedIndex;
            GridView.DataBind();
        }
    }
}

Points of Interest

  • You must have the DataValueField set on the GridView for this to work.
  • Currently, this code does not handle composite keys; however, it can be modified to do so.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

defwebserver
Software Developer (Senior) http://ADefWebserver.com
United States United States
Michael Washington is a Microsoft MVP. He is a ASP.NET and
C# programmer.
He is the founder of
LightSwitchHelpWebsite.com

He has a son, Zachary and resides in Los Angeles with his wife Valerie.

He is the Author of:

You may also be interested in...

Comments and Discussions

 
QuestionVery Useful Pin
Stretch200cm22-Feb-14 9:02
memberStretch200cm22-Feb-14 9:02 
QuestionDataBind in the loop?? Pin
ak9937212-Nov-08 9:58
memberak9937212-Nov-08 9:58 
AnswerRe: DataBind in the loop?? Pin
defwebserver12-Nov-08 14:03
memberdefwebserver12-Nov-08 14:03 
Questiondisplay grid view like as excel sheet with minimum empty rows Pin
vardhan.menta2-May-08 0:21
membervardhan.menta2-May-08 0:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.160212.1 | Last Updated 12 Feb 2008
Article Copyright 2008 by defwebserver
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid