c# dropdownlist selectedindexchanged in gridview sets selectedindex of second dropdownlist - c#

I have a gridview with two dropdownlists populated from a db. One is a descriptive name, the other is an abbreviated name. I need to accomplish the following:
When I select an item in DDL1 I need to change the selected index of DDL2 to match, and vice versa.
I have searched on here and found the following:
protected void ddlAddLabTest_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlLabTest = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddlLabTest.NamingContainer;
DropDownList ddlAddLabTestShortName = (DropDownList)row.FindControl("ddlAddShortname");
ddlAddLabTestShortName.SelectedIndex = intSelectedIndex;
}
Only when it gets to the assignment for "row" I receive the following:
Unable to cast object of type 'System.Web.UI.WebControls.DataGridItem' to type 'System.Web.UI.WebControls.GridViewRow'.
I have tried finding a working example but I can't. Any help is greatly appreciated!

Try this
protected void ddlAddLabTest_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlLabTest = (DropDownList)sender;
DataGridItem row = (DataGridItem) ddlLabTest.NamingContainer;
DropDownList ddlAddLabTestShortName = (DropDownList)row.FindControl("ddlAddShortname");
ddlAddLabTestShortName.SelectedIndex = intSelectedIndex;
}

Seems like the NamingContainer is not a row, so leave it like that. It already has the FindControl method.
var row = ddlLabTest.NamingContainer;

Related

How to get Row Id in RepositoryLookupEdit_ValueChanged event

I have gridcontrol that has a RepositoryLookupEdit in one of the columns. I can get the value of RepositoryLookupEdit after changed, but I dont know how to get the which row's RepositoryLookupEdit value changed. How can I get the Row ID?
With the code below, I can get the RepositoryLookupEdit value.
private void repositoryItemLookUpEdit1_EditValueChanged(object sender, EventArgs e)
{
LookUpEdit edit = sender as LookUpEdit;
var row = edit.Properties.GetDataSourceRowByKeyValue(edit.EditValue);
}
Since repositoryItemLookUpEdit isn't restricted to GridControls you cannot get the row handle from this event. You however have other possibilities.
First, if the edit is done by the user, you can use the ColumnView.GetFocusedRow() method to get the current grid row.
If however the edit value is changed via code it will also be changed in the grid so you can now use the ColumnView.CellValueChanged event.
private void repositoryItemLookUpEdit1_EditValueChanged(object sender, EventArgs e)
{
LookUpEdit edit = sender as LookUpEdit;
var row = edit.Properties.GetDataSourceRowByKeyValue(edit.EditValue);
gridRow = gridView.GetFocusedRow() as MyDataRow
}

Unable to cast object of type 'System.Web.UI.HtmlControls.HtmlForm' to type 'System.Web.UI.WebControls.GridViewRow'

Here is my ASP.NET code snippet.
I am trying to select a GridView Row and add the selected row items in Session Variable.
// ======================== MyGridView ========================
protected void GridView_MyGridView_SelectedIndexChanged(object sender, EventArgs e)
{
// Get Selected Row Items
Items myitems = new Items();
Session["Items"] = myitems;
((Invoices)Session["Items"]).ItemNo = int.Parse(((GridViewRow)(((WebControl)(sender)).Parent.Parent)).Cells[0].Text);
}
I do NOT want to use the clickable select button that comes with the GridView Columns.
I handled by the following code:
protected void GridView_MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
e.Row.ToolTip = "Click to Select a Visit.";
e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(sender as GridView, "SELECT$" + e.Row.RowIndex);
}
}
Now, when I run the program, I get the following error as soon as the gridview selected row changes:
Unable to cast object of type 'System.Web.UI.HtmlControls.HtmlForm' to type 'System.Web.UI.WebControls.GridViewRow'.
Please, provide your feedback.
Not sure what you are trying to do but your cast is what is causing it. Your call to Parent.Parent is going too far up the tree and you have ended up with the form container and are no longer within the GridView at all.
Instead of the cast you are attempting, why not just try:
GridViewRow row = GridView_MyGridView.SelectedRow;
((Invoices)Session["Items"]).ItemNo = int.Parse(row.Cells[0].Text);
Keep in mind, this will still error out if there is no text in the cell so you may want to look at handling that with additional logic or a TryParse() instead.

Accessing gridview from a non GridView event

I have a button outside a gridview called "UpdateAll", i need to be able to click on such button and find an item template "DropDownList" and update the database with that value for each record, I am not sure how to go about this any ideas?
public void UpdateAll_Click(object sender, EventArgs e)
{
}
Now I know I can access the drop down in the GridView_RowCommand something like this
GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
Myddl = row.FindControl("Quantity") as DropDownList;
But i am not sure how to do that from an outside event that is not GridView related, I can not do the one above because it accesses the e.CommandSource.
Please help.
You can do something like this;
for(int rowIndex =0; rowIndex<gv.rows.count; rowIndex++)
{
Myddl = gv.rows[rowIndex].FindControl("Quantity") as DropDownList;
}

selected index of DDL is always equal to Zero in asp .net

I have a Program Class, with properties as Id, ProgramName,ShortName and Code, im my app
I have a ASP DDL like
<asp:DropDownList ID="DDLProgram" runat="server"
OnSelectedIndexChanged ="OnDDLProgramChanged" AutoPostBack = "true">
</asp:DropDownList>
My OnDDLProgramChanged method is defined as
protected void OnDDLProgramChanged(object sender, EventArgs e)
{
List<CcProgramEntity> programEntities = GetAllPrograms();
DDLProgram.DataSource = programEntities;
DDLProgram.DataTextField = "Shortname";
DDLProgram.DataValueField = "Id";
//My Problem goes here
string programCode = programEntities[DDLProgram.SelectedIndex].Code;
}
My list is getting all the records correctly, I have checked it. But whenever I am changing an item in the DDL, the selected index in not changing. The selected index is remaining zero.Therefore, I am not being able to get the code of other items but the 0 index's item.
Can anyone help me in this case?
You are binding the data again in your selectedIndex Change event and it will reset your current SelectedIndex after rebinding. You don't need to rebind the data to your dropdown in SelectedIndex Change Event
It should be like..
protected void OnDDLProgramChanged(object sender, EventArgs e)
{
string programCode = programEntities[DDLProgram.SelectedIndex].Code;
}
You have to bind data to the DropDownList on page load method
if (!IsPostBack)
{
DDLProgram.DataSource = programEntities;
DDLProgram.DataTextField = "Shortname";
DDLProgram.DataValueField = "Id";
DDLProgram.DataBind();
}
Otherwise it will bind data everytime and hence clear the selection
Why are you assigning the DataSource in OnDDLProgramChanged, this would be resetting the selection you make.

DropDownList Selected Item Incorrect When Using FindControl

I have a number of dropdownlists embedded in a gridview. When I submit the page I loop through all the rows of the gridview and use the findcontrol method to get the dropdownlist e.g:
foreach (GridViewRow gvrItem in gvItems.Rows)
{
DropDownList ddlOption = gvrItem.Cells[2].FindControl("ddlOption") as DropDownList;
}
This works nicely, however when I try to get the selected item of the dropdownlist e.g:
ddlOption .SelectedItem.Text
It always returns the first item in the list rather than whats actually selecte din the page. Any ideas what I'm doing wrong?
You need to do this after the GridView has been databound. Try calling it in the DataBound event:
protected void GridView1_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow gvrItem in gvItems.Rows)
{
DropDownList ddlOption = gvrItem.Cells[2].FindControl("ddlOption") as DropDownList;
}
string selectedItem = ddlOption.SelectedItem.Text;
}
It turned out to be a quirk of .Net, if you fill the drop down list with ListItem's it won't carry those items into the ViewState. If you fill the drop down list with strings it will. Very odd I know.
e.g.:
DropDownList ddl = new DropDownList();
ddl.Add(new ListItem("text", "value")); <----Fails :(
ddl.Add("text"); <---- Works :)

Categories

Resources