Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从gridview获取TableName?_C#_Entity Framework_Devexpress_Devexpress Gridcontrol - Fatal编程技术网

C# 如何从gridview获取TableName?

C# 如何从gridview获取TableName?,c#,entity-framework,devexpress,devexpress-gridcontrol,C#,Entity Framework,Devexpress,Devexpress Gridcontrol,我正在使用实体框架和我填充的网格控件 Models.dbContext ent = new Models.dbContext(); ent.Locations.Load(); gridControl1.DataSource = ent.Locations.Local.ToBindingList(); 然后我试着得到表名,比如 DataTable dt = ((DataView)gridview1.DataSource).Table; Messagebox.Show(

我正在使用实体框架和我填充的网格控件

  Models.dbContext ent = new Models.dbContext();      
  ent.Locations.Load();
  gridControl1.DataSource = ent.Locations.Local.ToBindingList();
然后我试着得到表名,比如

 DataTable dt = ((DataView)gridview1.DataSource).Table;
 Messagebox.Show(dt.TableName);
它给了我错误:无法将类型为“System.Data.Entity.Internal.ObservableBackedBindingList`1[Models.Location]”的对象强制转换为类型为“System.Data.DataView”


为什么会发生这种情况?我的代码有什么问题?

您可以尝试为DataGridView创建一个扩展方法,以便将其转换为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;
    }
}

当然,这种方法很枯燥,但如果您很匆忙,它会起作用,如果您需要这些扩展来做其他事情,它会很有用。

gridControl1.DataSource=ent.Locations.Local.ToBindingList()
您将
DataSource
设置为英语,然后期望从
DataSource
获取
DataView
。绑定列表!=DataViewNow我看到了,但这是我第一次使用BindingList,在这种情况下如何获取TableName?您可以将TableName存储在
标记
属性或TableFrom BindingList-任务不可能。将表名存储在其他地方。因此,我必须了解有关列表的更多信息。谢谢
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);
        }
    }
}
string tableName = dataGridView.ToTable().TableName;