Setting selectedvalue for a dropdownlist in GridView - asp.net

I have a dropdownlist in my Gridview and I am binding a datasource to the gridview.
Though all the records are displaying properly the dropdown value is not selected.
How do I set something like
<%# Bind("Country") %> for a dropdownlist in the Gridview in ASP.net.
Thanks

You can hook into the RowDataBound event for the grid view, find the control and set the value.
protected void gridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
var dropdownList = e.Row.FindControl("YOUR_DROP_DOWN") as DropDownList;
dropdownList .SelectedIndex = SET_VALUE_HERE;
}

Setting DropDownList value from a Datasource should be like:
protected void gridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlCountry = (DropDownList)e.Row.FindControl("ddlCountry");
ddlCountry.SelectedValue = DataBinder.Eval(e.Row.DataItem, "Country").ToString();
}
}

Related

get row value on update Button

get the Product SKU Of the row which the button pressed
protected void On_Update_Click(object sender, EventArgs e)
{
}
The easiest way is to use the DataKeyNames property.
<asp:GridView ID="GridView1" runat="server" DataKeyNames="SKU">
And then you can access the correct DataKeyNames based on the Row Index.
protected void On_Update_Click(object sender, EventArgs e)
{
//cast the sender back to a control and get the gridviewrow from the namingconainer
GridViewRow row = (GridViewRow)(((Control)sender).NamingContainer);
//get the gridview itself from the gridviewrow
GridView gv = row.Parent.Parent as GridView;
//get the correct datakey from the gridview with the dataitemindex
int SKU = Convert.ToInt32(gv.DataKeys[row.DataItemIndex].Values[0]);
//display result
Label1.Text = SKU.ToString();
}

Referencing webcontrols in an UpdatePanel in EditItemTemplate of a Gridview

I am getting null reference exception when trying to reference a web control which is within an AJAX UpdatePanel, which itself is within and EditItemTemplate of a Gridview. I am doing this in the RowDataBound event handler of the gridview as follows:
protected void gvIntakes_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView row = (DataRowView)e.Row.DataItem;
UpdatePanel up = (UpdatePanel)e.Row.FindControl("Status_UpdatePanel"); //**** up is showing null on debug
Panel pnlSelectStaff = (Panel) up.FindControl("pnlSelectStaff") ;
Panel pnlSchedule = (Panel)up.FindControl("pnlSchedule");

How can I show datas on gridview when selecting a dropdownlist in ASP.NET?

I have a dropdownlist and a gridview. When Dropdownlist is selected , gridview should show the data.
i think that's gonna work for you
http://www.facebook.com/groups/314912835194902/doc/406584239361094/
protected void DropDownListChanged(object sender, EventArgs e)
{
gridView.DataSource = GetDataSource();
gridView.DataBind();
}
you need to write an event SelectedIndexChanged="ddlChanged" and write your logic under it.
protected void ddlChanged(object sender, EventArgs e)
{
gridView.DataSource = GetData();
gridView.DataBind();
}
Make sure that you have AutoPostBack="true" on DropDownList. Otherwise it will not fire the event.

How to find out rowindex when fire dropdown event inside gridview

I am using two dropdownlist in gridview. Total 5 to 7 rows in gridview.
Bind ProductId and product name in First Dropdown. when select product that time databound in second dropdown.
so how to findout row number when fire dropdown's event.
I assume the property you're looking for is GridViewRow.RowIndex. To get the reference to the row from the SelectedIndexChanged event from your DropDownLis(s), you can use it's NamingContainer property:
private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddl.NamingContainer;
int rowIndex = row.RowIndex;
// if you want to get the reference to the other DropDown in this row
DropDownList ddl2 = (DropDownList)row.FindControl("DropDownList2");
}
You can handle SelectedIndexChanged event of DropDownList.
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow gvr = (GridViewRow)((DropDownList)sender).Parent.Parent;
string rowNumber= GridView1.DataKeys[gvr.RowIndex].Value.ToString();
}

How to get the value of selected row using radiobutton in gridview in a button click

i need to get the value of the selected row of radiobutton in the gridview. the selected value is assigned to a textbox. my code is
protected void lnBTNDone_Click(object sender, EventArgs e)
{
GridViewRow gvRow = grdEventDetails.SelectedRow;
txtEventId.Text = gvRow.Cells[0].Text;
}
the problem is the gvRow value is assigned as null .
the linkbutton is not inside gridview. the gridview n linkbutton and the textbox are inside the user controls.
You can always get the GridViewRow via the sender's NamingContainer:
protected void lnBTNDone_Click(object sender, EventArgs e)
{
LinkButton lnBTNDone = (LinkButton)sender;
GridViewRow row = (GridViewRow)lnBTNDone.NamingContainer;
txtEventId.Text = row.Cells[0].Text;
}
Assuming that the LinkButton is in the selected row.
Btw, i'm yet not sure why SelectedRow is null there, maybe because the GridView's SelectedIndexChanged event is triggered after the LinkButton's click event.
protected void lnBTNDone_Click(object sender, EventArgs e)
{
for (int i = 0; i < grdEventDetails.Rows.Count; i++)
{
RadioButton rb = (grdEventDetails.Rows[i].FindControl("grdRdo")) as RadioButton;
if (rb.Checked == true)
{
txtEventId.Text = grdEventDetails.Rows[i].Cells[1].Text;
}
}
}

Resources