如何在代码优先实体框架6.1中绑定数据网格视图 [英] How Do I Bind Data Grid View In Code First Entity Frame 6.1
问题描述
我正在使用该方法绑定数据网格视图但我面临错误指导我..
void BindGrid()
{
使用(Model1 context = new Model1())
{
if (context.tblEmps.Count()> 0 )
{
gridSample.DataSource = context.tblEmps;
gridSample.DataBind();
}
其他
{
var obj = new 列表< tblEmp>();
obj.Add( new tblEmp());
// 将包含空白行的DataTable绑定到GridView
gridSample .DataSource = obj;
gridSample.DataBind();
int columnsCount = gridSample.Columns.Count;
gridSample.Rows [ 0 ]。Cells.Clear(); // 清除行中的所有单元格
gridSample.Rows [ 0 ]。Cells.Add( new TableCell()); // 添加新的空白单元格
gridSample.Rows [ 0 ]。单元格[ 0 ]。ColumnSpan = columnsCount; // 将列范围设置为新添加的单元格
// 您可以在此设置样式
gridSample.Rows [ 0 ]。单元格[ 0 ]。HorizontalAlign = HorizontalAlign.Center;
gridSample.Rows [ 0 ]。单元格[ 0 ]。ForeColor = System.Drawing。红色;
gridSample.Rows [ 0 ]。单元格[ 0 ]。Font.Bold = 真跨度>;
// 设置未找到新添加的单元格的结果
gridSample.Rows [ 0 ]。单元格[ 0 ]。文本= 找不到结果!;
}
}
}
受保护 void gridSample_RowDataBound( object sender,GridViewRowEventArgs e)
{
DropDownList ddl = null 跨度>;
if (e.Row.RowType == DataControlRowType.Footer)
{
ddl = e.Row.FindControl( ddlDeprt) as DropDownList;
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
ddl = e.Row .FindControl( ddlDeprt) as 下拉列表;
}
如果(ddl!= null )
{
使用(Model1 context = new Model1())
{
ddl.DataSource = context.tblDepts;
ddl.DataTextField = DeptName;
ddl.DataValueField = DeptID;
ddl.DataBind();
ddl.Items.Insert( 0 , new ListItem( ));
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
ddl.SelectedValue =( (tblDept)(e.Row.DataItem))DeptID.ToString();
}
}
}
]
面临错误......... ........
不支持直接与商店查询(DbSet,DbQuery,DbSqlQuery,DbRawSqlQuery)绑定数据。而是使用数据填充DbSet,例如通过在DbSet上调用Load,然后绑定到本地数据。对于WPF绑定到DbSet.Local。对于WinForms绑定到DbSet.Local.ToBindingList()。对于ASP.NET WebForms,您可以绑定到查询上调用ToList()的结果或使用模型绑定,有关详细信息,请参阅http://go.microsoft.com/fwlink/?LinkId=389592.
http://techbrij.com/insert-update- delete-gridview-entity-framework [ ^ ]
i am using that method to binding data grid view but i facing error guide me..
void BindGrid()
{
using (Model1 context = new Model1())
{
if (context.tblEmps.Count() > 0)
{
gridSample.DataSource = context.tblEmps;
gridSample.DataBind();
}
else
{
var obj = new List<tblEmp>();
obj.Add(new tblEmp());
// Bind the DataTable which contain a blank row to the GridView
gridSample.DataSource = obj;
gridSample.DataBind();
int columnsCount = gridSample.Columns.Count;
gridSample.Rows[0].Cells.Clear();// clear all the cells in the row
gridSample.Rows[0].Cells.Add(new TableCell()); //add a new blank cell
gridSample.Rows[0].Cells[0].ColumnSpan = columnsCount; //set the column span to the new added cell
//You can set the styles here
gridSample.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;
gridSample.Rows[0].Cells[0].ForeColor = System.Drawing.Color.Red;
gridSample.Rows[0].Cells[0].Font.Bold = true;
//set No Results found to the new added cell
gridSample.Rows[0].Cells[0].Text = "NO RESULT FOUND!";
}
}
}
protected void gridSample_RowDataBound(object sender, GridViewRowEventArgs e)
{
DropDownList ddl = null;
if (e.Row.RowType == DataControlRowType.Footer)
{
ddl = e.Row.FindControl("ddlDeprt") as DropDownList;
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
ddl = e.Row.FindControl("ddlDeprt") as DropDownList;
}
if (ddl != null)
{
using (Model1 context = new Model1())
{
ddl.DataSource = context.tblDepts;
ddl.DataTextField = "DeptName";
ddl.DataValueField = "DeptID";
ddl.DataBind();
ddl.Items.Insert(0, new ListItem(""));
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
ddl.SelectedValue = ((tblDept)(e.Row.DataItem)).DeptID.ToString();
}
}
}
]
Error facing.................
Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery, DbRawSqlQuery) is not supported. Instead populate a DbSet with data, for example by calling Load on the DbSet, and then bind to local data. For WPF bind to DbSet.Local. For WinForms bind to DbSet.Local.ToBindingList(). For ASP.NET WebForms you can bind to the result of calling ToList() on the query or use Model Binding, for more information see http://go.microsoft.com/fwlink/?LinkId=389592.
http://techbrij.com/insert-update-delete-gridview-entity-framework[^]
这篇关于如何在代码优先实体框架6.1中绑定数据网格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!