Answered by:
Gridview bound column and edit item template issue

Question
-
User-148788041 posted
hi,
I had bind the gridview with datasource which has
sql="select teamowner,issue etc from table1"
But the dropdownlist are in edit item template I am populating it in row databound from another table .
sql = "select text,value from tabl2"
How to show selected index for dropdown in edit item template.
<asp:GridView ID="GridView1" DataKeyNames = "deliveryId" runat="server" AutoGenerateColumns="False" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDataBound = "RowDataBound" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Width="37px" Height="223px" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"> <Columns> <asp:TemplateField HeaderText = "deliveryStatus"> <ItemTemplate> <asp:Label ID="lblStatus" runat="server" Text='<%# Eval("TeamOwner")%>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:DropDownList ID = "ddlStatus" runat = "server" > </asp:DropDownList> </EditItemTemplate>
I had populated the ddlStatus dropdown in Row databound.
In Row command event how to show the selected index of dropdown.
Friday, July 8, 2016 5:33 AM
Answers
-
User-1034726716 posted
But the dropdownlist are in edit item template I am populating it in row databound from another table .This should help you with that: ASP.NET GridView: Implementing Cascading DropDownList on Edit Mode
You should be able to see the code there for accessing a DropDownList within EditItemTemplate. Here's the "meat" that you should keep an eye to:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if ((e.Row.RowState & DataControlRowState.Edit) > 0) { DropDownList ddl = (DropDownList)e.Row.FindControl("YourDropDownListID"); ddl.DataSource = ?? //Set the DataSource here ddl.DataValueField = "FieldName"; ddl.DataTextField = "FieldName"; ddl.DataBind(); } } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, July 8, 2016 3:46 PM
All replies
-
User-1907363112 posted
Please go through this link http://www.aspsnippets.com/Articles/Populate-DropDownList-with-Selected-Value-in-EditItemTemplate-of-GridView-in-ASPNet.aspx .
It is explained beautifully.
Hope it will resolve your issue.
Don't forget to mark it as answer so that others can also benefit from it.
Friday, July 8, 2016 9:14 AM -
User-1034726716 posted
But the dropdownlist are in edit item template I am populating it in row databound from another table .This should help you with that: ASP.NET GridView: Implementing Cascading DropDownList on Edit Mode
You should be able to see the code there for accessing a DropDownList within EditItemTemplate. Here's the "meat" that you should keep an eye to:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if ((e.Row.RowState & DataControlRowState.Edit) > 0) { DropDownList ddl = (DropDownList)e.Row.FindControl("YourDropDownListID"); ddl.DataSource = ?? //Set the DataSource here ddl.DataValueField = "FieldName"; ddl.DataTextField = "FieldName"; ddl.DataBind(); } } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, July 8, 2016 3:46 PM -
User36583972 posted
Hi guhananth,
From your description, I have made a sample on my side. You can refer the following code.
HTML:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand"> <Columns> <asp:BoundField DataField="ID" HeaderText="ID" /> <asp:BoundField DataField="Product" HeaderText="Product" /> <asp:BoundField DataField="Version" HeaderText="Version" /> <asp:BoundField DataField="Description" HeaderText="Description" /> <asp:TemplateField HeaderText="Dropdown"> <ItemTemplate> <asp:DropDownList ID="ddlName" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlName_SelectedIndexChanged"> <asp:ListItem Text="John"></asp:ListItem> <asp:ListItem Text="Mark"></asp:ListItem> <asp:ListItem Text="Jim"></asp:ListItem> </asp:DropDownList> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> </form> </body> </html>
ASPX.CS:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bind(); } } private void bind() { DataTable tblDatas = new DataTable("Datas"); DataColumn dc = null; dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32")); dc.AutoIncrement = true;// dc.AutoIncrementSeed = 1;// dc.AutoIncrementStep = 1;// dc.AllowDBNull = false; dc = tblDatas.Columns.Add("Product", Type.GetType("System.String")); dc = tblDatas.Columns.Add("Version", Type.GetType("System.String")); dc = tblDatas.Columns.Add("Description", Type.GetType("System.String")); DataRow newRow; for (int i = 0; i < 5; i++) { newRow = tblDatas.NewRow(); newRow["Product"] = "Count" + i.ToString(); newRow["Version"] = "2.0"; newRow["Description"] = "Hello boy"; tblDatas.Rows.Add(newRow); } ViewState["dt"] = tblDatas; GridView1.DataSource = tblDatas; GridView1.DataBind(); } protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName.Equals("CustomDelete")) { GridViewRow oItem = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer; int RowIndex = oItem.RowIndex; Response.Write(oItem.Cells[0].Text + "" + oItem.Cells[1].Text); } } protected void ddlName_SelectedIndexChanged(object sender, EventArgs e) { DropDownList list = (DropDownList)sender; GridViewRow row = (GridViewRow)list.NamingContainer; string index = list.SelectedIndex.ToString(); Response.Write("GridID:"+ row .Cells[0].Text+ ", dropindex:"+index + ", dropvalue:" + list.SelectedValue.ToString()); }
Best Regards,
Yohann Lu
Tuesday, July 12, 2016 9:58 AM