How to select a Record from a DataGrid...

Posted on 2005-04-17
Last Modified: 2012-05-05
Hello I have a DataGrid in my C# .NET 2003 Windows App that is populed from a StoreProcedure.  

When someone clicks one of the row or record, i need a field called "ID" from that record be submited to my DetailForm
so that the customers information can be loaded. I also need to know how my DetailForm is going to have access to that
variable that is going to be sent to.

Question by:mhertzDEV
    1 Comment
    LVL 21

    Accepted Solution

    to get a field off your data grid, you can try the following:

    int row = dg.CurrentRowIndex;      // get the selected row

    if( row >= 0 )       // a little protection
       // i presume you know the column number of your ID
       int id = Convert.ToInt32( dg[ row, 0 /* that's your column ID, may not be 0 */ ]);

       // then you can pass this id to whatever form you want...

    I'm not sure I understood the second part of your question -

    "I also need to know how my DetailForm is going to have access to that
    variable that is going to be sent to."

    Do you want to know how to send a variable to a newly created form?
    There're really quite a few ways to do it, first you can specify a constructor of this form to have a parameter and pass the id when initializing the form -

    // somewhere in your detail form class
    private int customerId;

    // that's your constructor - add a new one or change existing (if you're not going to crate the form without
    // passing parameters) - the former is safer
    public DetailForm( int id )
         customerId = id;

    // then when you initiate our detail form in your main form:
    DetailForm dt = new DetailForm( id );

    another way is to make a public function in your DetailForm class:
    // in your DetailForm
    public void SetID( id )
       customerID = id;

    // in your main form
    DetailForm dt = new DetailForm( id );
    dt.SetID( id );

    another way is to may public property in your detail form:
    // in your DetailForm class
    private int customerID;
    public int CustomerID
       get{ return customerID; }
       set{ customerID = value; }

    // and in your main form class:
    DetailForm dt = new DetailForm( id );
    dt.CustomerID = id;

    if you want to get it FROM your DetailForm, it's a bit tricky, again several ways, I'll show you one:
    // have some public function, property or some way of providing id from your main form:
    public int ID // that's a property
       get{ return id; }

    // in your main form while initiating your detail form
    DetailForm dt = new DetailForm( id );
    dt.Parent = this; // set a parent of your form detail form to be your main form

    // and somewhere where you want to access your id:
    customerID = (( MainForm )Parent ).ID; // MainForm - is the name of your main form (Form1) or whatever



    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now