Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to select a Record from a DataGrid...

Posted on 2005-04-17
1
Medium Priority
?
343 Views
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.

Thanks
0
Comment
Question by:mhertzDEV
1 Comment
 
LVL 21

Accepted Solution

by:
Yurich earned 2000 total points
ID: 13802813
hello,
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

regards




0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

810 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