Code Snippets

  

C# Source Code


Welcome to Dream.In.Code
Become a C# Expert!

Join 148,616 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,287 people online right now. Registration is fast and FREE... Join Now!





DataGridView - Get Selected List

Returns a list of all DataRows selected in DataGridView.

Submitted By: baavgai
Actions:
Rating:
Views: 8,454

Language: C#

Last Modified: December 31, 1969

Snippet


  1. public List<DataRow> GetSelected(DataGridView dgv) {
  2.         List<DataRow> list = new List<DataRow>();
  3.         if (dgv.SelectedRows == null || dgv.SelectedRows.Count == 0) {
  4.                 return list;
  5.         }
  6.         foreach (DataGridViewRow dgvRow in dgv.SelectedRows) {
  7.                 DataRow row = null;
  8.                 try {
  9.                         DataRowView drv = (DataRowView)dgvRow.DataBoundItem;
  10.                         row = drv.Row;
  11.                 } catch (Exception ex) {
  12.                         Debug.WriteLine("Error: " + ex.Message);
  13.                 }
  14.                 if (row == null) { continue; }
  15.                 list.Add(row);
  16.         }
  17.         return list;
  18.         // I prefer the List<DataRow> generic, if you prefer arrays then
  19.         // changed the return type to DataRow[], comment out the prior
  20.         // reuturn and use this one
  21.         // return list.ToArray();
  22. }

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month