Solved

Return Cell from Selected row in datagridview

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

15 Experts available now in Live!

Get 1:1 Help Now