Gridview中的错误行数据绑定事件

编程入门 行业动态 更新时间:2024-10-18 03:26:21
本文介绍了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中的错误行数据绑定事件

本文发布于:2023-11-27 21:57:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639747.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   行数   错误   事件   Gridview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!