Add rows in grid view dynamically
I have a grid view which is used to show the Product details. Another thing is there is a table viz. ProductMaster. The data in the gridview is coming from this table. Now what I want is, at first time when page is loaded, only first column should be filled,which contains dropdown list 开发者_JAVA百科containing product names. All other rows should be empty. Now when user select the Item from the dropdown list, at that time other columns should get filled repectively and other empty row should appear below filled row. Whenever the page is loaded there will be just one empty row(First column dropdown list will be filled with products)
How can I achieve this task??????
Check this article out: http://geekswithblogs.net/dotNETvinz/archive/2009/06/04/adding-dynamic-rows-in-gridview-with-textboxes.aspx
Add DropDownList
inside TemplateColumn
and in OnRowDataBound
event fill it with the product names and instead of Click
event of Button
as explained in mentioned article you have to handle SelectedIndexChanged
event of DropDownList
, get the reference on row which contains this DropDownList
and fill other columns of this row.
aspx:
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="cmbProduct" runat="server" OnSelectedIndexChanged="cmbProduct_Changed" AutoPostBack="true" >
</asp:DropDownList>
</ItemTemplate>
...
</Columns>
code behind:
protected void cmbProduct_Changed(object sender, EventArgs e)
{
DropDownList cmbProduct = (DropDownList)sender;
GridViewRow parentRow = (GridViewRow)cmbProduct.NamingContainer;
string selectedProdId = cmbProduct.SelectedValue;
/* Fetch product details & bind other columns/controls with product details */
Label lbl = (Label)parentRow.FindControl("lblProductName");
lbl.Text = "....";
/* Call AddNewRowToGrid explaied in article*/
AddNewRowToGrid();
}
精彩评论