KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
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 (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; } I use: dtCurrent = dataGridView1.ToDataTable(dtCurrent.TableName); The code do
Tags (comma-separated)
Save Edits
Cancel