Solved

Return Cell from Selected row in datagridview

Posted on 2008-10-28
3
883 Views
Last Modified: 2013-11-26
I have a datagrid view with 8 columns.  The user selects a datarow from the table.  How do I send the cell from column2 in the selected datarow to List<string>?  Also, I want to add unique data to the List, thus if "x" already exists in the list, it will not add it again.
0
Comment
Question by:MichelleLacy
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
jorgesv13 earned 500 total points
ID: 22825688
1. You can access the SelectedRow, and then the Cell.
2. You have to use the Contains method of a list

    ArrayList li = new ArrayList();
    DataGridView DataGridView1 = new DataGridView();
    if (DataGridView1.SelectedRows.Count > 0) {
        string value = DataGridView1.SelectedRows(0).Cells("Column2").Value;
        // Replace Column2 with the name of your second column
        if (!li.Contains(value)) li.Add(value); //add the value if is not yet in the list
    }

Open in new window

0
 

Author Closing Comment

by:MichelleLacy
ID: 31510919
thanks so much!!!
0
 

Author Comment

by:MichelleLacy
ID: 22833218
string value = DataGridView1.SelectedRows(0).Cells("Column2").Value;
Please note the type correction:  brackets instead of parentheses
string value = DataGridView1.SelectedRows[0].Cells["Column2"].Value;
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Desktop Application 3 54
MVC DDL Json Not Binding to Model 2 27
Iteration Help (Asp.net VB) 5 24
Need syntax help building hyperlink 2 25
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 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