Blog ( Of Ryan Olshan)

Recent Posts

News

About Ryan Olshan

LinkedIn Profile

Follow Ryan via twitter

Join StrongTypes - Free Generics Help By Email
Join StrongTypes

Community Credit Winner

View my MVP profile

The posts on this weblog are provided "as is" with no warranties and confer no rights. The opinions expressed herin are my own personal opinions and do not represent my employer's view in any way.

Subscribe to Rolog via My AOL
Subscribe to Rolog via Bloglines


Subscribe to Rolog via Feedburner

Furl Rolog




Subscribe to Rolog via Newsburst


Subscribe to Rolog via Pluck

Add Rolog to Technorati Favorites




Thycotic Secret Server

Receive Email Updates


.NET Websites

  • => StrongTypes.com <=
  • Community Credit
  • DotNetCharity.com
  • DotNetSlackers
  • EasyListBox
  • LearnAsp.com
  • SystemNetMail.com
  • SystemWebMail.com

Blogroll

  • Alex Campbell
  • Carlos Magalhaes
  • Charles Carroll
  • Chris Taylor
  • Darren Neimke
  • David Betz
  • Eric Smith
  • Rob Caron
  • Rob Howard
  • Sam Gentile
  • Scott Guthrie
  • Scott Mitchell
  • Tess
  • Wesner Moise
  • Jason Alexander
  • Bill Ryan
  • Steve Smith
  • Sahil Malik
  • David Silverlight

Other Websites

  • 911 Pigeon Alert
  • Pacific Wildlife Project

Strong Coder Bloggers

  • Dean Fiala
  • Doug Nelson
  • Kevin Jones

Yahoo! Groups

  • => StrongTypes <=
  • AspNet2
  • AspNet20GridView
  • PayPalDevDotNet
  • RubyOnRails
  • VSTS

Multiple DataKeys Null Conditional

Lets say you have multiple DataKeys assigned to the DataKeyNames property of the GridView control where only one DataKey will have a value and the rest will be null. Based on the non-null value, you want to trigger a conditional. I've discovered an easy to accomplish this is using Reflection and a GridView event. In this example, the value of my DataKeys are all of type integer.

VB.NET
Private Sub GridView_Created( ByVal sender As Object , ByVal e As GridViewRowEventArgs )
   
If e.Row.RowType = DataControlRowType.DataRow Then
       
Dim nKeyVal As Integer

       
If g ridView.DataKeys(e.Row.RowIndex).Values( "K eyName1" ).GetType() == Type .GetType( "System.Int32 " ) Then
            nKeyVal = 
g ridView .DataKeys(e.Row.RowIndex).Values ( "K eyName1" )

            ' Do something

        End If

        If g ridView.DataKeys(e.Row.RowIndex).Values ( "K eyName2" ).GetType() == Type .GetType( "System.Int32 " ) Then
            nKeyVal = 
g ridView .DataKeys(e.Row.RowIndex).Values ( "K eyName2" )

           
' Do something
        End If

        If gridView.DataKeys(e.Row.RowIndex).Values ( "K eyName3" ).GetType() == Type .GetType( "System.Int32 " ) Then
            nKeyVal = 
gridView .DataKeys(e.Row.RowIndex).Values( "K eyName3" )

            ' Do something
       
End If
    End If
End Sub

C#
private void GridView_Created( object sender, GridViewRowEventArgs e)
{
   
if (e.Row.RowType == DataControlRowType.DataRow)
    {
       
int nKeyVal;

       
if ( g ridView.DataKeys[e.Row.RowIndex].Values[ "K eyName1" ].GetType() == Type .GetType( "System.Int32 " ))
        {
            nKeyVal = (
int ) g ridView .DataKeys[e.Row.RowIndex].Values[ "K eyName1" ];

           
// Do something
        }

        if ( g ridView.DataKeys[e.Row.RowIndex].Values[ "K eyName2" ].GetType() == Type .GetType( "System.Int32 " ))
        {
            nKeyVal = (
int ) g ridView .DataKeys[e.Row.RowIndex].Values[ "K eyName2" ];

           
// Do something
        }

        if (gridView.DataKeys[e.Row.RowIndex].Values[ "K eyName3" ].GetType() == Type .GetType( "System.Int32 " ))
        {
            nKeyVal = (
int ) gridView .DataKeys[e.Row.RowIndex].Values[ "K eyName3" ];

            // Do something
        }

    }
}


Note: gridView is the ID of the GridView control.

R
Share this post: | bookmark it! | digg it! | reddit! | kick it! | live it!
Posted: Dec 06 2005, 09:53 AM by Ryan Olshan | with no comments
Filed under: ,

Comments

No Comments