Blog

How to Get the Selected Cells and Rows in the Windows Forms DataGridView Control

Posted by Hemant Srivastava Blogs | Windows Controls C# Oct 19, 2012
Here I am showing how to get selected cells/rows in DataGridView Control and basically use of IEnumerator while accessing them.

Here I am showing how to get selected cells/rows in DataGridView Control and basically use of IEnumerator while accessing them. In the following code, dataGrid_ZipList is DataGridView Control. We store all the selected rows into a DataGridViewSelectedRowCollection object. Then we put them in an enumerator and iterate the collection one by one and keep on adding the one cell item (Postal Code in this case) of selected row into a List (say lstPostalCodes).


DataGridViewSelectedRowCollection SelectRowsSet = dataGrid_ZipList.SelectedRows;

DataGridViewRow row;

IEnumerator Enumerator = SelectRowsSet.GetEnumerator();

Enumerator.Reset();        

while (Enumerator.MoveNext())

{

      row = (DataGridViewRow)Enumerator.Current;

      lstPostalCodes.Add(row.Cells["POSTAL_CODE"].Value.ToString());                        

}

post comment
     

I will be feeling good if you explained the code line by line.

Posted by Perumal Prasanth S Oct 20, 2012
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts