Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The item template column control is dropdown in gridview. dataset values will binded into that dropdown control
i have written code into the page load Event
DataSet ds = new DataSet();
ds = GetDataValue();//This line get values from the database
GrdEmploye.DataSource = ds;
GrdEmploye.DataBind();
int i = 0;
DropDownList Drp;
foreach (GridViewRow RowItem in GrdEmploye.Rows)
{ 
    Drp = (DropDownList)(RowItem.Cells[0].FindControl    ("DrpEmployee"));//This line is denote the item template column control
    Drp.DataSources= ds;
    Drp.DataTextField  = ds.Tables[0].Rows[i]["Empsap"].ToString();
    Drp.DataValueField = ds.Tables[0].Rows[i]["Empsap"].ToString();
    Drp.DataBind();//This line got error
}
Posted
Updated 16-Dec-10 1:28am
v4
Comments
Ankur\m/ 16-Dec-10 6:15am    
What error?
Abdul Quader Mamun 16-Dec-10 7:28am    
Spelling check.

Many suggestion to avoiding error.


Drp.DataSources= ds;


Rather then binding whole dataset to Drp. just bind the specific table inside.

Like ds.Tables["sometablename"];


Drp.DataTextField = ds.Tables[0].Rows[i]["Empsap"].ToString();
Drp.DataValueField = ds.Tables[0].Rows[i]["Empsap"].ToString();



Here you just need to assign specific table column name rather then dynamic row value data.<clear>



For Example are if you're sure that table have column, suppose "Fees" then bind it with.
Drp.DataTextField = "Fees";

Same holds true for DataValueField.

And If you aren't sure with the column name, then just fetch column name from table Like
Drp.DataTextField = dt.Tables[0].Columns[2].ColumnName

Please vote and Accept Answer if it Helped.
 
Share this answer
 
v5
Comments
Hiren solanki 16-Dec-10 8:28am    
Hey Tonyo, I know how better add a tag to code, Don't spoil my code with pre.

I kept it as bold because I wanted to point that out.
I am not a newbie with CP.
Try This

DropDownList ddl;
int i = 0;
foreach (GridViewRow grdrow in grdmenu.Rows)
{
ddl = (DropDownList)grdmenu.Rows[grdrow.RowIndex].FindControl("ddloffer");
ddl.SelectedIndex = Convert.ToInt16(ds.Tables[0].Rows[i]["Offer"]);
ddl = (DropDownList)grdmenu.Rows[grdrow.RowIndex].FindControl("ddloffers");
ddl.SelectedIndex = Convert.ToInt16(ds.Tables[0].Rows[i]["OfferType"]);
i++;
ddl.DataBind();
}
 
Share this answer
 
v2
Comments
lapislaxuli 28-Jul-15 14:28pm    
DropDownList ddl;
foreach (GridViewRow gvRow in gvProf.Rows)
{
ddl = (DropDownList)(gvRow.Cells[5].FindControl("ddlDcode"));
ddl.DataSource = ds;
ddl.DataTextField = ds.Tables[0].Columns["dis_name"].ToString();
ddl.DataValueField = ds.Tables[0].Columns["dis_code"].ToString();
ddl.DataBind();
}
It is working fine
Richard Deeming 28-Jul-15 14:35pm    
Do you really think the OP is still waiting for your answer after nearly five years?!
DropDownList ddl;
foreach (GridViewRow gvRow in gvProf.Rows)
{
ddl = (DropDownList)(gvRow.Cells[5].FindControl("ddlDcode"));
ddl.DataSource = ds;
ddl.DataTextField = ds.Tables[0].Columns["dis_name"].ToString();
ddl.DataValueField = ds.Tables[0].Columns["dis_code"].ToString();
ddl.DataBind();
}
 
Share this answer
 
Comments
Richard Deeming 28-Jul-15 14:35pm    
Do you really think the OP is still waiting for your answer after nearly five years?!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900