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

Inserting Records to radgrid on external button click

5 Answers 455 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Amit
Top achievements
Rank 1
Amit asked on 18 Oct 2010, 01:18 PM
Hey everyone,

I am using this radgrid
<telerik:RadGrid ID="RadGrid1" runat="server" AllowFilteringByColumn="True" OnNeedDataSource="RadGrid1_NeedDataSource"
                AllowPaging="True" AllowSorting="True" GridLines="None" Skin="Office2007">
                <MasterTableView>
                    <RowIndicatorColumn>
                        <HeaderStyle Width="20px" />
                    </RowIndicatorColumn>
                    <ExpandCollapseColumn>
                        <HeaderStyle Width="20px" />
                    </ExpandCollapseColumn>
                    <EditFormSettings>
                        <FormTemplate>
                            <table border="0" cellpadding="0" cellspacing="0" width="100%">
                                <tr>
                                    <td colspan="2" style="font-size: small">
                                        <b>Order Details</b>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <table border="0" cellpadding="0" cellspacing="0" width="100%">
                                            <tr>
                                                <td>
                                                </td>
                                                <td>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Order Id:
                                                </td>
                                                <td>
                                                    <asp:Label ID="lblOrderId" runat="server" />
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Product Name:
                                                </td>
                                                <td>
                                                    <asp:DropDownList ID="ddlProductName" runat="server" DataSourceID="SqlDataSource1"
                                                        DataTextField="ProductName" DataValueField="Pid" />
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Rate:
                                                </td>
                                                <td>
                                                    <asp:Label ID="lblRate" runat="server" />
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Qunatity:
                                                </td>
                                                <td>
                                                    <asp:TextBox ID="txtQuantity" runat="server" />
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Amount:
                                                </td>
                                                <td>
                                                    <asp:Label ID="lblAmount" runat="server" />
                                                </td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                            </table>
                        </FormTemplate>
                    </EditFormSettings>
                </MasterTableView>
                <ClientSettings AllowColumnsReorder="True" ReorderColumnsOnClient="True">
                </ClientSettings>
            </telerik:RadGrid>
Now,i want to open insert form on button click that is outside the grid.My dataSource for radgrid is datatable--
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            dtValues = new DataTable();
            dtValues.Columns.Add("Items");
            dtValues.Columns.Add("Rate");
            dtValues.Columns.Add("Quantity");
            dtValues.Columns.Add("Amount");
            RadGrid1.DataSource = dtValues;
        }
How to proceed?...

Thanks
Amit

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 18 Oct 2010, 01:26 PM
Hello,


The following code will help you in opening insertform from from external control event.

CS:
protected void Button1_Click(object sender, EventArgs e)
{
    RadGrid1.MasterTableView.IsItemInserted = true;
    RadGrid1.MasterTableView.Rebind();
}



Thanks,
Princy.
0
Amit
Top achievements
Rank 1
answered on 18 Oct 2010, 01:34 PM
hello Princy,

Thanks,that worked perfectly.Just one more quick question.When i fill textboxes and click on insert,Will this work as automatic insert?...As i want these values to be inserted in the datatable,Which i've used as datasource for my grid.I am not using any declarative datasource for this.Or is there a way i can fire this event as i like. Suggest me some...

Thanks
Amit
0
Shinu
Top achievements
Rank 2
answered on 18 Oct 2010, 02:01 PM
Hello Amit,

The automatic data source operations only work when binding the grid to a declarative data source using the DataSourceID property of the grid. In your case, since you are not using declarative datasource, you need to access the insertform values update db using sql query.

In case of you want to invoke the insert operation from control outside grid, then make use of FireCommandEvent. For more information on FireCommandEvent will be available here:
How to fire command events from code

-Shinu.
0
Amit
Top achievements
Rank 1
answered on 19 Oct 2010, 08:39 AM
Hey,

let me elaborate my scenario a bit.I have grid,its datasouce is a datatable(dtValues) which is blank initially with some columns created in it.I have an external button btnAdd,on its click,edit template form opens(which is giving some errors).Edit form template have 2 buttons save and cancel on which i want my values on labels,textboxes and RadComboBox to be saved to the dataTable and hence to the RadGrid.I want to save records temporarily into the dataTable and RadGrid(and not in the database directly..).
My aspx is-
<telerik:RadGrid ID="RadGrid1" runat="server" AllowFilteringByColumn="True" OnNeedDataSource="RadGrid1_NeedDataSource"
                AllowPaging="True" AllowSorting="True" GridLines="None" Skin="Office2007" OnItemCreated="RadGrid1_ItemCreated"
                OnItemDataBound="RadGrid1_ItemDataBound">
                <MasterTableView>
                    <RowIndicatorColumn>
                        <HeaderStyle Width="20px" />
                    </RowIndicatorColumn>
                    <ExpandCollapseColumn>
                        <HeaderStyle Width="20px" />
                    </ExpandCollapseColumn>
                    <EditFormSettings EditFormType="Template">
                        <FormTemplate>
                            <table border="0" cellpadding="0" cellspacing="0" width="100%">
                                <tr>
                                    <td colspan="2" style="font-size: medium">
                                        <b>Enter your Order Details here:</b>
                                        <br />
                                        <br />
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <table border="0" cellpadding="0" cellspacing="0" width="100%" style="font-size: medium">
                                            <tr>
                                                <td>
                                                </td>
                                                <td>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td style="width: 15%">
                                                    Order Id:
                                                </td>
                                                <td align="left">
                                                    <asp:Label ID="lblOrderId" runat="server" />
                                                </td>
                                            </tr>
                                            <tr>
                                                <td style="width: 15%">
                                                    Product Name:
                                                </td>
                                                <td align="left" style="width: 85%">
                                                    <telerik:RadComboBox ID="RadComboBox1" runat="server" Skin="Office2007" DataSourceID="SqlDataSource1"
                                                        DataTextField="ProductName" MarkFirstMatch="true" DataValueField="Pid" AllowCustomText="true"
                                                        EmptyMessage="Please select an Item" Height="20px" Width="145px">
                                                    </telerik:RadComboBox>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td style="width: 15%">
                                                    Rate:
                                                </td>
                                                <td align="left" style="width: 85%">
                                                    <asp:Label ID="lblRate" runat="server" />
                                                </td>
                                            </tr>
                                            <tr>
                                                <td style="width: 15%">
                                                    Qunatity:
                                                </td>
                                                <td align="left" style="width: 85%">
                                                    <telerik:RadNumericTextBox ID="txtQuantityE" runat="server" Skin="Black" EmptyMessage="Enter Quantity"
                                                        SelectionOnFocus="CaretToEnd" ToolTip="Enter Quantity to place Order" Culture="English (United States)"
                                                        Height="14px" Width="141px" />
                                                </td>
                                            </tr>
                                            <tr>
                                                <td style="width: 15%">
                                                    Amount:
                                                </td>
                                                <td align="left" style="width: 85%">
                                                    <asp:Label ID="lblAmount" runat="server" />
                                                </td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                            </table>
                        </FormTemplate>
                    </EditFormSettings>
                </MasterTableView>
                <ClientSettings AllowColumnsReorder="True" ReorderColumnsOnClient="True">
                </ClientSettings>
            </telerik:RadGrid>
 
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ChalkHillConnectionString %>"
            DataSourceMode="DataReader" SelectCommand="SELECT [Pid], [ProductName] FROM [tblProducts]">
        </asp:SqlDataSource>
and cs is-
public static DataTable dtValues;
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
        protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            dtValues = new DataTable();
            dtValues.Columns.Add("Items");
            dtValues.Columns.Add("Rate");
            dtValues.Columns.Add("Quantity");
            dtValues.Columns.Add("Amount");
            RadGrid1.DataSource = dtValues;
        }
 
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            RadGrid1.MasterTableView.IsItemInserted = true;
            RadGrid1.MasterTableView.Rebind();
        }
        protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted)
            {
                GridEditFormInsertItem insertItem = (GridEditFormInsertItem)e.Item;
                RadComboBox combo = (RadComboBox)insertItem["RadComboBox1"].Controls[0];
                combo.AutoPostBack = true;
                combo.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(combo_SelectedIndexChanged); //attaching SelectedIndexChanged event
            }
        }
        protected void combo_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            RadComboBox combo = (RadComboBox)o;
            GridEditFormInsertItem insertItem = (GridEditFormInsertItem)combo.NamingContainer;
            Label lblRate = (Label)insertItem.FindControl("lblRate");
            SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["conn"].ToString());
            SqlCommand cmd = new SqlCommand("select [Rate] FROM [tblProducts] where ProductName=@ProductName", conn);
            cmd.Parameters.Add(new SqlParameter("@ProductName", combo.SelectedValue));
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                lblRate.Text = ds.Tables[0].Rows[0].ItemArray.GetValue(0).ToString();
            }
            RadNumericTextBox txtQauntityE = (RadNumericTextBox)insertItem.FindControl("txtQuantityE");
            txtQauntityE.Focus();
        }
        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted)
            {
                GridEditFormInsertItem insertItem = (GridEditFormInsertItem)e.Item;
                RadComboBox combo = (RadComboBox)insertItem["RadComboBox1"].Controls[0];
                combo.Items.Insert(0, new RadComboBoxItem("Select an Item", "0"));
            }
        }
I am getting error in RadGrid1_ItemDataBound .this is the error--

Item in insert mode does implement indexer only when the edit form is autogenerated.

for this line--
RadComboBox combo = (RadComboBox)insertItem["RadComboBox1"].Controls[0];

Please Help me with this....

Thanks
Amit
0
Accepted
Tsvetina
Telerik team
answered on 22 Oct 2010, 10:55 AM
Hello Amit,

I assume that you are not accessing the RadComboBox correctly. Please, check what happens if you replace RadComboBox1 with the name of the column which the combo is editor for (I suppose that would be "Product"):
RadComboBox combo = (RadComboBox)insertItem["ColumnUniqueName"].Controls[0];

Best wishes,
Tsvetina
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
Amit
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Amit
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Tsvetina
Telerik team
Share this question
or