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



Answer this question

converting a datagridviewrow to a datarow

  • Patrick.I

    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;
    }



  • EvelynR

    This is an example of how to do the task.

  • converting a datagridviewrow to a datarow