ADDing Dropdownlist in gridview [Resolved]

Posted by Harsh_rc under ASP.NET on 4/14/2010 | Views : 3096 | Status : [Member] | Replies : 7
i want to add dropdownlist in gridview..i am able to do that bt prob. is that i want to access CustomerId also which is on same row
when i change option in dropdownlist i want to access customerid also which is on same row




Responses

Posted by: Sabarees on: 4/15/2010 [Member] Starter

Up
0
Down

Resolved
Hi Harsh,

Use hiddenfield in your gridview & bind the value of customerid into hiddenfield, then you have to get the hidden field's value for the corresponding rowid that also get in the previous posts.

in your selected index changed event for dropdown enter below code to access the customerid on that row,

protected void ddlRoles_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl_Roles = (DropDownList)sender;
GridViewRow row = (GridViewRow)((Control)sender).Parent.Parent;

// Change the cell no to your current position of a hiddenfield
int customerId = ((HiddenField)row.Cells[2].FindControl("hdnCustomerId")).Value

}

Best Ragards,
Sabarees M

Harsh_rc, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Harsh_rc on: 4/15/2010 [Member] Starter

Up
0
Down

Resolved
hey.....i modified ur previous code.....n now it is working..thnks a lot

Harsh_rc, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pavanandey on: 4/14/2010 [Member] Bronze

Up
0
Down


Hi

<asp:TemplateField HeaderText="Role">
<ItemTemplate>
<asp:DropDownList ID="ddlRoles" runat="server" OnSelectedIndexChanged="ddlRoles_SelectedIndexChanged" ></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>

In row databound event of gridview


protected void Gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList DDl = new DropDownList();
DDl = (DropDownList)e.Row.FindControl("ddlRoles");
/// Fill Drop Down Here
}
}

protected void ddlRoles_SelectedIndexChanged(object sender, EventArgs e)
{
// Here the the dropdown changed value u can perform ur actions
}




Thanks
Pavan Kumar
Mark Answer if this fits the need

Harsh_rc, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 4/14/2010 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
Bind your grid with the Customerid and hide it. in tell your gridview that the primary key is Customerid by adding this property in the defination of the gridview

DataKeyNames="Customerid "


onselectedindexchanged of the dropdown in the grid and on the server side if you want to get the id of that row, do this

 

//If we are using our gridview1 do the Following
// parse through the grdview rows
foreach (GridViewRow row in grdViewTreeRules.Rows)
{
String Customerid = Convert.ToString(grdViewTreeRules.DataKeys[row.RowIndex].Value);

DropDownList ddl = (DropDownList)row.Cells[2].FindControl("drplstparent"); //the Index there is where the dropdown is, this is when you want to get the value of a dropdown

String Dropdownvalue = ddl.SelectedValue;
}


Thank you for posting at Dotnetfunda

Vuyiswa Maseko

Thank you for posting at Dotnetfunda
[Administrator]

Harsh_rc, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Harsh_rc on: 4/15/2010 [Member] Starter

Up
0
Down
sorry to say bt ur code is always giving me id of last row
in gridview......so please do anything

Harsh_rc, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 4/15/2010 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
My Example or Pavanandey example ?


Thank you for posting at Dotnetfunda
[Administrator]

Harsh_rc, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Harsh_rc on: 4/15/2010 [Member] Starter

Up
0
Down
ur Example

Harsh_rc, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response