Click here to Skip to main content
Click here to Skip to main content

How To Create a DataSet from Multiple Selected Rows in a DataGrid

By , 6 Jun 2006
 

Introduction

I searched and searched the Internet for an easy way to take the rows that my users selected in a DataGrid and insert them into a DataSet or some other form of container that I could then make use of in my code. More specifically, when the user would sort and select multiple rows.

While I did find articles that helped, I came up short of the vision of creating an actual DataSet. One of these articles was off of The Code Project Web site published by Chad Z. Hower aka Kudzu called "Obtaining Current DataTable Row for a DataGrid". This was helpful if my users only selected one row but did not achieve the multi-row requirement.

I am not sure how advanced or basic this really is. All I know is that my searches kept coming up short. Since I am an avid user of code that I find on the Internet, I felt it was my moral obligation to give something back and that this was my chance to do so.

Code

public static DataRow GetCurrentDataRow(DataGrid aGrid)

// Gets Currently Selected Row in a Datagrid.
{
CurrencyManager xCM = 
    (CurrencyManager)aGrid.BindingContext[aGrid.DataSource, aGrid.DataMember];
DataRowView xDRV = (DataRowView)xCM.Current;
return xDRV.Row;
}

public static ArrayList GetSelectedRowsArray(DataGrid aGrid)

//Returns an ArrayList containing the indexes of selected rows in a DataGrid 
{ 
   ArrayList al = new ArrayList(); 
   CurrencyManager cm = 
        (CurrencyManager)aGrid.BindingContext[aGrid.DataSource, aGrid.DataMember]; 
   DataView dv = (DataView)cm.List; 
   for(int i = 0; i < dv.Count; ++i) 
   { 
     if(aGrid.IsSelected(i))
        al.Add(i); 
   } 
   return al; 
}

GetSelectedRowsDataSet(DataGrid aGrid)

//Returns a DataSet containing information for selected rows of a DataGrid.
{ 
   //DataSet and DataTable objects used in creating the new DataSet. 
   DataSet ds = new DataSet(); 
   DataTable table = new DataTable(); 
   try 
   { 
      //Use the CurrencyManager to create a data view so that you can 
      //use this to create the table schema. 
      CurrencyManager cm = 
        (CurrencyManager)aGrid.BindingContext[aGrid.DataSource, aGrid.DataMember]; 
      DataView dv = (DataView)cm.List; 
      //Add columns from dataview to table 
      foreach(DataColumn col in dv.Table.Columns) 
      { 
         table.Columns.Add(col.ColumnName); 
      } 

      //Using the GetSelectedRowsArray function loop through each of the selected 
      //row indexes setting the CurrentRowIndex property of your grid. Then import 
      //the current row using the GetCurrentDataRow function. 
      foreach(int i in GetSelectedRowsArray(aGrid)) 
      { 
         aGrid.CurrentRowIndex = i; 
         table.ImportRow(GetCurrentDataRow(aGrid));      
      } 
      //Add your new table to the DataSet and return it. 
      ds.Tables.Add(table); 
      return ds; 
   } 
   catch (System.Exception se) 
   { 
      //Error has occurred 
      throw se; 
   } 
} 

Anyway, I hope this is helpful. Any ideas on how to improve would be helpful. Thanks and happy coding.

History

  • 6th June, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Blexrude
Web Developer
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberMember 86594317 Apr '12 - 23:26 
GeneralDataGridViewmemberredware.com25 Jan '09 - 19:14 
Generalget only selected rows in a datagridmemberMember #352162523 Jan '07 - 19:46 
GeneralRe: get only selected rows in a datagridmemberBlexrude24 Jan '07 - 4:22 
GeneralThanks!membersillett10 Nov '06 - 10:29 
Questionwhen i sort the datagrid,cann't get the selected rows! urgent!memberantgao29 Oct '06 - 17:04 
when user click a field to sort
select 1 record,the value is value of old row,not the new value
how to do it??
AnswerRe: when i sort the datagrid,cann't get the selected rows! urgent!memberBlexrude30 Oct '06 - 5:09 
QuestionHelp?memberttphp6 Jun '06 - 18:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 6 Jun 2006
Article Copyright 2006 by Blexrude
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid