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

Combo in RadGrid with AutoGeneratedColumns="True"

1 Answer 68 Views
Grid
This is a migrated thread and some comments may be shown as answers.
juansimon
Top achievements
Rank 1
juansimon asked on 17 Mar 2012, 03:49 PM

Hi,

I need some help with your RadGrid.  I have a RadGrid with the option AutoGenerateColumns="True".  It has many columns from a Datasource that dynamically fills the Grid.  This DataSource returns a Datatable with many columns.  There is a table called orders and the other tables called city and product.  The select statement contains a combination of these tables in order to take the names of the city and the product for each order.   

My problem is: I want to edit a row with all CRUD operations using some ComboBoxes for some Columns in order to change the city or the product of an order using comboboxes. 

could you help me How can I achieve this?

Thanks

Juan Viteri

1 Answer, 1 is accepted

Sort by
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 18 Mar 2012, 04:46 PM
Hello juansimon,

Please check below code snippet.

<telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_OnNeedDataSource"
            AutoGenerateColumns="true" OnItemCreated="RadGrid_OnItemCreated"
            onupdatecommand="RadGrid1_UpdateCommand">
            <MasterTableView EditMode="InPlace">
                <Columns>
                <telerik:GridEditCommandColumn></telerik:GridEditCommandColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>

namespace TelerikTest.Web
{
    public partial class aaaa : System.Web.UI.Page
    {
        List<GridData> GridSourceList = null;
 
        protected void Page_Load(object sender, EventArgs e)
        {
            GridSourceList = new List<GridData>();
            GridSourceList.Add(new GridData("Abdul", "Abdul", 001, new DateTime(2012, 1, 1)));
            GridSourceList.Add(new GridData("Baskar", "Baskar", 002, new DateTime(2012, 1, 2)));
            GridSourceList.Add(new GridData("Chandran", "Chandran", 003, new DateTime(2012, 1, 3)));
        }
        protected void RadGrid1_OnNeedDataSource(object sender, EventArgs e)
        {  
            RadGrid1.DataSource = GridSourceList;
        }
        
        protected void RadGrid_OnItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                GridEditableItem editedItem = e.Item as GridEditableItem;
 
                RadComboBox rcb = new RadComboBox();
                rcb.ID = "rcb";
                rcb.Items.Add(new RadComboBoxItem("test1", "test1"));
                rcb.Items.Add(new RadComboBoxItem("test2", "test2"));
                rcb.Items.Add(new RadComboBoxItem("test3", "test3"));
                (editedItem["LASTNAME"].Controls[0] as TextBox).Visible = false;
                editedItem["LASTNAME"].Controls.Add(rcb);
            }
 
             
        }
 
        protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            RadComboBox rcb = editedItem["LASTNAME"].FindControl("rcb") as RadComboBox;
            string str = rcb.SelectedValue;
        }
 
        
    }
 
 
    public class GridData
    {
        public GridData(string strFirstName, string strLastname, int intID, DateTime dateTime)
        {
            _firstname = strFirstName;
            _lastname = strLastname;
            _id = intID;
            _date = dateTime;
        }
        private string _firstname;
        private string _lastname;
        private int _id;
        private DateTime _date;
        public DateTime DATE { get { return _date; } set { _date = value; } }
        public string NAME { get { return _firstname; } set { _firstname = value; } }
        public int ID { get { return _id; } set { _id = value; } }
        public string LASTNAME { get { return _lastname; } set { _lastname = value; } }
    }
}

Thanks,
Jayesh Goyani
Tags
Grid
Asked by
juansimon
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Share this question
or