How to Show data in GridView TextBox, and/or DropDownListBoxs in asp.net
My client has given one requirement like below:
He wants to Insert, Update, Delete record inside the GridView only. He wants all the data to be present in TextBoxes and DropDownList controls (i.e. not in the label controls). If he wants to update any record, then he wants to straight-away update the record there and clicks on Update button. He doesn't want to click on the Edit link to get the record in Edit mode.
Ho开发者_StackOverfloww can I achieve this functionality? I should also consider references columns while designing GridView. i.e. some templates will contain DropDownList items also.
Can anybody please suggest me how to do this. Some suggestions or some links to learn are appreciatable.
Please help me on this.
GvYou can add the requied field to the grid view in a template field
<asp:TemplateField HeaderText="fieldname" >
<ItemTemplate>
<asp:TextBox ID="txbType" Text='<%# Eval("field") %>' runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
and then in the backend code
protected void Gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int index = Convert.ToInt32(e.RowIndex.ToString());
GridViewRow row = GV.Rows[index];
var txbName = row.FindControl("txbType") as TextBox;
string name = "";
if (txbName != null)
{
name = txbName.Text.Trim();
}
if (true)
// your update function
}
same for the deleting
protected void Gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
Checkout this link: http://geekswithblogs.net/dotNETvinz/archive/2009/08/09/adding-dynamic-rows-in-asp.net-gridview-control-with-textboxes-and.aspx. In this example author used only TextBoxes but DropDownList can be used in similar fashion, from the link you will at least get the idea about using control inside GridView cell and then retrieving the data they contains.
精彩评论