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

RadComboBox outside RadGrid

10 Answers 63 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Simone
Top achievements
Rank 1
Simone asked on 07 Oct 2013, 12:30 PM
Hello ...
I have a combobox outside a RadGrid and I have to take the selected value and insert it in record.

I tried to use insertcommand but I don't know how to refer to the value.

With the normal asp.net controls it was quite easy ...

I wrote a code for Iteminserting event:
e.values("fieldName") = myValue ...


Now, I am disoriented ...


Thanks

10 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 07 Oct 2013, 12:47 PM
Hi Simone,

Please try the sample code snippet.Let me know if any concern.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnInsertCommand="RadGrid1_InsertCommand">
    <MasterTableView CommandItemDisplay="Top">
      . . .
    </MasterTableView>
</telerik:RadGrid>
 
<telerik:RadComboBox ID="RadComboBox1" runat="server" DataSourceID="SqlDataSource2" DataTextField="ShipCountry" DataValueField="ShipCountry" >
</telerik:RadComboBox>
 <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Northwind_newConnectionString3 %>"
 SelectCommand="SELECT distinct ShipCountry FROM [Orders]"></asp:SqlDataSource>

C#:
protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
    {
     string value= RadComboBox1.SelectedValue; //Access the selected value of the RadComboBox
    }

Thanks,
Princy
0
Simone
Top achievements
Rank 1
answered on 07 Oct 2013, 02:03 PM
Yes ... this is perfect to retrieve the value from ComboBox ...

but ... how to insert this value in record ??

"e.values" does not work ...
0
Princy
Top achievements
Rank 1
answered on 08 Oct 2013, 05:19 AM
Hi Simone,

Please try the sample code snippet that shows how to insert a value from RadComboBox.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource" AllowPaging="true" OnInsertCommand="RadGrid1_InsertCommand">           
   <MasterTableView DataKeyNames="OrderID" CommandItemDisplay="Top">              
      <Columns>      
         <telerik:GridBoundColumn HeaderText="OrderID" DataField="OrderID" ReadOnly="false" UniqueName="OrderID" />
         <telerik:GridBoundColumn HeaderText="EmployeeID" DataField="EmployeeID" UniqueName="EmployeeID">               
      </Columns>
   </MasterTableView>
 </telerik:RadGrid>
<telerik:RadComboBox ID="RadComboBox1" runat="server" DataSourceID="SqlDataSource2" DataTextField="ShipCountry" DataValueField="ShipCountry" >
</telerik:RadComboBox>      
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Northwind_newConnectionString3 %>"
SelectCommand="SELECT distinct ShipCountry FROM [Orders]"></asp:SqlDataSource>

C#:
public static string connection = WebConfigurationManager.ConnectionStrings["Northwind_newConnectionString3"].ConnectionString;
SqlConnection conn = new SqlConnection(connection);
protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
    {     
        if (e.Item is GridEditableItem )
        {
            string value = RadComboBox1.SelectedValue;
            GridEditableItem edit = (GridEditableItem)e.Item;
            TextBox tx=(TextBox)edit["OrderID"].Controls[0];
            TextBox txt = (TextBox)edit["EmployeeID"].Controls[0];        
            conn.Open();
            string query = "INSERT into Orders (OrderID,EmployeeID,ShipCountry ) VALUES ('" + tx.Text + "','" + txt.Text + "','"+value+"')";
            SqlCommand cmd = new SqlCommand(query, conn);
            cmd.ExecuteNonQuery();
            conn.Close();
        }
    }

Thanks,
Princy
0
Simone
Top achievements
Rank 1
answered on 08 Oct 2013, 06:43 AM
so I have to insert every field manually not limiting to the one provided by combobox ??
0
Konstantin Dikov
Telerik team
answered on 10 Oct 2013, 10:37 AM
Hello Simone,


Since I am not able to fully understand your exact requirement, could you please provide more information.

Nevertheless, if you want to change the entered value from the insert form depending on the selected value from the RadComboBox, you could handle the "ItemCommand" server-side event of the grid, get reference of the editor control for a column and change the value. Here is how you could achieve this:
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == "PerformInsert")
    {
        GridEditFormInsertItem editForm = (GridEditFormInsertItem)e.Item;
 
        //Get the insert control for specific column and change the value
        (editForm["CategoryName"].Controls[0] as TextBox).Text += RadComboBox1.SelectedValue;
    }
}

Hope that helps.


Regards,
Konstantin Dikov
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Simone
Top achievements
Rank 1
answered on 10 Oct 2013, 12:00 PM
Many thanks for your answer ... I am evaluating your product and I am trying to substitute standard gridview with rad one.

Using classic  listview I used this code to insert a specific value into a field, when inserting a record.

I would like how to programmatically enter a value in DB like I did in this example:
Protected Sub myLV_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewInsertEventArgs) Handles myLV.ItemInserting
 
 e.Values("myField") = myValue
0
Simone
Top achievements
Rank 1
answered on 15 Oct 2013, 06:45 AM
any helps ??
0
Konstantin Dikov
Telerik team
answered on 15 Oct 2013, 07:49 AM
Hi Simone,

For the RadListView you could use the following approach in order to manipulate the input data on insert:
VB:
Protected Sub RadListView1_ItemCommand(sender As Object, e As RadListViewCommandEventArgs)
    If e.CommandName = "PerformInsert" Then
        TryCast(e.ListViewItem.FindControl("TextBox1"), TextBox).Text = "new value"
    End If
End Sub
C#:
protected void RadListView1_ItemCommand(object sender, RadListViewCommandEventArgs e)
{
    if (e.CommandName == "PerformInsert")
    {
        (e.ListViewItem.FindControl("TextBox1") as TextBox).Text = "new value";
    }
}

Hope that helps.

 

Regards,
Konstantin Dikov
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Simone
Top achievements
Rank 1
answered on 15 Oct 2013, 07:57 AM
Can I use this with GridView too ??
0
Konstantin Dikov
Telerik team
answered on 15 Oct 2013, 01:35 PM
Hello Simone,

For your last question you could refer to this help article that demonstrates ways to access cells and rows in RadGrid. 

For general questions regarding our controls you could refer to our online help articles, where detail information is available for all RadControls.

Hope that helps.

 

Regards,
Konstantin Dikov
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
Simone
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Simone
Top achievements
Rank 1
Konstantin Dikov
Telerik team
Share this question
or