Solved

Return Cell from Selected row in datagridview

Posted on 2008-10-28
3
878 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
I need help finding an unknown string value within a string in my VB.Net application 3 37
Syntax error 9 45
Run time Error 4 34
Help with C#, MVC, razor. 6 23
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

910 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

21 Experts available now in Live!

Get 1:1 Help Now