Solved

Return Cell from Selected row in datagridview

Posted on 2008-10-28
3
881 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
MediaHelp 4 23
Please explain the parts of these 2 LINQ expressions 3 30
C# Gridview 1 32
parsing JSON help 1 9
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

777 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