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

Populate correct value for GridDropDownColumn inside RadGrid

1 Answer 246 Views
Grid
This is a migrated thread and some comments may be shown as answers.
SilverFish
Top achievements
Rank 1
SilverFish asked on 21 Feb 2017, 03:43 AM

I am using Telerik.Web.UI version 2017.1.118.40 with Visual studio 2010.

I have an issue using GridDropDownColumn inside RadGrid. In Edit mode when data is displayed in dropdownlist, its data is reset (i.e. it is not displaying the text which was in Item template or on the form for that row). I cannot use SQLDataSource, so please do not give me sample code using that.

Here is the ASPX code :

            <telerik:RadGrid ID="RadGrid1"runat="server"AutoGenerateColumns="False"GridLines="None"
                AllowMultiRowEdit="True"OnItemDataBound="RadGrid1_ItemDataBound"OnNeedDataSource="RadGrid1_NeedDataSource"
                OnUpdateCommand="RadGrid1_UpdateCommand">
                <MasterTableViewAutoGenerateColumns="false"DataKeyNames="AccessID"Width="100%"
                    CommandItemDisplay="Top">
                    <Columns>
                        <telerik:GridButtonColumn CommandName="Delete"Text="Delete"UniqueName="Delete">
                        </telerik:GridButtonColumn>
                      <telerik:GridBoundColumn DataField="AccessID" DataType="System.Int32" FilterControlAltText="Filter AccessID column"
                            HeaderText="Access ID" ReadOnly="True"SortExpression="AccessID"  UniqueName="AccessID">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="FirstName"FilterControlAltText="Filter FirstName column"
                            HeaderText="First Name" ReadOnly="True" SortExpression="FirstName" UniqueName="FirstName">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="LastName"FilterControlAltText="Filter LastName column"
                            HeaderText="Last Name" ReadOnly="True" SortExpression="LastName" UniqueName="LastName">
                        </telerik:GridBoundColumn>

                        <telerik:GridDropDownColumnFilterControlAltText="Filter ddlForm column" UniqueName="ddlForm"
                            ListTextField="FormName"ListValueField="FormID"SortExpression="FormName"
 HeaderText="Form Name"
                            DataField="FormID"ListDataMember="FormName"DropDownControlType="DropDownList">
                        </telerik:GridDropDownColumn>

                        <telerik:GridBoundColumn DataField="ContactType"FilterControlAltText="Filter ContactType column"
                            HeaderText="Contact Type" SortExpression="ContactType" UniqueName="ContactType">
                        </telerik:GridBoundColumn>
                        <telerik:GridEditCommandColumn>
                        </telerik:GridEditCommandColumn>
                    </Columns>
                    <EditFormSettingsColumnNumber="2"CaptionFormatString="Edit details for Access ID {0}"
                        CaptionDataField="AccessID">
                        <FormTableItemStyle Wrap="False"></FormTableItemStyle>
                        <FormCaptionStyle CssClass="EditFormHeader"></FormCaptionStyle>
                        <FormMainTableStyle CellSpacing="0"CellPadding="3"Width="100%"/>
                        <FormTableStyle GridLines="Horizontal"CellSpacing="0"CellPadding="2"CssClass="module"
                            Height="110px"Width="100%"/>
                        <FormTableAlternatingItemStyleWrap="False"></FormTableAlternatingItemStyle>
                        <FormStyle Width="100%"BackColor="#EEF2EA"></FormStyle>
                        <EditColumn UpdateText="Update record" UniqueName="EditCommandColumn1" CancelText="Cancel edit">
                        </EditColumn>
                        <FormTableButtonRowStyle HorizontalAlign="Right" CssClass="EditFormButtonRow"></FormTableButtonRowStyle>
                    </EditFormSettings>
                </MasterTableView>
            </telerik:RadGrid>

Here is code behind:
    protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {

            SqlConnection con = newSqlConnection(DataAccess.Connection.StrConn);
            SqlCommand cmd = newSqlCommand("dbo.MyStoredProc", con);
            SqlDataAdapter adapter = newSqlDataAdapter();
            adapter.SelectCommand = cmd;

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandTimeout = 90;

            DataTable myDataTable = newDataTable();

            con.Open();
            try
            {
                adapter.Fill(myDataTable);
            }
            finally
            {
                con.Close();
            }

            RadGrid1.DataSource = myDataTable;
        }


        protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {

                GridEditableItem editedItem = e.Item as GridEditableItem;

                GridEditManager editMan = editedItem.EditManager;

                GridDropDownListColumnEditor editor = editMan.GetColumnEditor("ddlForm") as GridDropDownListColumnEditor;
                DropDownList ddList = editor.DropDownListControl;
                ddList.DataSource = GetDataTable("SELECT [FormID], FormName  FROM  dbo.[Table_FormList]");  //ds;
                ddList.DataTextField = "FormName";
                ddList.DataValueField = "FormID";
                ddList.DataBind();

            }
            }
            public DataTable GetDataTable(string query)
            {
                SqlConnection con = new SqlConnection(DataAccess.Connection.StrConn);
                SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.SelectCommand = new SqlCommand(query, con);
                DataTable myDataTable = new DataTable();
                con.Open();
                try
                {
                    adapter.Fill(myDataTable);
                }
                finally
                {
                  con.Close();
                }
                return myDataTable;
            }

 

 

1 Answer, 1 is accepted

Sort by
0
Marin Bratanov
Telerik team
answered on 23 Feb 2017, 08:26 AM

Hi,

Could you take a look in my answer in your other thread: http://www.telerik.com/forums/sample-project-for-radgrid-using-dropdown-for-edit-insert? I hope it covers your questions. As a general rule of thumb, opening several threads with the same question does not result in faster answers or greater popularity, it usually dilutes the thread and scatters information, making it harder to get help.

If this does not help, please explain what is the outstanding issue with the data, because the code generally seems fine.

Regards,

Marin Bratanov
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
SilverFish
Top achievements
Rank 1
Answers by
Marin Bratanov
Telerik team
Share this question
or