I have one form that is a CRUD, this form can manage any data from many tables, if a table has foreign keys the CRUD found the tables and columns for respectives columns of the current table, so in the DataGridView can show columns as CheckBox, TextBox or ComboBox
I do all this BEFORE the DataGridView is filled with the data, so I can't use this:
dataGridView1.DataSource = dtCurrent;
I need something like this:
dtCurrent = dataGridView1.DataSource;
But just give a null
I have tried with ExtensionMethod to the 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 (Da开发者_开发知识库taGridViewRow 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;
}
I use:
dtCurrent = dataGridView1.ToDataTable(dtCurrent.TableName);
The code does not works when:
int affectedUpdates = dAdapter.Update(dtCurrent);
I get an Exception about duplicated values (translated from spanish):
The changes requested to the table were not successful because they would create duplicate values in the index, primary key or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again.
I just need to Update changes in the DataGridView using DataTable
alternatively this may help toconvert datagrigview to datatable.
Dim dt As New DataTable
dt = (DirectCast(DataGridView1.DataSource, DataTable))
do a direct casting on datagridview for datatable can get the final result.
If you need a reference to the actual DataTable source, try this
((DataRowView)DataGridView1.Rows[0].DataBoundItem).DataView.Table
don't forget to process errors.
I wrote something like this:
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);
What about this :
DataView dv = (DataView)(dataGridView1.DataSource);
dt = dv.ToTable();
Works for all case!
精彩评论