Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
I want to bind the data from the other datatable in a dropdown control which is in the gridview.
Posted 11-Jun-11 3:20am
Edited 11-Jun-11 3:34am
thatraja239.9K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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

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
        }
    }
 
  Permalink  
v2
Comments
manuthebos at 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();
}
}
}
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
. If you use RowDataBound Event, On *.aspx page
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
</asp:GridView>
and on *.aspx.cs use this code below
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
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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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-
  Permalink  

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

  Print Answers RSS
0 thatraja 395
1 Sergey Alexandrovich Kryukov 295
2 Gihan Liyanage 261
3 kbrandwijk 204
4 CPallini 190
0 OriginalGriff 793
1 Sergey Alexandrovich Kryukov 604
2 Dnyaneshwar@Pune 604
3 Kornfeld Eliyahu Peter 530
4 thatraja 440


Advertise | Privacy | Mobile
Web04 | 2.8.140902.1 | Last Updated 12 Jun 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100