这是我的全网格
<div id="grid">
<asp:GridView ID="gridProduct" runat="server"
AutoGenerateColumns="false"
ShowFooter="true"
onrowcancelingedit="gridProduct_RowCancelingEdit"
onrowdeleting="gridProduct_RowDeleting" onrowediting="gridProduct_RowEditing"
onrowupdating="gridProduct_RowUpdating"
onrowcommand="gridProduct_RowCommand"
onrowdatabound="gridProduct_RowDataBound">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:Button ID="buttonUpdate" CommandName="Update" runat="server" ToolTip="Update" Text="Update" />
<asp:Button ID="buttonCancel" CommandName="Cancel" runat="server" ToolTip="Cancel" Text="Cancel" />
</EditItemTemplate>
<ItemTemplate>
<asp:Button ID="buttonEdit" CommandName="Edit" runat="server" Text="Edit" ToolTip="Edit"/>
<asp:Button ID="buttonDelete" CommandName="Delete" runat="server" Text="Delete" ToolTip="Delete"/>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="buttonAdd" runat="server" Text="Ajouter" CommandName="AddNew" ToolTip="Add new User" ValidationGroup="validaiton" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="#Piece">
<EditItemTemplate>
<asp:DropDownList ID="dropDownListPartsEdit" DataTextField="Nom" DataValueField="ID_AchatTemplate" runat="server" Text='<%#Eval("Piece") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="labelItemPiece" runat="server" Text='<%#Eval("Piece") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="dropDownListPartsFooter" runat="server" DataTextField="Nom" DataValueField="ID_AchatTemplate">
</asp:DropDownList>
<%--<asp:TextBox ID="txtBoxPiece" runat="server"/>--%>
<%--<asp:RequiredFieldValidator ID="fieldValidPiece" runat="server" ControlToValidate="txtBoxPiece" Text="*" ValidationGroup="validaiton"/>--%>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Series">
<EditItemTemplate>
<asp:TextBox ID="txtBoxEditSeries" runat="server" Text='<%#Eval("Series") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="labelItemSeries" runat="server" Text='<%#Eval("Series") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtBoxSeries" runat="server"/>
<asp:RequiredFieldValidator ID="fieldValidSeries" runat="server" ControlToValidate="txtBoxSeries" Text="*" ValidationGroup="validaiton"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Revision">
<EditItemTemplate>
<asp:TextBox ID="textBoxEditRevision" runat="server" Text='<%#Eval("Rev") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="labelItemRevision" runat="server" Text='<%#Eval("Rev") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtBoxRevision" runat="server"/>
<asp:RequiredFieldValidator ID="fieldValidRevision" runat="server" ControlToValidate="txtBoxRevision" Text="*" ValidationGroup="validaiton"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Firmware">
<EditItemTemplate>
<asp:TextBox ID="textBoxEditFirmware" runat="server" Text='<%#Eval("Firmware") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="labelItemFirmware" runat="server" Text='<%#Eval("Firmware") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtBoxFirmware" runat="server"/>
<asp:RequiredFieldValidator ID="fieldValidFirmware" runat="server" ControlToValidate="txtBoxFirmware" Text="*" ValidationGroup="validaiton"/>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
这是我编辑的方法事件。首先,我尝试绑定我的dropDownList但是我收到错误“对象引用没有设置为对象的实例。”
protected void gridProduct_RowEditing(object sender, GridViewEditEventArgs e)
{
ClientParts getclient = new ClientParts();
ClientPartsSearchCriteria criteria = new ClientPartsSearchCriteria();
((DropDownList)gridProduct.Rows[e.NewEditIndex].FindControl("dropDownListPartsEdit")).DataSource = getclient.Get(criteria);
((DropDownList)gridProduct.Rows[e.NewEditIndex].FindControl("dropDownListPartsEdit")).DataBind();
gridProduct.EditIndex = e.NewEditIndex;
PsaDataSet psaList = (PsaDataSet)ViewState["psaList"];
BindGrid(psaList, false);
}
我不明白为什么“对象引用未设置为对象的实例”。从这一行
((DropDownList)gridProduct.Rows[e.NewEditIndex].FindControl("dropDownListPartsEdit")).DataSource = getclient.Get(criteria);
答案 0 :(得分:0)
((DropDownList)gridProduct.Rows[e.NewEditIndex].FindControl("dropDownListPartsEdit")).DataSource = getclient.Get(criteria)
这是一段相当不安全的代码。首先,假设您的ClientParts.Get()
方法返回正确的数据,其次,您假设FindControl()
正在找到您的DropDownList。为了隔离哪个部分失败,将该行拆分为几行代码,并添加空检查。
我的c#生锈了,但它应该是这样的(假设ClientParts.Get()
返回DataTable
):
(DropDownList)ddlParts = ((DropDownList)gridProduct.Rows[e.NewEditIndex].FindControl("dropDownListPartsEdit"));
if (ddlParts != null)
{
(DataTable)dtParts = getclient.Get(criteria);
if (dtParts != null)
{
ddlParts.DataSource = dtParts;
ddlParts.DataBind();
}
}
编辑:问题是在此事件期间,您无法使用GridView.Rows [index] .FindControl()引用EditItemTemplate控件。使用它将访问ItemTemplate中的控件。我不确定解决方法是什么,因为我从未试图以这种方式在模板中数据绑定控件。也许你应该考虑使用ObjectDataSource?
答案 1 :(得分:0)
我必须将此下拉列入此onrowdatabound =“gridProduct_RowDataBound”&gt; !!