Welcome to Strong Coders Community Sign in | Join | Help

Blog ( Of Ryan Olshan)

News

  • About Ryan Olshan

    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

.NET Websites

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

Blogroll

  • Alex Campbell
  • Carlos Magalhaes
  • 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
  • Kevin Jones

Thycotic Bloggers

  • Bryant Smith
  • John Morales
  • Jonathan Cogley

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: Tuesday, December 06, 2005 9:53 AMby Ryan Olshan
Filed under: ,

Comments

No Comments

New Comments to this post are disabled