Code Snippets

  

C# Source Code


Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 99,782 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 1,534 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! 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: 4,922

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.





Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month
-->