Gridview中的错误行数据绑定事件 [英] error Row Databound Event in Gridview
本文介绍了Gridview中的错误行数据绑定事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我在gridview中进行编辑时,出现此对象引用未设置为对象实例的错误.
这是我的代码.
When i am editing in gridview i am getting error like this Object reference not set to an instance of an object.
Here is my code.
public List<products> GetProducts()
{
string query = "Select ProductID, ProductName, UnitPrice, UnitsinStock, CategoryID, Discontinued from Products";
con = new SqlConnection(cons);
cmd = new SqlCommand(query, con);
try
{
con.Open();
dr = cmd.ExecuteReader();
List<products> lst = new List<products>();
if (dr.HasRows)
{
while (dr.Read())
{
Products pd = new Products();
pd.Productid = Convert.ToInt32(dr[0]);
pd.ProductName = dr[1].ToString();
pd.UnitPrice = Convert.ToInt32(dr[2]);
pd.UnitsinStock = Convert.ToInt32(dr[3]);
pd.CategoryID = Convert.ToInt32(dr[4]);
pd.Discontinued = Convert.ToBoolean(dr[5]);
lst.Add(pd);
}
}
return lst;
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
编辑产品-
Edit Product -
protected void EditProduct(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex;
GetProducts();
}
gv_rowdatabound
事件.
gv_rowdatabound
event.
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && gv.EditIndex == e.Row.RowIndex)
{
DropDownList ddlist = (DropDownList)e.Row.FindControl("ddlcategoryid");
ddlist.DataSource = cbl.GetCategories();
ddlist.DataTextField = "CategoryID";
ddlist.DataValueField = "CategoryName";
ddlist.DataBind();
ddlist.Items.FindByValue((e.Row.FindControl("lblcategoryid") as Label).Text).Selected = true;
}
}
}
传递rowdatabound事件后,它会出现 this 错误:对象引用未设置为对象的实例.
请帮我谢谢.
[edit]从下面的注释中添加了错误信息(粗体)-Nelek [/edit]
After passing rowdatabound event it is getting this error: Object reference not set to an instance of an object.
Please help me thank you.
[edit] Added error information from comment below (bold) - Nelek [/edit]
推荐答案
在您的代码中验证以下内容:-
-调用GetCategories完成时,lblcategoryid文本应作为ddlist中的值出现
-标签的第二个检查文本大小写(下部或上部).它应该与dropDown值相同.
您可以在使用"FindByValue()"之前进行检查
verify these things in your code :-
- lblcategoryid text should be present as a value in ddlist while you are calling completed of GetCategories
- second one check text case(lower or upper) of label. it should be same as dropDown Value.
you can check before using "FindByValue()"
string str1 = ddlist.SelectedValue; //"product1"
string str2 = e.Row.FindControl("lblcategoryid") as Label).Text // "Product1"
我对我的问题感到满意.我添加了objectdatasource,并保持了dropdownlist selectedvalue
这是我的解决方法.
I soleved my problem. i added objectdatasource and i kept dropdownlist selectedvalue
here is my solution.
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false"
DataKeyNames="CustomerID" OnRowEditing="EditCustomer"
onrowcancelingedit="gv_RowCancelingEdit"
onrowupdating="gv_RowUpdating">
<Columns>
<asp:CommandField ShowEditButton="true" ShowDeleteButton="true" />
<asp:TemplateField HeaderText="CustomerID" SortExpression="CustomerID">
<ItemTemplate>
<asp:Label ID="lblCustomerid" runat="server" Text='<%# Eval("CustomerID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CustomerName" SortExpression="CustomerName">
<ItemTemplate>
<asp:Label ID="lblCustomerName" runat="server" Text='<%# Eval("CustomerName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtcustomername" runat="server" Text='<%# Bind("CustomerName") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City" SortExpression="City">
<ItemTemplate>
<asp:Label ID="lblcity" runat="server" Text='<%# Eval("City") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlcity" runat="server" AutoPostBack="True"
DataSourceID="ObjectDataSource1" DataTextField="City"
DataValueField="City" SelectedValue='<%# Bind("City") %>' ></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="GetAddress" TypeName="BusinessLogicLayer.Customersbll">
</asp:ObjectDataSource>
这篇关于Gridview中的错误行数据绑定事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文