编辑使用编辑模板,在GridView中只有特定的领域 [英] edit only specific fields in gridview using edit template
问题描述
我有一个错误:对象引用不设置到对象的实例。而红色文字是:
dt.Rows [row.RowIndex] [名称] =名称;
我想在我的GridView编辑数据。这里是我的code:
保护无效的OnUpdate(对象发件人,EventArgs的发送)
{
GridViewRow行=(发件人为LinkButton的).NamingContainer为GridViewRow;
字符串名称=(row.Cells [0] .Controls [0]为文本框)。文本;
串价格=(row.Cells [2] .Controls [0]为文本框)。文本;
DataTable的DT =的ViewState [DT]作为数据表;
dt.Rows [row.RowIndex] [名称] =名称;
dt.Rows [row.RowIndex] [价格] =价格;
的ViewState [DT] = DT;
gdview.EditIndex = -1;
this.GetProducts(0);
}保护无效OnRowEditing(对象发件人,GridViewEditEventArgs E)
{
gdview.EditIndex = e.NewEditIndex;
this.GetProducts(0);
}
这里是的GetProducts()
私人无效的GetProducts(INT类别ID)
{
我的购物K =新的购物车()
{
类别id =类别ID
};
gdview.DataSource = NULL;
gdview.DataSource = k.GetAllProducts();
gdview.DataBind();
}
我是什么在这里失踪?
另外一个问题。当我更新点击链接,它显示的名称和价格字段编辑文本框。但在名字的价值,不是吗?这里是一个屏幕截图。
这里是我的html code:
<柱体和GT;
< ASP:BoundField的的HeaderText =名称数据字段=名称SORTEX pression =名称>
< ItemStyle HEIGHT =20像素WIDTH =150像素/>
< / ASP:BoundField的> < ASP:BoundField的的HeaderText =产品分类只读=真的DataField =类别名称SORTEX pression =CategoryNaame>
< ItemStyle HEIGHT =20像素WIDTH =150像素/>
< / ASP:BoundField的> < ASP:BoundField的的HeaderText =价格数据字段=价格SORTEX pression =价格>
< ItemStyle HEIGHT =20像素WIDTH =150像素/>
< / ASP:BoundField的> < ASP:ImageField的的HeaderText =的ImageUrlDataImageUrlField =的ImageUrlSORTEX pression =的ImageUrl只读=真ControlStyle-WIDTH =10> < ControlStyle WIDTH =50像素>< / ControlStyle> < / ASP:ImageField的> < ASP:BoundField的的HeaderText =产品数量的DataField =产品数量只读=真SORTEX pression =产品数量>
< ItemStyle HEIGHT =20像素WIDTH =150像素/>
< / ASP:BoundField的> < ASP:BoundField的的HeaderText =ProductSold数据字段=ProductSoldSORTEX pression =ProductSold只读=真>
< ItemStyle HEIGHT =20像素WIDTH =150像素/>
< / ASP:BoundField的> < ASP:BoundField的的HeaderText =AvailableStock数据字段=AvailableStockSORTEX pression =AvailableStock只读=真>
< ItemStyle HEIGHT =20像素WIDTH =150像素/>
< / ASP:BoundField的> < ASP:的TemplateField>
<&ItemTemplate中GT;
< ASP:LinkButton的ID =LinkButton1文本=编辑=服务器的CommandName =编辑/>
< / ItemTemplate中>
<&EditItemTemplate的GT;
< ASP:LinkButton的ID =LinkButton2文本=更新=服务器的OnClick =的OnUpdate/>
< ASP:LinkButton的ID =LinkButton3文本=取消=服务器的OnClick =OnCancel/>
< / EditItemTemplate中>
< / ASP:的TemplateField>
首先,你必须将数据表分配给UR视图状态。那么你就可以能够更新字段的值。
仅供参考,我已经在GridView SqlDataSource的通过对象绑定。
请检查code本
如果(!Page.IsPostBack)
{
尝试
{
gdview.DataSource = SqlDataSource1;
gdview.DataBind();
DataView的DV =(数据视图)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
DataTable的DT =新的DataTable();
DT = dv.ToTable();
的ViewState [DT] = DT;
}
赶上(异常前)
{
}
}
-----------------您的另一种方法。
私人无效的GetProducts(INT类别ID)
{
我的购物K =新的购物车()
{
类别id =类别ID
};
gdview.DataSource = NULL;
gdview.DataSource =的ViewState [DT];
gdview.DataBind();
}
i am having an error : Object reference not set to an instance of an object. and the red text is:
dt.Rows[row.RowIndex]["Name"] = Name;
i want to edit data in my gridview. here is my code:
protected void OnUpdate(object sender, EventArgs e)
{
GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;
string Name = (row.Cells[0].Controls[0] as TextBox).Text;
string Price = (row.Cells[2].Controls[0] as TextBox).Text;
DataTable dt = ViewState["dt"] as DataTable;
dt.Rows[row.RowIndex]["Name"] = Name;
dt.Rows[row.RowIndex]["Price"] = Price;
ViewState["dt"] = dt;
gdview.EditIndex = -1;
this.GetProducts(0);
}
protected void OnRowEditing(object sender, GridViewEditEventArgs e)
{
gdview.EditIndex = e.NewEditIndex;
this.GetProducts(0);
}
here is the getproducts()
private void GetProducts(int CategoryID)
{
ShoppingCart k = new ShoppingCart()
{
CategoryID = CategoryID
};
gdview.DataSource = null;
gdview.DataSource = k.GetAllProducts();
gdview.DataBind();
}
what am i missing here?
Another question. When i click on the update link, it shows the edit textbox on the Name, and Price fields. But the value on the name is not there? here is a screenshot.
here is my html code:
<Columns>
<asp:BoundField HeaderText="Name" DataField="Name" SortExpression="Name">
<ItemStyle Height="20px" Width="150px" />
</asp:BoundField>
<asp:BoundField HeaderText="ProductCategory " ReadOnly="true" DataField="CategoryName" SortExpression="CategoryNaame" >
<ItemStyle Height="20px" Width="150px" />
</asp:BoundField>
<asp:BoundField HeaderText="Price" DataField="Price" SortExpression="Price" >
<ItemStyle Height="20px" Width="150px" />
</asp:BoundField>
<asp:ImageField HeaderText ="ImageUrl" DataImageUrlField="ImageUrl" SortExpression="ImageUrl" ReadOnly="true" ControlStyle-Width ="10">
<ControlStyle Width="50px"></ControlStyle>
</asp:ImageField>
<asp:BoundField HeaderText="ProductQuantity" DataField="ProductQuantity" ReadOnly="true" SortExpression="ProductQuantity" >
<ItemStyle Height="20px" Width="150px" />
</asp:BoundField>
<asp:BoundField HeaderText="ProductSold" DataField="ProductSold" SortExpression="ProductSold" ReadOnly="true" >
<ItemStyle Height="20px" Width="150px" />
</asp:BoundField>
<asp:BoundField HeaderText="AvailableStock" DataField="AvailableStock" SortExpression="AvailableStock " ReadOnly="true" >
<ItemStyle Height="20px" Width="150px" />
</asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" Text="Edit" runat="server" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton2" Text="Update" runat="server" OnClick="OnUpdate" />
<asp:LinkButton ID="LinkButton3" Text="Cancel" runat="server" OnClick="OnCancel" />
</EditItemTemplate>
</asp:TemplateField>
First you have to assign the datatable to ur viewstate. then you can able to update the value of the field.
FYI, I have bind the gridview by sqldatasource object.
Please check the code with this
if (!Page.IsPostBack)
{
try
{
gdview.DataSource = SqlDataSource1;
gdview.DataBind();
DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
DataTable dt = new DataTable();
dt = dv.ToTable();
ViewState["dt"] = dt;
}
catch(Exception ex)
{
}
}
-----------------your other method
private void GetProducts(int CategoryID)
{
ShoppingCart k = new ShoppingCart()
{
CategoryID = CategoryID
};
gdview.DataSource = null;
gdview.DataSource = ViewState["dt"];
gdview.DataBind();
}
这篇关于编辑使用编辑模板,在GridView中只有特定的领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!