How To Fill gridview textbox by selecting data from gridview Dropdownlist
-
try like this
//DropDownList inside the gridview
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//identifying DropDownList
DropDownList gridDropDownList = (DropDownList)sender;
//selecting grid row
GridViewRow gridRow = (GridViewRow)gridDropDownList.NamingContainer;
//setting Index
int selectedIndex = gridRow.DataItemIndex;
//finding textbox in selected gridview row
TextBox textBox = (TextBox)GridView1.Rows[selectedIndex].FindControl("yourTextBoxID");
string selected = gridDropDownList.SelectedValue;
if (textBox != null)
{
textBox.Text = selected;
}
}My Mind is the Devil's Workshop.
-
try like this
//DropDownList inside the gridview
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//identifying DropDownList
DropDownList gridDropDownList = (DropDownList)sender;
//selecting grid row
GridViewRow gridRow = (GridViewRow)gridDropDownList.NamingContainer;
//setting Index
int selectedIndex = gridRow.DataItemIndex;
//finding textbox in selected gridview row
TextBox textBox = (TextBox)GridView1.Rows[selectedIndex].FindControl("yourTextBoxID");
string selected = gridDropDownList.SelectedValue;
if (textBox != null)
{
textBox.Text = selected;
}
}My Mind is the Devil's Workshop.
-
Hi, I suggest to you a javascript client solution: add this javascript function somewhere in your project
function selectChoice(ddl, textboxID) {
var chosenoption = ddl.options[ddl.selectedIndex];
if (chosenoption.value != 'nothing') {
document.getElementById(textboxID).value = chosenoption.value;
}
}add onrowdatabound event for your grid with this code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlChoice = e.Row.FindControl("ddlChoice") as DropDownList;
TextBox txtSelectedValue = e.Row.FindControl("txtSelectedValue") as TextBox;string jScript = "selectChoice(this, '{0}');"; ddlChoice.Attributes.Add("onchange", string.Format(jScript, txtSelectedValue.ClientID)); }
}
____________________ Web Programmer http://glucolo.wordpress.com
modified on Monday, July 4, 2011 6:10 AM