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

[Solved] Accessing SelectedValue of RadComboBox1 within in RadGrid1 GridEditInsertItem to insert new value into SQL table

1 Answer 86 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jon-Jon Kershaw
Top achievements
Rank 2
Jon-Jon Kershaw asked on 20 Aug 2009, 07:13 PM
   protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)  
    {  
        if (e.CommandName == RadGrid.InitInsertCommandName)  
        {  
            GetData(RadGrid1, "SELECT [RoleID],[Rolename] FROM [vw_UsersinRoles] WHERE [UserID] = " + this.DropDownList4.SelectedValue + " ORDER BY [Username]");  
        }  
        else if (e.CommandName == RadGrid.UpdateCommandName)  
        {  
        }  
        else if (e.CommandName == RadGrid.DeleteCommandName)  
        {  
            SQLQuery("DELETE FROM [UsersinRoles] WHERE [UserID] = " + this.DropDownList4.SelectedValue + " AND [RoleID] = " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["RoleID"].ToString());  
        }  
        else if (e.CommandName == RadGrid.EditCommandName)  
        {  
        }  
        else if (e.CommandName == RadGrid.PerformInsertCommandName)  
        {  
            GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item.OwnerTableView.GetInsertItem();  
            string RoleIDstring = (insertedItem["RadComboBox2"].Controls[0] as RadComboBox).SelectedValue;  
            int RoleID = Convert.ToInt16(RoleIDstring);  
            SQLQuery("INSERT INTO [UsersinRoles] ([UserID],[RoleID]) VALUES (" + this.DropDownList4.SelectedValue + "," + RoleID + ")");  
        }  
    }  
 public static void GetData(RadGrid RadGrids, String SqlQuery)  
    {  
        DataTable dtTable = new DataTable();  
        string conn = ConfigurationManager.ConnectionStrings["SecurityConnectionString"].ToString();  
        SqlDataAdapter sqladp = new SqlDataAdapter();  
        SqlConnection sqlconn = new SqlConnection(conn);  
        sqlconn.Open();  
        try 
        {  
            string selectQuery = SqlQuery;  
            sqladp.SelectCommand = new SqlCommand(selectQuery, sqlconn);  
            sqladp.Fill(dtTable);  
            RadGrids.DataSource = dtTable;  
            RadGrids.DataBind();  
        }  
        finally 
        {  
            sqlconn.Close();  
        }  
    }  
 
    public static void SQLQuery(String SqlQuery)  
    {  
        string conn = ConfigurationManager.ConnectionStrings["SecurityConnectionString"].ToString();  
        SqlConnection sqlconn = new SqlConnection(conn);  
        sqlconn.Open();  
        try 
        {  
            SqlCommand cmd = new SqlCommand(SqlQuery, sqlconn);  
            cmd.ExecuteNonQuery();  
        }  
        finally 
        {  
            sqlconn.Close();  
        }  
    } 
  <telerik:RadGrid ID="RadGrid1" runat="server" AllowAutomaticUpdates="True" AllowAutomaticInserts="True" 
        AllowAutomaticDeletes="True" AllowPaging="True" EnableLinqExpressions="False" 
        OnItemCommand="RadGrid1_ItemCommand" AutoGenerateColumns="False" Width="310px" 
        BorderColor="Black" BorderWidth="1px" GridLines="None">  
        <HeaderContextMenu EnableTheming="True">  
            <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation> 
        </HeaderContextMenu> 
        <MasterTableView DataKeyNames="RoleID" CommandItemSettings-AddNewRecordText="Add New Role" 
            CommandItemDisplay="Top">  
            <CommandItemSettings AddNewRecordText="Add New Role"></CommandItemSettings> 
            <RowIndicatorColumn> 
                <HeaderStyle Width="20px"></HeaderStyle> 
            </RowIndicatorColumn> 
            <ExpandCollapseColumn> 
                <HeaderStyle Width="20px"></HeaderStyle> 
            </ExpandCollapseColumn> 
            <Columns> 
                <telerik:GridTemplateColumn UniqueName="DeleteColumn" ItemStyle-Width="16px" AllowFiltering="false">  
                    <HeaderStyle Width="16px" /> 
                    <ItemTemplate> 
                        <asp:ImageButton ID="imgbtnDelete" Width="14px" ToolTip="Delete Contact" runat="server" 
                            CommandName="Delete" ImageUrl="~/images/recycle.jpg" OnClientClick="javascript:if(!confirm('This action will delete the selected contact. Are you sure?')){return false;}" /> 
                    </ItemTemplate> 
                    <ItemStyle Width="16px"></ItemStyle> 
                </telerik:GridTemplateColumn> 
                <telerik:GridTemplateColumn HeaderText="Rolename" UniqueName="RolenameColumn">  
                    <EditItemTemplate> 
                        <telerik:RadComboBox ID="RadComboBox2" AppendDataBoundItems="true" Height="200px" 
                            runat="server" Skin="WebBlue" DataTextField="Rolename" DataValueField="RoleID" 
                            DataSourceID="RCBaddnewrole" SelectedValue='<%# Bind("Rolename")%>'>  
                            <Items> 
                                <telerik:RadComboBoxItem Text="" Value="" /> 
                            </Items> 
                        </telerik:RadComboBox> 
                    </EditItemTemplate> 
                    <ItemTemplate> 
                        <%#Eval("Rolename")%> 
                    </ItemTemplate> 
                </telerik:GridTemplateColumn> 
            </Columns> 
            <EditFormSettings ColumnNumber="3" CaptionFormatString="Edit details for User {0}" 
                CaptionDataField="RoleID">  
                <EditColumn UniqueName="EditCommandColumn1">  
                </EditColumn> 
                <FormTemplate> 
                    <asp:Table runat="server" ID="Table1" CellSpacing="1" CellPadding="1" Width="250" 
                        border="0">  
                        <asp:TableRow> 
                            <asp:TableCell ID="RCB2">  
                                <telerik:RadComboBox ID="RadComboBox2" runat="server" DataSourceID="RCBaddnewrole" 
                                    DataTextField="Rolename" DataValueField="RoleID">  
                                </telerik:RadComboBox> 
                            </asp:TableCell> 
                        </asp:TableRow> 
                        <asp:TableRow> 
                            <asp:TableCell> 
                                <asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>' 
                                    runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'>  
                                </asp:Button>&nbsp;  
                                <asp:Button CausesValidation="false" ID="btnEdit" Text="EditRole" runat="server" 
                                    CommandName="EditRole"></asp:Button> 
                                <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" 
                                    CommandName="Cancel"></asp:Button></td>  
                            </asp:TableCell> 
                        </asp:TableRow> 
                    </asp:Table> 
                </FormTemplate> 
            </EditFormSettings> 
            <NoRecordsTemplate> 
                No Records  
            </NoRecordsTemplate> 
        </MasterTableView> 
    </telerik:RadGrid> 
    <asp:SqlDataSource ID="RCBaddnewrole" runat="server" ConnectionString="<%$ ConnectionStrings:LawsonSecurityConnectionString %>" 
        SelectCommand="SELECT [Rolename], [RoleID] FROM [Roles] ORDER BY [Rolename]">  
    </asp:SqlDataSource> 

Trying to access the RadComboBox item within the add new to use the selectedvalue to nisert into the SQL table.

I've hitting my head trying to access it through different ways but have no idea.

Is there a way if you add certain controls to different areas of a radgrid you can do a reverse look up on the item and know it's location within the radgrid? 

Thanks again you guys are awesome.

1 Answer, 1 is accepted

Sort by
0
Jon-Jon Kershaw
Top achievements
Rank 2
answered on 20 Aug 2009, 10:29 PM
weird.  i tried e.item.findcontrol("radcombobox2") before and it didn't work but now...voila.  strange how those things work.
Tags
Grid
Asked by
Jon-Jon Kershaw
Top achievements
Rank 2
Answers by
Jon-Jon Kershaw
Top achievements
Rank 2
Share this question
or