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

Can i show an empty RadGrid For Inserting new records?..

13 Answers 295 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Amit
Top achievements
Rank 1
Amit asked on 11 Oct 2010, 11:01 AM
Hey everyone,

I am working with RadGrid,everything(Edit,Insert and Delete) works fine when there is data in grid.But when i want Grid to be used only for Inserting new records,the grid is not visible,only a single line appears in the web page.Is this not possible?..If yes HOW?..

Thanks
Amit

13 Answers, 1 is accepted

Sort by
0
Amit
Top achievements
Rank 1
answered on 11 Oct 2010, 12:02 PM
Hey,

Now i've proceeded a bit by applying a false select query to the Sql Data source of the radGrid.It is visible now.My grid now looks like in attached file...

Now,how can i bring the rates of the selected item from the dropdown list to the label in front of rate on ddl's selected index change?...
Also,one more thing radgrid events like itemCreated,itemDataBound,NeedDataSource,ItemCommand--When to use which?.. any link that
that provides thorough explanation of these events.Which will be required for inserting new records?....
0
Accepted
Princy
Top achievements
Rank 2
answered on 11 Oct 2010, 01:06 PM
Hello Amit,

Here is the sample code that I tried in my application for similar kind of scenario(set Text of Label based on the SelectedValue of RadComboBox.)

ASPX:
<telerik:GridTemplateColumn>
    <EditItemTemplate>
       <telerik:RadComboBox ID="RadComboBox1" runat="server"
          DataSourceID="SqlDataSource1" DataTextField="EmployeeID"
          DataValueField="EmployeeID" AutoPostBack="true"
          SelectedValue='<%#Bind("EmployeeID") %>'
          OnSelectedIndexChanged="RadComboBox1_SelectedIndexChanged">
       </telerik:RadComboBox>
    </EditItemTemplate>
 </telerik:GridTemplateColumn>
 <telerik:GridTemplateColumn>
    <EditItemTemplate>
        <asp:Label ID="FirstName" runat="server" ></asp:Label>
    </EditItemTemplate>
 </telerik:GridTemplateColumn>

C#:
protected void RadComboBox1_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
   {
       RadComboBox combo = (RadComboBox)o;
       GridEditFormInsertItem insertItem = (GridEditFormInsertItem)combo.NamingContainer;
       Label lbname = (Label)insertItem.FindControl("FirstName");
       SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
       SqlCommand cmd = new SqlCommand("select [FirstName] FROM [Employees] where EmployeeID=@EmployeeID ", con);
       cmd.Parameters.Add(new SqlParameter("@EmployeeID", combo.SelectedValue));
       SqlDataAdapter da = new SqlDataAdapter(cmd);
       DataSet ds = new DataSet();
       da.Fill(ds);
       lbname.Text = ds.Tables[0].Rows[0].ItemArray.GetValue(0).ToString();  
   }

And please refer the following documentation.
Server Side Events
Distinguishing the major differences between ItemCreated and ItemDataBound events
Advanced Data-binding (using NeedDataSource event)

Thanks,
Princy.
0
Amit
Top achievements
Rank 1
answered on 11 Oct 2010, 01:14 PM
Actually,I am using a GridDropDown Column and i've used a Sql datasource for populating Combo box.Under which event should i write my code and how to call selectedIndexChange?..

ItemCreated or ItemDataBound?...
0
Accepted
Princy
Top achievements
Rank 2
answered on 12 Oct 2010, 08:48 AM
Hello Amit,

Try the following code snippet in ItemCreated event to achieve your requirement.

ASPX:
<telerik:GridDropDownColumn ListTextField="EmployeeID" ListValueField="EmployeeID"
   DataSourceID="SqlDataSource1" UniqueName="GridDropDownColumn">
</telerik:GridDropDownColumn>

C#:
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["GridDropDownColumn"].Controls[0];
           combo.AutoPostBack = true;
           combo.SelectedIndexChanged+=new RadComboBoxSelectedIndexChangedEventHandler(combo_SelectedIndexChanged); //attaching SelectedIndexChanged event
       }
   }
   protected void combo_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
   {
 
   }

Thanks,
Princy.
0
Amit
Top achievements
Rank 1
answered on 14 Oct 2010, 06:02 AM
Hey PRINCY,

I am doing this now.aspx--
<telerik:GridDropDownColumn HeaderText="Product Name" UniqueName="GridDropDownColumn"
                        DataSourceID="SqlDataSource1" ListTextField="ProductName" DataField="ProductName" DropDownControlType="RadComboBox">
                    </telerik:GridDropDownColumn>
                    <telerik:GridTemplateColumn HeaderText="Rate" UniqueName="RateTemplateColumn">
                        <ItemTemplate>
                            <asp:Label ID="lblRate" runat="server" />
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Label ID="lblRateE" runat="server" />
                        </EditItemTemplate>
                    </telerik:GridTemplateColumn>
and CS is--
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["GridDropDownColumn"].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("lblRateE");
            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);
            lblRate.Text = ds.Tables[0].Rows[0].ItemArray.GetValue(0).ToString();  
        }
its throwing error -

There is no row at position 0.

I want lblRate.Text to show Rate of the selected item from combobox.


Thanks
Amit
0
Accepted
Princy
Top achievements
Rank 2
answered on 14 Oct 2010, 06:44 AM
Hello Amit,

Since you have not set the ListValueField property of GridDropDownColumn, combo.SelectedValue returns null. I guess thats why you are getting this error. In order to avoid this, set ListValueField of GridDropDownColumn to corresponding field (or use combo.SelectedItem.Text to get the selected item of RadComboBox).

ASPX:
<telerik:GridDropDownColumn HeaderText="Product Name"  
  UniqueName="GridDropDownColumn"                   
  DataSourceID="SqlDataSource1" ListTextField="ProductName" 
  ListValueField="ProductName" DataField="ProductName"
  DropDownControlType="RadComboBox">
</telerik:GridDropDownColumn>

Thanks,
Princy.
0
Amit
Top achievements
Rank 1
answered on 14 Oct 2010, 07:01 AM
Hey Princy,

Thanks Man,that resolves the problem.I jus wanna ask one more thing..i am adding a new item to the combobox like this...
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["GridDropDownColumn"].Controls[0];
                combo.Items.Insert(0, new RadComboBoxItem("Select an Item", "0"));
                combo.SelectedIndex = 0;
                combo.AutoPostBack = true;
                combo.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(combo_SelectedIndexChanged); //attaching SelectedIndexChanged event
            }
        }

but it does'nt add to the list.

Thanks
Amit
0
Accepted
Princy
Top achievements
Rank 2
answered on 14 Oct 2010, 07:33 AM
Hello Amit,

If you want to add new item to RadComboBox, try it in ItemDataBound event rather than ItemCreated event. The ItemCreated event is too early to bind the combo and added items are overridden when binding the combo.

C#:
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["GridDropDownColumn"].Controls[0];
        combo.Items.Insert(0, new RadComboBoxItem("Select an Item", "0"));
      }
 }

Thanks,
Princy.
0
Amit
Top achievements
Rank 1
answered on 14 Oct 2010, 08:33 AM
Hey,

Jus a quick question once again.After label shows the Rate of the selected item,how to set focus on the RadNumericTextBox that i've used for inputting no. of quantity user wants to place in Order.I am doing this--

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["GridDropDownColumn"].Controls[0];
                combo.Items.Insert(0, new RadComboBoxItem("Select an Item", "0"));
                RadNumericTextBox txtQauntityE = (RadNumericTextBox)insertItem["QuantityTemplateColumn"].Controls[0];
                txtQauntityE.Focus();
            }
        }
But its showing error--
Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'Telerik.Web.UI.RadNumericTextBox'.

Thanks
Amit.
0
Accepted
Shinu
Top achievements
Rank 2
answered on 14 Oct 2010, 11:06 AM
Hello Amit,


For referencing control in editform if it is inside (GridTemplateColumn/FormTemplate), then use the Findcontrol(controlID) method.

Code:
 RadNumericTextBox txtQauntityE = (RadNumericTextBox)insertItem.FindControl("QuantityTemplateColumn");



-Shinu.
0
Amit
Top achievements
Rank 1
answered on 14 Oct 2010, 11:14 AM
Hello Shinu,

It looks like this now--
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["GridDropDownColumn"].Controls[0];
                combo.Items.Insert(0, new RadComboBoxItem("Select an Item", "0"));
                RadNumericTextBox txtQauntityE = (RadNumericTextBox)insertItem.FindControl("QuantityTemplateColumn");
                txtQauntityE.Focus();
            }
        }
gettin this error--Object reference not set to an instance of an object.(NullReferenceException)

I am getting Rate of selected item from combobox on to a label.After which i want to set focus on textBox within same event.

Thanks
Amit
0
Accepted
Shinu
Top achievements
Rank 2
answered on 15 Oct 2010, 08:14 AM
Hi Amit,


Here is my the modified code. Try this and see whether it helps.


ASPX:
<Columns>
       <telerik:GridDropDownColumn UniqueName="GridDropDownColumn" DataField="ProductName"
           DataSourceID="SqlDataSource1" ListTextField="ProductName" ListValueField="ProductName">
       </telerik:GridDropDownColumn>
       <telerik:GridTemplateColumn>
           <EditItemTemplate>
               <asp:Label ID="lblRateE" runat="server" Text="Label"></asp:Label>
           </EditItemTemplate>
           <ItemTemplate>
               ...
           </ItemTemplate>
       </telerik:GridTemplateColumn>
       <telerik:GridTemplateColumn UniqueName="GridTemplateColumn">
           <EditItemTemplate>
               <telerik:RadNumericTextBox ID="QtyNumericTextBox" runat="server">
               </telerik:RadNumericTextBox>
           </EditItemTemplate>
           <ItemTemplate>
               ...
           </ItemTemplate>
       </telerik:GridTemplateColumn>


Code:
protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted)
    {
        GridEditFormInsertItem insertItem = (GridEditFormInsertItem)e.Item;
        RadComboBox combo = (RadComboBox)insertItem["GridDropDownColumn"].Controls[0];
        combo.AutoPostBack = true;
        // Attach selectedIndexChanged event to dropdown control
        combo.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(combo_SelectedIndexChanged);
    }
}
 
 
void combo_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
{
    RadComboBox combo = (RadComboBox)o;
    GridEditFormInsertItem insertItem = (GridEditFormInsertItem)combo.NamingContainer;
    Label lblRate = (Label)insertItem.FindControl("lblRateE");
    // Your code to get string to set to the label
    lblRate.Text = "text changed"; / Set the text here
    // Access the numericTextBox here
    RadNumericTextBox txtQauntityE = (RadNumericTextBox)insertItem.FindControl("QtyNumericTextBox");
    // Set the focus to numerictextbox
    txtQauntityE.Focus();
}


Happy coding,
Shinu.
0
Amit
Top achievements
Rank 1
answered on 15 Oct 2010, 10:56 AM
Thanks Shinu,that worked just perfectly...:)
Tags
Grid
Asked by
Amit
Top achievements
Rank 1
Answers by
Amit
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Shinu
Top achievements
Rank 2
Share this question
or