This is a migrated thread and some comments may be shown as answers.

How to bind dropdownlist in Insert Mode

3 Answers 177 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Vivek Tyagi
Top achievements
Rank 1
Vivek Tyagi asked on 23 Oct 2011, 07:54 PM
how do i bind the dropdownlist in insert or edit mode and then how the Update or Insert command will find the control...
i tried a lot didnt worked

here is the code

protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
     {
         SqlDataAdapter da = new SqlDataAdapter("select c.City, s.State from MST_State as s inner join MST_City as c on c.StateID = s.ID ", con);
         DataTable mydatatable = new DataTable();
         da.Fill(mydatatable);
 
         RadGrid1.DataSource = mydatatable.DefaultView;
 
     }
     //protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
     //{
     //   
     //}
         protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
     {
        //GridEditableItem editedItem = e.Item as GridEditableItem;
        //UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);
        GridEditFormInsertItem inserteditem = (GridEditFormInsertItem)e.Item;
        //Create new row in the DataSource
        
 
        //Insert new values
 
         string City = (inserteditem["City"].Controls[0] as TextBox).Text;
 
 
         DropDownList list = (inserteditem.FindControl("State") as DropDownList);
         string State = list.SelectedValue;
 
 
 
 
 
         con.Open();
         string insertquery = String.Format("insert into MST_City values('{0}',  '{1}')", City, State);
 
         SqlCommand sql = new SqlCommand();
         sql.CommandText = insertquery;
         sql.Connection = con;
         sql.ExecuteNonQuery();
         con.Close();
 
     }

and i want when i click add new record the state should be binded in dropdownlist...

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 24 Oct 2011, 04:54 AM
Hello Vivek,

You can try the following method to access DropDownList in InsertCommand and UpdateCommand.
C#:
protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
{
  GridEditFormInsertItem insertItem1 = e.Item as GridEditFormInsertItem;
  DropDownList list = (DropDownList)insertItem1.FindControl("State");
   //your insert query here
}
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
{
   GridEditableItem editItem = e.Item as GridEditableItem;
   DropDownList drop = (DropDownList)editItem.FindControl("State");
}

Thanks,
Princy.
0
Vivek Tyagi
Top achievements
Rank 1
answered on 24 Oct 2011, 05:44 AM
Princy thanks for your support

but still its showing textbox, b'coz i want it to be genrated in Runtime when i click Add new record...

i wanted to do it from code behind only... (.cs file)
Any Help?
0
Princy
Top achievements
Rank 2
answered on 25 Oct 2011, 07:58 AM
Hello Vivek,

Try the following code snippet to add a DropDownList on clicking AddNewRecord button.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
  if (e.Item is GridEditFormInsertItem)
   {
      GridEditFormInsertItem item = (GridEditFormInsertItem)e.Item;
      TableCell cell = item["UniqueName"];
      DropDownList dropdown = new DropDownList();
    dropdown.ID = "DropDownList1";
      //bind your drop down here
      cell.Controls.Add(list);
   }
}

Thanks,
Princy.
Tags
Grid
Asked by
Vivek Tyagi
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Vivek Tyagi
Top achievements
Rank 1
Share this question
or