UpdateCommand for RadGrid dropdownlist inplace editing

1 Answer 21 Views
DropDownList Grid
IT
Top achievements
Rank 1
IT asked on 17 Jan 2022, 08:30 PM

Hello!

I am trying to configure my updatecommand for a RadGrid dropdownlist edit entry. I have attached the code for the dropdownlist and my current update command below 

DropDownList

if (e.Item is GridEditableItem && e.Item.IsInEditMode && !(e.Item is GridEditFormInsertItem))
        {
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString);
            SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM [ExampleDB].[dbo].[Status]", connection);
            DataSet ds = new DataSet();
            da.Fill(ds);

            GridEditableItem edit = (GridEditableItem)e.Item;
            TextBox txt = (TextBox)edit["Status"].Controls[0];
            txt.Visible = false;
            DropDownList droplist = new DropDownList();
            droplist.DataSource = ds;
            droplist.DataTextField = "Name";
            droplist.DataValueField = "ID";
            droplist.DataBind();
            edit["OrderStatus"].Controls.Add(droplist);
        }

 

Update Command

protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        GridEditableItem editableItem = ((GridEditableItem)e.Item);
        Hashtable values = new Hashtable();
        editableItem.ExtractValues(values);
        string query = "UPDATE [MSIMM].[dbo].[Order] SET OrderStatusID=@OrderStatusID";
        DataAccess(Command: RadGrid.UpdateCommandName, Query: query, OrderStatusID: values["ID"].ToString());

    }

1 Answer, 1 is accepted

Sort by
0
Attila Antal
Telerik team
answered on 20 Jan 2022, 03:35 PM

Hi,

Since you have created the DropDownList dynamically, there is no Bind() expression, and so extracting the values has to be done manually by accessing the control in the Edit Item, see Updating Values Using UserControl and FormTemplate

For more examples accessing Controls in the Grid check out the Accessing Values and Controls article, see https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/accessing-values-and-controls/overview

If you want the "editableItem.ExtractValues(values);" to extract the values automatically, you will need to create a Template column and use the Bind() expression like so:

 

<telerik:GridTemplateColumn HeaderText="My DropDownList">
    <ItemTemplate>
        Something here
    </ItemTemplate>

    <EditItemTemplate>
        <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind("OrderStatusID") %>' OnDataBinding="DropDownList1_DataBinding"></asp:DropDownList>
    </EditItemTemplate>
</telerik:GridTemplateColumn>

 

Binding data to DropDownList

protected void DropDownList1_DataBinding(object sender, EventArgs e)
{

    SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString);
    SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM [ExampleDB].[dbo].[Status]", connection);
    DataSet ds = new DataSet();
    da.Fill(ds);

    var droplist = (DropDownList)sender;
    droplist.DataSource = ds;
    droplist.DataTextField = "Name";
    droplist.DataValueField = "ID";
}

 

 

Regards,
Attila Antal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

anna
Top achievements
Rank 1
Bronze
Iron
commented on 21 Jan 2022, 06:00 PM

Attached File : ex.png

telerik:RadGrid (ASP.NET Web)

I want to get the header name of the column clicked by the user with the mouse.

<ClientSettings EnablePostBackOnRowClick="true">  
<Selecting AllowRowSelect="true" /> 
</ClientSettings>   

 protected void RadGrid1_SelectedIndexChanged(object sender, EventArgs e)
        {
            var dataItem = gv.SelectedItems[0] as GridDataItem;
            if (dataItem != null)
            {
                var name = dataItem["ColUniqueName"].Text;
            }
        }
The above source code is not what I want.
Because the user specified ColUniqueName .
I want to automatically get the coordinates of Row and Column&Column HeaderName when the mouse cursor is placed on the Cell of RadGrid.

Vessy
Telerik team
commented on 24 Jan 2022, 06:04 PM

Hello anna,

We would like to kindly ask you to post your questions only once and make sure that the chosen threads are matching the problem faced by you. You can find the answer to the question above in the thread below:

https://www.telerik.com/forums/radgrid-i-want-to-automatically-get-the-coordinates-of-row-and-column-when-the-mouse-cursor-is-placed-on-the-cell-of-radgrid

Tags
DropDownList Grid
Asked by
IT
Top achievements
Rank 1
Answers by
Attila Antal
Telerik team
Share this question
or