繁体   English   中英

从 DataGridView 获取 DataTable

[英]Get DataTable from DataGridView

我有一个 CRUD 表单,这个表单可以管理来自多个表的任何数据,如果一个表有外键,CRUD 会找到当前表的相应列的表和列,因此在 DataGridView 中可以将列显示为 CheckBox,文本框或组合框

在 DataGridView 填充数据之前,我做了所有这些,所以我不能使用这个:

dataGridView1.DataSource = dtCurrent;

我需要这样的东西:

dtCurrent = dataGridView1.DataSource;

但只要给一个空值

我已经尝试将ExtensionMethod用于 DataGridView:

public static DataTable ToDataTable(this DataGridView dataGridView, string tableName)
{

    DataGridView dgv = dataGridView;
    DataTable table = new DataTable(tableName);

    // Crea las columnas 
    for (int iCol = 0; iCol < dgv.Columns.Count; iCol++)
    {
        table.Columns.Add(dgv.Columns[iCol].Name);
    }

    /**
      * THIS DOES NOT WORK
      */
    // Agrega las filas 
    /*for (int i = 0; i < dgv.Rows.Count; i++)
    {
        // Obtiene el DataBound de la fila y copia los valores de la fila 
        DataRowView boundRow = (DataRowView)dgv.Rows[i].DataBoundItem;
        var cells = new object[boundRow.Row.ItemArray.Length];
        for (int iCol = 0; iCol < boundRow.Row.ItemArray.Length; iCol++)
        {
            cells[iCol] = boundRow.Row.ItemArray[iCol];
        }

        // Agrega la fila clonada                 
        table.Rows.Add(cells);
    }*/

    /* THIS WORKS BUT... */
    foreach (DataGridViewRow row in dgv.Rows)
    {

        DataRow datarw = table.NewRow();

        for (int iCol = 0; iCol < dgv.Columns.Count; iCol++)
        {
            datarw[iCol] = row.Cells[iCol].Value;
        }

        table.Rows.Add(datarw);
    }

    return table;
} 

我用:

dtCurrent = dataGridView1.ToDataTable(dtCurrent.TableName);

该代码在以下情况下不起作用:

int affectedUpdates = dAdapter.Update(dtCurrent);

我收到关于重复值的异常(从西班牙语翻译):

请求对表的更改不成功,因为它们会在索引、主键或关系中创建重复值。 更改包含重复数据的一个或多个字段中的数据、删除索引或重新定义索引以允许重复条目并重试。

我只需要使用 DataTable 更新 DataGridView 中的更改

或者,这可能有助于将 datagrigview 转换为数据表。

Dim dt As New DataTable
dt = (DirectCast(DataGridView1.DataSource, DataTable))

对数据表直接在 datagridview 上进行转换可以获得最终结果。

如果您需要对实际 DataTable 源的引用,请尝试此操作

 ((DataRowView)DataGridView1.Rows[0].DataBoundItem).DataView.Table 

不要忘记处理错误。

我写了这样的东西:

private DataTable GetDataGridViewAsDataTable(DataGridView _DataGridView) 
{
    try {
        if (_DataGridView.ColumnCount == 0) return null;
        DataTable dtSource = new DataTable();
        //////create columns
        foreach(DataGridViewColumn col in _DataGridView.Columns) {
            if (col.ValueType == null) dtSource.Columns.Add(col.Name, typeof(string));
            else dtSource.Columns.Add(col.Name, col.ValueType);
            dtSource.Columns[col.Name].Caption = col.HeaderText;
        }
        ///////insert row data
        foreach(DataGridViewRow row in _DataGridView.Rows) {
            DataRow drNewRow = dtSource.NewRow();
            foreach(DataColumn col in dtSource.Columns) {
                drNewRow[col.ColumnName] = row.Cells[col.ColumnName].Value;
            }
            dtSource.Rows.Add(drNewRow);
        }
        return dtSource;
    }
    catch {
        return null;
    }
}
 DataTable dt = new DataTable();
    dt = ((DataTable)DataGridView1.DataSource);

那这个呢 :

DataView dv = (DataView)(dataGridView1.DataSource);

dt = dv.ToTable();

适用于所有情况!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM