Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > converting a datagridviewrow to a datarow
 

converting a datagridviewrow to a datarow

I've got a datagridview, and I'm trying to create a new datatable and add each row in the datagridviewselectedrows collection into the datatable, but I can't find a way to convert a datagridviewrow into a datarow, or another way to accomplish this. Does anyone have any ideas how I can accomplish this?

Thanks,

smtraber
smtraber  Tuesday, March 14, 2006 7:18 AM

This is an example of how to do the task.


Dim dt As New DataTable

dt.Columns.Add("Column1", GetType(Integer))

dt.Columns.Add("Column2", GetType(String))

For Each dgvr As DataGridViewRow In MyDataGridView.SelectedRows

Dim dr As DataRow = dt.NewRow()

dr.Item("Column1") = dgvr.Cells("Cell1").Value

dr.Item("Column2") = dgvr.Cells("Cell2").Value

dt.Rows.Add(dr)

Next


vkh75  Tuesday, March 14, 2006 7:58 AM

This is an example of how to do the task.


Dim dt As New DataTable

dt.Columns.Add("Column1", GetType(Integer))

dt.Columns.Add("Column2", GetType(String))

For Each dgvr As DataGridViewRow In MyDataGridView.SelectedRows

Dim dr As DataRow = dt.NewRow()

dr.Item("Column1") = dgvr.Cells("Cell1").Value

dr.Item("Column2") = dgvr.Cells("Cell2").Value

dt.Rows.Add(dr)

Next


vkh75  Tuesday, March 14, 2006 7:58 AM

Here is an example that I found and seems to work for me in C#:

public static DataTable CreateTable(DataView obDataView)
{
if (null == obDataView)
{
throw new ArgumentNullException
("DataView", "Invalid DataView object specified");
}

DataTable obNewDt = obDataView.Table.Clone();
int idx = 0;
string[] strColNames = new string[obNewDt.Columns.Count];
foreach (DataColumn col in obNewDt.Columns)
{
strColNames[idx++] = col.ColumnName;
}

IEnumerator viewEnumerator = obDataView.GetEnumerator();
while (viewEnumerator.MoveNext())
{
DataRowView drv = (DataRowView)viewEnumerator.Current;
DataRow dr = obNewDt.NewRow();
try
{
foreach (string strName in strColNames)
{
dr[strName] = drv[strName];
}
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
}
obNewDt.Rows.Add(dr);
}


return obNewDt;
}

js123  Saturday, March 18, 2006 4:59 PM

If you want to retrieve a DataRow from a DataGridViewRow (for example starting from a DataGridViewSelectedRowCollection), you simplyneed to get the Row property of the DataRowView obtained byconverting theDataBoundItem object of the DataGridViewRow :

DataGridViewSelectedRowCollection rows = MyDataGridView.SelectedRows;
foreach (DataGridViewRow row in rows)
{
DataRow myRow = (row.DataBoundItem as DataRowView).Row;
// do something with your DataRow�/font>
}

Hope it helps

Nicolas Rafalowski  Tuesday, April 01, 2008 12:59 PM
datarow = datagridviewrow.row

datagridviewrow.row property is a datarow
RyanFK  Wednesday, July 02, 2008 7:19 PM
Say I have the setup my System.Windows.Forms.DataGridView control as follows:


class MyItem
{
public string propertyA;

public MyItem(string propertyA)
{
this.propertyA = propertyA;
}

public string PropertyA
{
get {return this.propertyA;}
set {this.propertyA = value;}
}
}

DataGridView dgv = new DataGridView();
//Skipping codes where it adds a column with DataPropertyName = "PropertyA"


MyItem item1 = new MyItem("AAAA");
MyItem item2 = new MyItem("BBBB");

MyItem[] myItemCollection = { item1, item2};

dgv.DataSource = myItemCollection;


Now if i iterate over the dgv.Rows will I still be able to convert the DataGridViewRow to DataRow?

For example:

foreach(DataGridViewRow row in dgv.Rows)
{
DataRow dr = row as DataRow; //Will this work?
}


If not how will I get information about row changes similar to the "RowState" property of the "DataRow" object?

You help will be appreciated.

Thank you,
Rajat



rbshrestha  Thursday, July 17, 2008 5:52 PM

DataRow
myRow = (row.DataBoundItem as DataRowView ).Row;


The line above gives me an "Object reference not set to an instance of an object" error :(
My datagridview is not empty... but aparently row.DataBoundItem is null.
Any idea how to fix this ?
silviu2k7  Saturday, May 16, 2009 1:27 PM
using Nicholas's example, I got this working to get DataRow for each datagriview row:

dataGridViewCodeTable.SelectAll();
DataRow dr;
foreach (DataGridViewRow row in rows) {
    dr = ((DataRowView)(row.DataBoundItem)).Row;
    ...
}


For some reasons I don't understand, one can't convert to DataRow from dataGridViewCodeTable.Rows but from SelectedRows

as for people interested in the current datagridview row, here is another way

DataRow row = ((DataRowView)this.myDataGridView.CurrentRow.DataBoundItem).Row; 
  • Proposed As Answer byfs - new to w7 Friday, September 18, 2009 1:16 AM
  • Edited byfs - new to w7 Friday, September 04, 2009 8:12 PMcorrected vb syntax to c# and added Current row conversion
  •  
fs - new to w7  Wednesday, September 02, 2009 12:17 AM

You can use google to search for other answers

Custom Search

More Threads

• DATAGRID
• Get DataType of DataGridView Columns
• [C#]load file.txt in datagridview
• BindingList bound to a list box. ResetItem calling SelectedIndexChanged
• Combo box binding
• Combo box populate data on entering
• how can i get a username or password from mydatabase??
• Bind the datagrid with two datatables
• BindingSources, nested relations, look-up table, help.
• binding certain rows of data?