Search code examples
c#entity-frameworkdevexpressdevexpress-gridcontrol

How to get TableName from gridview?


I m using entity framework an I fill gridcontrol like;

  Models.dbContext ent = new Models.dbContext();      
  ent.Locations.Load();
  gridControl1.DataSource = ent.Locations.Local.ToBindingList();

then I m trying to get Table name like;

 DataTable dt = ((DataView)gridview1.DataSource).Table;
 Messagebox.Show(dt.TableName);

it gives me error: Unable to cast object of type 'System.Data.Entity.Internal.ObservableBackedBindingList`1[Models.Location]' to type 'System.Data.DataView'.

Why this happens? what is wrong with my code?


Solution

  • Something you could try is make an extension method for a DataGridView so it can be converted to a DataTable:

    public static class DataGridViewExtension 
    {
        // Method for: DataGridView to DataTable conversion
        public static DataTable ToTable(this DataGridView dataGridView, bool onlyVisible=false)
        {
            // Identifiers used are:
            int rowCount = dataGridView.Rows.Count;
            var dataTable = new DataTable();
            var columnTypes = new Dictionary<string, Type>();
            var rowItems = new List<object>();
    
            // Get the column names and types
            columnTypes = dataGridView.ColumnTypes(onlyVisible);
    
            // Setup the DataTable column structure
            dataTable.SetupColumns(columnTypes);
    
            // Get the rows of the DataGridView
            foreach (DataGridViewRow row in dataGridView.Rows)
            {
                // Get the row as a list to give to the table
                rowItems = row.ToList(onlyVisible);
                // Give the row items to the DataTable
                dataTable.Rows.Add(rowItems.ToArray());
            }
    
            // Return the data table
            return dataTable;
        }
    }
    

    where I have made 3 more extensions which are:

    public static class DataGridViewExtension 
    {
        // Class for: Getting the column names with column types from a DataGridView
        public static Dictionary<string, Type> ColumnTypes(this DataGridView dataGridView, 
            bool onlyVisible=false)
        {
            // Identifiers used are:
            var types = new Dictionary<string, Type>();
    
            // Go through the columns of the view
            foreach (DataGridViewColumn column in dataGridView.Columns)
            {
                if (onlyVisible == true && column.Visible == true)
                {
                    types[column.HeaderText] = column.ValueType;
                }
                else if (!onlyVisible)
                {
                    types[Column.HeaderText] = column.ValueType;
                }
            }
    
            // Return the results
            return types;
        }
    }
    
    public static class DataGridViewRowExtension
    {
        // Method for: Returning the cells of a row as a List<object>
        public static List<object> ToList(this DataGridViewRow dataGridViewRow,
            bool onlyVisible= false)
        {
            // Identifiers used are:
            var objectList = new List<object>();
    
            // Go through the row and add the items to the list
            foreach (DataGridViewCell cell in dataGridViewRow.Cells)
            {
                if (onlyVisible == true && cell.Visible == true)
                {
                    objectList.Add(cell.Value);
                }
                else if (!onlyVisible)
                {
                    objectList.Add(cell.Value);
                }
            }
    
            // Return the list
            return objectList;
        }
    }
    
    public static class DataTableExtension
    {
        // Method for: Setting up the column structure of a data table using a dictionary holding column name with data type
        public static void SetupColumns(this DataTable dataTable,
            Dictionary<string, Type> columns)
        {
            // Identifiers used are:
            int columnCount = columns.Count;
    
            // Set up the structure
            foreach (KeyValuePair<string, Type> column in columns)
            {
                dataTable.Columns.Add(column.Key, column.Value);
            }
        }
    }
    

    Although this is a round about way, you can at least then do the following

    string tableName = dataGridView.ToTable().TableName;
    

    of course this way is tedious but it works if you are in a hurry and useful if you need these extensions for other things.