我找到了很多文章,我制作了这段代码
我有这个错误
无法将“AllProjects_Result”类型的对象转换为“Project”类型。
<asp:GridView ID="GVAllProjects" runat="server"
OnRowDataBound="GVAllProjects_RowDataBound"
OnRowDeleting="GVAllProjects_RowDeleting"
OnRowEditing="GVAllProjects_RowEditing"
OnRowUpdating="GVAllProjects_RowUpdating"
AutoGenerateColumns="False" AllowPaging="True" PageSize="5" DataKeyNames="ID">
<Columns>
<asp:CommandField ButtonType="Link" ShowEditButton="true" ShowDeleteButton="true" ItemStyle-Width="150" />
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="lblProID" Visible="false" runat="server" Text='<%# Eval("ID")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="المشروعات" ItemStyle-Width="150" >
<ItemTemplate>
<asp:Label ID="lblProjectName" runat="server" Text='<%# Eval("ProjectName")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtProjectName" runat="server" Text='<%# Eval("ProjectName") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="الموقع" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblDistrictName" runat="server" Text='<%# Eval("DistrictName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlDistrictName" runat="server">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="valDistrictName" runat="server" ControlToValidate="ddlDistrictName"
Display="Dynamic" ErrorMessage="DistrictName is required." ForeColor="Red" SetFocusOnError="True"
ValidationGroup="editGrp">*</asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlDistrictNameNew" runat="server">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="valDistrictNameNew" runat="server" ControlToValidate="ddlDistrictNameNew"
Display="Dynamic" ErrorMessage="DistrictName is required." ForeColor="Red" SetFocusOnError="True"
ValidationGroup="newGrp">*</asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="المساحات" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblAreas" runat="server" Text='<%# Eval("Areas")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtAreas" runat="server" Text='<%# Eval("Areas") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="نظام السداد" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblPaymentSystem" runat="server" Text='<%# Eval("PaymentSystem")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtPaymentSystem" runat="server" Text='<%# Eval("PaymentSystem") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="تاريخ الاستلام" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblReceivedDate" runat="server" Text='<%# Eval("ReceivedDate", "{0:d}")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditDate" runat="server"></asp:TextBox>
<asp:ImageButton runat="server" ID="imgPopup" ImageUrl="~/images/-calendar.png" ImageAlign="Bottom" />
<ajaxToolkit:calendarextender popupbuttonid="imgPopup" id="CalendarExtender1" runat="server" targetcontrolid="txtEditDate"
format="MM/dd/yyyy" enabled="true" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddDate" runat="server"></asp:TextBox>
<asp:ImageButton runat="server" ID="imgPopup" ImageUrl="~/images/-calendar.png" ImageAlign="Bottom" />
<ajaxToolkit:calendarextender id="CalendarExtender2" runat="server"
targetcontrolid="txtAddDate" popupbuttonid="imgPopup" enabled="true" format="dd/MM/yyyy" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="تصنيف العقار" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblClassification" runat="server" Text='<%# Eval("PropertyClassification")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtClassification" runat="server" Text='<%# Eval("PropertyClassification") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="صورة المشروع" ItemStyle-Width="150">
<ItemTemplate>
<img src='ProjectsImages/<%# Eval("ProjectImage") %>' style="width:120px;height:120px;" />
</ItemTemplate>
<EditItemTemplate>
<asp:FileUpload ID="UploadPhoto" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="الغرض" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblPurposeName" runat="server" Text='<%# Bind("PurposeName")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlPurposeName" runat="server">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="valPurposeName" runat="server" ControlToValidate="ddlPurposeName"
Display="Dynamic" ErrorMessage="PurposeName is required." ForeColor="Red" SetFocusOnError="True"
ValidationGroup="editGrp">*</asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlPurposeNameNew" runat="server">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="valClassificationNew" runat="server" ControlToValidate="ddlPurposeNameNew"
Display="Dynamic" ErrorMessage="DistrictName is required." ForeColor="Red" SetFocusOnError="True"
ValidationGroup="newGrp">*</asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="النوع" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblTypes" runat="server" Text='<%# Bind("TypeName")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlTypes" runat="server">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="valTypes" runat="server" ControlToValidate="ddlTypes"
Display="Dynamic" ErrorMessage="Types is required." ForeColor="Red" SetFocusOnError="True"
ValidationGroup="editGrp">*</asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlTypesNew" runat="server">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="valTypesNew" runat="server" ControlToValidate="ddlTypesNew"
Display="Dynamic" ErrorMessage="Types is required." ForeColor="Red" SetFocusOnError="True"
ValidationGroup="newGrp">*</asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
这是我的代码:
protected void GVAllProjects_RowDataBound(object sender, GridViewRowEventArgs e)
{
DropDownList ddlDistrictName = null;
DropDownList ddlPurposeName = null;
DropDownList ddlTypes = null;
if (e.Row.RowType == DataControlRowType.Footer)
{
ddlDistrictName = e.Row.FindControl("ddlDistrictNameNew") as DropDownList;
ddlPurposeName = e.Row.FindControl("ddlPurposeNameNew") as DropDownList;
ddlTypes = e.Row.FindControl("ddlTypesNew") as DropDownList;
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
ddlDistrictName = e.Row.FindControl("ddlDistrictName") as DropDownList;
ddlPurposeName = e.Row.FindControl("ddlPurposeName") as DropDownList;
ddlTypes = e.Row.FindControl("ddlTypes") as DropDownList;
}
if (ddlDistrictName != null)
{
using (AlamaarRealEstateEntities context = new AlamaarRealEstateEntities())
{
ddlDistrictName.DataSource = context.Districts.ToList();
ddlDistrictName.DataTextField = "DistrictName";
ddlDistrictName.DataValueField = "ID";
ddlDistrictName.DataBind();
ddlPurposeName.Items.Insert(0, new ListItem(""));
ddlPurposeName.DataSource = context.Purposes.ToList();
ddlPurposeName.DataTextField = "PurposeName";
ddlPurposeName.DataValueField = "ID";
ddlPurposeName.DataBind();
ddlTypes.Items.Insert(0, new ListItem(""));
ddlTypes.DataSource = context.Types.ToList();
ddlTypes.DataTextField = "TypeName";
ddlTypes.DataValueField = "ID";
ddlTypes.DataBind();
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
ddlDistrictName.SelectedValue = ((Project)(e.Row.DataItem)).DistrictID.ToString();
ddlPurposeName.SelectedValue = ((Project)(e.Row.DataItem)).PurposeID.ToString(); ;
ddlTypes.SelectedValue = ((Project)(e.Row.DataItem)).TypeID.ToString();
}
}
}
}
void BindGrid()
{
using (AlamaarRealEstateEntities context = new AlamaarRealEstateEntities())
{
if (context.Projects.Count() > 0)
{
GVAllProjects.DataSource = context.AllProjects().ToList();
GVAllProjects.DataBind();
}
else
{
var obj = new List<Project>();
obj.Add(new Project());
// Bind the DataTable which contain a blank row to the GridView
GVAllProjects.DataSource = obj;
GVAllProjects.DataBind();
int columnsCount = GVAllProjects.Columns.Count;
GVAllProjects.Rows[0].Cells.Clear();// clear all the cells in the row
GVAllProjects.Rows[0].Cells.Add(new TableCell()); //add a new blank cell
GVAllProjects.Rows[0].Cells[0].ColumnSpan = columnsCount; //set the column span to the new added cell
//You can set the styles here
GVAllProjects.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;
GVAllProjects.Rows[0].Cells[0].ForeColor = System.Drawing.Color.Red;
GVAllProjects.Rows[0].Cells[0].Font.Bold = true;
//set No Results found to the new added cell
GVAllProjects.Rows[0].Cells[0].Text = "NO RESULT FOUND!";
}
}
}
这行中的错误:
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
ddlDistrictName.SelectedValue = ((Project)(e.Row.DataItem)).DistrictID.ToString();
ddlPurposeName.SelectedValue = ((Project)(e.Row.DataItem)).PurposeID.ToString(); ;
ddlTypes.SelectedValue = ((Project)(e.Row.DataItem)).TypeID.ToString();
}
}
感谢任何帮助。