ASP.NET - How to bind the data to the dropdownlist which is in GridView

Asked By Ganesh Channa on 28-Sep-11 11:40 PM

 i am using Gridview in that i have one dropdownlist

 i am getting the data from the database for a dropdownlist using Method Getstatus(ObjList );

 but the data is not binding to the dropdownlist.............. 

 can u please help me in this...........
 

 
aneesa replied to Ganesh Channa on 28-Sep-11 11:52 PM
  protected void grdvire_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
           
                DropDownList ddlCollectionMode = (DropDownList)e.Row.FindControl("ddlCollectionMode");
                ddlCollectionMode.Items.Clear();
                ddlCollectionMode.DataSource=" call the datasource function";
                ddlCollectionMode.DataBind();
           
        }
    }
aneesa replied to Ganesh Channa on 28-Sep-11 11:55 PM
01.protected void grdvire_RowDataBound(object sender, GridViewRowEventArgs e)
02.   {
03.     if (e.Row.RowType == DataControlRowType.DataRow)
04.     {
05.       
06.         DropDownList ddlCollectionMode = (DropDownList)e.Row.FindControl("ddlCollectionMode");
07.         ddlCollectionMode.Items.Clear();
08.         ddlCollectionMode.DataSource=" call the datasource function";
09.         ddlCollectionMode.DataValueField="give the value field";
10.         ddlCollectionMode.DataTextField="give the text field";       
11.         ddlCollectionMode.DataBind();
12.       
13.     }
14.   }

smr replied to Ganesh Channa on 28-Sep-11 11:55 PM
Hi

In the RowDataBound event, simply add the following conditional:

if (myGridView.EditIndex == e.Row.RowIndex)
{
     //do work
}
smr replied to Ganesh Channa on 28-Sep-11 11:57 PM
HI

// Here new SQL query is pass to the SQL command object to get the data from tbl_order_status table.

mySqlCommand = new SqlCommand("select * from tbl_order_status", mySQLconnection);
 
mySqlAdapter = new SqlDataAdapter(mySqlCommand);
 
myDataSet = new DataSet();
 
mySqlAdapter.Fill(myDataSet);


 
// DropDownList Control Object Created to bind the data dynamically with each nested DropDownlist control placed inside the template column of the GridView Control.
DropDownList drdList;
 
// foreach loop is used to loop through each row of GridView Control.
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 Control.
  drdList.DataSource = myDataSet;
  drdList.DataValueField = "order_status_id";
  drdList.DataTextField = "order_status_name";
  drdList.DataBind();
}


refer
http://forums.asp.net/t/1185210.aspx
TSN ... replied to Ganesh Channa on 29-Sep-11 12:02 AM
hi..
Try this.
protected void gvEmp_RowEditing(object sender, GridViewEditEventArgs e)
{
  DemoGrid.EditIndex = e.NewEditIndex;
  BindGrid();
  DropDownList dropdown = gvEmp.Rows[e.NewEditIndex].FindControl("DropDownList1") as DropDownList;
  BindDropDown(dropdown);
}
 
private void BindDropDown(DropDownList temp)
{
  string connectionString = "Password=priyal;User ID=priyal;Initial Catalog=asptest;Data Source=dbsvr";
  string query = "select deptno from employees";
  DataSet dataset = new DataSet("Employees");
  using (SqlConnection connection = new SqlConnection(connectionString))
  {
    using (SqlDataAdapter adapter = new SqlDataAdapter(query, connection))
    {
      adapter.Fill(dataset);
    }
  }
 
  temp.DataSource = dataset;
  temp.DataTextField = "deptno";
  temp.DataValueField = "deptno";
  temp.DataBind();
}
.

Unless you pass the rows DropDownList as a parameter, how would your BindDropDown recognize which DropDownList to bind to?

hope this helps you...

Anoop S replied to Ganesh Channa on 29-Sep-11 01:02 AM
add one dropdownlist in templatefield in GridView make sure you add OnLoad event this event will fire for each row in gridview

....
....
<asp:TemplateField>
    <HeaderTemplate>Emp</HeaderTemplate>
    <ItemTemplate ><asp:DropDownList ID="ddl1" runat ="server" OnLoad="ddl1_load"></asp:DropDownList></ItemTemplate>
</asp:TemplateField>
....
....


Now in C# Code behind you can create OnLoad event handlar that will bind the data to the dropdown. In below example dropdown is bind with employee data.

....
....
protected void ddl1_load(object sender, EventArgs e)
{
    SqlDataAdapter da = new SqlDataAdapter("select emp from Employee", cn);
    DataTable dt = new DataTable();
    da.Fill(dt);
    DropDownList ddl = (DropDownList)sender;
    ddl.DataSource = dt;
    ddl.DataTextField = "EmpName";
    ddl.DataValueField = "EmpId";
    ddl.DataBind();
}
.....
.....
Radhika roy replied to Ganesh Channa on 29-Sep-11 01:05 AM

try this code-

DropDownList ddl = new DropDownList();

//Bind ddl with a dataset

ddl.DataSource = DS;

ddl.DataTextField = "Name";ddl.DataValueField = "ID";

ddl.DataBind();

//Bind the control in the grid view

e.Row.Cells(i).Controls.Add(ddl);

Hope this helps you.

Radhika roy replied to Ganesh Channa on 29-Sep-11 01:07 AM

try this code-

Protected Sub GridView4_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

If e.Row.RowType = DataControlRowType.DataRow AndAlso e.Row.RowIndex > 1 Then

'loop through the current row to add DropDownList to every cell.

For i As Integer = 2 To e.Row.Cells.Count - 1
e.Row.Cells(i).Controls.Add(New DropDownList())

Next

End If

End Sub


You have to bind dropdownlist before adding it in cell.

try following code

DropDownList ddl=new DropDownList();

ddl.DataSource=//Method to bind DropDownList

ddl.DataTextField="ColumnNameForText" ;

ddl.DataValueField="ColumnNameForValue";

ddl.DataBind();

ddl.Items.Insert("0",new ListItem("--select--","0"));

for(int i=0;i<e.Row.Cells.Count ;i++)

    e.Row.Cells[i].Controls.Add(ddl);

 hope this will hwlp you

Radhika roy replied to Ganesh Channa on 29-Sep-11 01:08 AM

try this code-

In the RowDataBound event, simply add the following conditional:

if (myGridView.EditIndex == e.Row.RowIndex) {    //do work }

hope this will help you

dipa ahuja replied to Ganesh Channa on 29-Sep-11 02:02 AM
One of the simple way i suggest you to use the SqlDataSource 

<EditItemTemplate>

<asp:DropDownList AutoPostBack="true" ID="DropDownList3" runat="server" DataSourceID="SqlDataSource1"
  DataTextField="empid" DataValueField="empid">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
  SelectCommand="SELECT [empid] FROM [emp]"></asp:SqlDataSource>
</EditItemTemplate>