3

Out of curiosity is it possible to open a form based on row selection in a datagrid? I would also need the form to show information based on the username in the datagrid. The persons username is included within the row of the datagrid.

6
  • Are you asking if there is some "Automatic" way to do this? Or if its possible at all? Mar 18, 2011 at 16:19
  • Well if its possible at all, ive had a look and I havnt found much information on the datagrid.
    – Joe Bell
    Mar 18, 2011 at 16:22
  • @JoeBell When you say open a form with details, could it be as simple as linking to another page with the Username of the person required, or do you want to catch the the selection on the ItemCommand and then do something with the selected person?
    – gdp
    Mar 18, 2011 at 16:40
  • @Joe Bell: Are you talking about Windows forms or ASP.NET web forms? You're getting mixed answers due to the ambiguity.
    – Misko
    Mar 18, 2011 at 16:59
  • Im talking about windows forms, and geepie that is what im trying to do (linking to another page with the username.)
    – Joe Bell
    Mar 18, 2011 at 17:25

3 Answers 3

4

You will have to code this, but yes, it is possible.

First, populate your DataGrid with data that you can handle.

On the DataGrid's Selection Changed event, read that data, create the form you want to show (if it does not already exist), and display it using Show().

This would be like a typical Menu program.

2

You can handle this under the following event

 dataGridView1_CellClick

Get the CurrentCell value of the datagridiview

Check for the username exists or not as per you asked and show the respective form

Sample code:

if (this.dataGridView1.CurrentCell != null) 
{
    string strusrname=dataGridView1.CurrentCell.Value.ToString();
    //Here find out for the user name from the string as you get the currentcell value of the datagridview
    // Raise the corresponding form as per you required
} 
2
  • Won't this will occur when any part of the cell is clicked, even the borders and padding? You would want to use the DataGridView1_CellContentClick.
    – gdp
    Mar 22, 2011 at 9:52
  • @Geppie : But as i had only a cell completely with a text so i used this may be it changes if he has different cells then he go for as you said
    – Developer
    Mar 22, 2011 at 9:58
0

Not really sure if this is what your after, as i don't no if you want to show the data on another pre-built form or create a new one, but here it goes. This way you won't even need to worry about the row selected, assuming you have the username of the person bound to the datagrid you can create a hyperlinkcolumn like this:

<asp:HyperLinkcolumn DataNavigateUrlField="Username" 
                     DataNavigateUrlFormatString="PersonForm.aspx?Username={0}"     
                     HeaderText="More Details" 
                     Text="View Person Details" />

Then the PersonForm can load the persons details. Or if you would like some help on how to catch the selected row on itemcommand then let me no.

Hope this helps.

EDIT: after your winforms tag update you may be interested in this: DataGridViewLink On MSDN

The general code is:

DataGridViewLinkColumn links = new DataGridViewLinkColumn();

links.UseColumnTextForLinkValue = true;
links.HeaderText = ColumnName.ReportsTo.ToString();
links.DataPropertyName = //Set your field here.
links.ActiveLinkColor = Color.White;
links.LinkBehavior = LinkBehavior.SystemDefault;
links.LinkColor = Color.Blue;
links.TrackVisitedState = true;
links.VisitedLinkColor = Color.YellowGreen;

DataGridView1.Columns.Add(links);

Once you have added a link you can catch it using DataGridView1_CellContentClick and do what you want with it, ie open a new form or alter the current one.

2
  • He asked for Winforms not for Web application
    – Developer
    Mar 22, 2011 at 9:33
  • @Dorababu yes and the first web application part was initially there when the question was still vague. I just added the windows.forms code smaple and link in my edit.
    – gdp
    Mar 22, 2011 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.