Click here to Skip to main content
15,880,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to bind the data from the other datatable in a dropdown control which is in the gridview.
Posted
Updated 11-Jun-11 3:34am
v2

Grid view has an event called gridview1_rowdatabound. Follow code as :


C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
            // then bind the ddl
        }
    }
 
Share this answer
 
v2
Comments
manuthebos 30-Jun-13 4:50am    
i typed this code but its only work with first items .. (VS 2012 C# )

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList dl = (DropDownList)e.Row.FindControl("DropDownList1");
if (dl != null)
{
SqlDataReader dr = obj.Reader("select * from category");
dl.DataSource = dr;
dl.DataTextField = "name";
dl.DataValueField = "id";
dl.DataBind();
}
}
}
Hi,

You can use either GridView RowDataBound event to bind dropdown or you can use a simple Foreach loop to iterate and bind each dropdown in a
GridView<br />
. If you use RowDataBound Event, On *.aspx page
XML
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
</asp:GridView>

and on *.aspx.cs use this code below
C#
void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
  if(e.Row.RowType == DataControlRowType.DataRow)
  {
    DropDownList  drdList=(DropDownList)e.Row.FindControl("DropDownList1");
  }
}

Or alternatively you can use the code below after DataBinding on GridView1
C#
DropDownList drdList;
foreach (GridViewRow grdRow in GridView1.Rows)
{
    /// Nested DropDownList Control reference is passed to the DrdList object. 
    ///This will allow you access the properties of dropdownlist placed inside the GridView Template column.
    drdList = (DropDownList)( GridView1.Rows[ grdRow.RowIndex ].Cells[1].FindControl( "DropDownList1" ));

    // DataBinding of nested DropDownList Control for each row of GridView.
    drdList.DataSource = myDataSet;
    drdList.DataValueField = "ColumnName";
    drdList.DataTextField = "ColumnName";
    drdList.DataBind();
}

You will find more details here[^]
Hope this will help.
 
Share this answer
 
i think you should use a Data Set for that.
on the Data set, add a datatable. when you finish adding.
add another Query then from that. Write this

WHERE column name=@column name
then rebuild your code.

after that, choose a datasource from your dropdown.
choose objectdatasource

select the one you first created on your dataset.
and from there you will select from which you want to control.


then from your gridview.
select the second one.


-CG-
 
Share this answer
 

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