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

Insert Row to radgrid

11 Answers 526 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Neema Sridhar
Top achievements
Rank 1
Neema Sridhar asked on 22 Jul 2010, 11:27 AM
Hi,

I have a radgrid with 2 columns, the first column has a dropdown and the second column has a textbox. outside the grid there is a Add "Add ROw" which should insert one row to the grid. The newly added row should have  a dropdown(binded with soime data) in 1st and textox in the 2nd column. The dat entered or selected for previous row must be retained.

Please let me know how this can be achevied.
Please reply at the earliest.

11 Answers, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 22 Jul 2010, 02:55 PM
Hi Neema,

RadGrid is data-bound control and its items are created based on the records in its datasource and display the datasource data. Therefore in order to add new row in the grid, you need to add new record in its datasource and rebind the grid.

Try it out and let me know if any issues arise.

Best wishes,
Iana
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
0
Neema Sridhar
Top achievements
Rank 1
answered on 23 Jul 2010, 06:24 AM
I tried the way you had mentioned. Stored the initial datasource in a viestate and on click of the button  i am retv from the viewstate and adding a new record to the datasource and calling ReBind(). But this is getting rendered with a blank dropdown and Item_DataBound event is not getting triggered.

Please let me know of any alternative or for any solution
0
Iana Tsolova
Telerik team
answered on 23 Jul 2010, 07:42 AM
Hi Neema,

Can you send the page code with a sample data? I will try to create a runnable sample for you based on it to illustrate the suggested solution.

Greetings,
Iana
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
0
Neema Sridhar
Top achievements
Rank 1
answered on 23 Jul 2010, 07:58 AM
ASPX CODE
<telerik:RadGrid runat="server" ID="dgInst" AllowSorting="true" EnableEmbeddedSkins="false"
            Skin="NextGen" AllowAutomaticDeletes="True" AutoGenerateColumns="false" OnItemCommand="dgInst_ItemCommand">
            <ClientSettings>
                <Scrolling AllowScroll="false" />
            </ClientSettings>
            <MasterTableView HorizontalAlign="center" AutoGenerateColumns="false" HeaderStyle-BackColor="#add3f7">
                <RowIndicatorColumn Visible="False">
                    <HeaderStyle Width="20px" />
                </RowIndicatorColumn>
                <ExpandCollapseColumn Resizable="false" Visible="false">
                    <HeaderStyle Width="20px" />
                </ExpandCollapseColumn>
                <CommandItemSettings></CommandItemSettings>
                  
                <Columns>
                    <telerik:GridTemplateColumn>
                        <HeaderTemplate>
                            <asp:Label ID="lblHdrSNo" Text="Instruction Code" runat="server"></asp:Label>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <telerik:RadComboBox ID="radcmbInstCode" runat="server" Width="300px" MaxHeight="100"
                                CssClass="cmb_borderstyle" EnableEmbeddedSkins="false" Skin="NextGen">
                                <CollapseAnimation Duration="200" Type="OutQuint" />
                            </telerik:RadComboBox>
                        </ItemTemplate>
                        <ItemStyle Width="20px" />
                    </telerik:GridTemplateColumn>
                    <telerik:GridTemplateColumn>
                        <HeaderTemplate>
                            <p>
                                <asp:Label ID="lblHdrInstDesc" Text="Description " runat="server"></asp:Label>
                            </p>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <div class="cmb_width190">
                                <div class="cmb_ltFlt cmb_vsp3">
                                    <asp:TextBox ID="radGtxtInstDesc" Width="400px" EnableEmbeddedSkins="false" CssClass="cmb_textbox"
                                        runat="server" MaxLength="40" BackColor="White">
                                    </asp:TextBox>
                                </div>
                            </div>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                      
                </Columns>
                <HeaderStyle BackColor="#ADD3F7"></HeaderStyle>
            </MasterTableView>
        </telerik:RadGrid>

<

 

asp:Button ID="btnAddRow" Text="Add Row" runat="server" Height="30px" Width="200px"

 

 

CausesValidation="false" OnClick="btnAddRow_Click" />

 



COde Behind

//Methods to add empty rows to grid
  
 static DataTable addNewRow(DataTable dtCode)
        {
            DataRow drNewRow = dtCode.NewRow();
            dtCode.Rows.Add(drNewRow);
            dtCode.AcceptChanges();
            return dtCode;
        }
        static int totalRows = 0;
          
        public void ShowEmptyGrid(int rows)
        {
            DataTable dtCode = new DataTable();
            dtCode.Locale = System.Globalization.CultureInfo.InvariantCulture;
            dtCode.Columns.Add("Code");
            for (int i = 0; i < rows; i++)
            {
                dtCode = addNewRow(dtCode);
            }
            //dgInst.ItemCreated += new GridItemEventHandler(dgInst_ItemCreated);
            dgInst.DataBound += new EventHandler(dgInst_DataBound);
  
            dgInst.DataSource = dtCode;
            dgInst.DataBind();
            ViewState["Data"] = dtCode;
  
        }
  
  
 void dgInst_DataBound(object sender, EventArgs e)
        {
            RadComboBoxItem cItem;
            foreach (GridDataItem item in dgInst.Items)
            {                
                RadComboBox cmbInst = item.FindControl("radcmbInstCode") as RadComboBox;
                if (cmbInst != null)
                {                    
                    for (int i = 1; i < 6; i++)
                    {
                        cItem = new RadComboBoxItem("Description" + i.ToString(), "Code" + i.ToString());
                        cmbInst.Items.Add(cItem);
  
                    }
                }
            }
        }
  
  
  
protected void btnAddRow_Click(object sender, EventArgs e)
        {
            if (ViewState["Data"] != null)
            {
               DataTable  dt = (DataTable)ViewState["Data"];
               dt = addNewRow(dt);            
               dgInst.Rebind();
                
            }
}

0
Iana Tsolova
Telerik team
answered on 28 Jul 2010, 12:03 PM
Hi Neema,

I used the provided code and prepared the sample project attached to this post. Try it out and let me know if it works as desired or I missed something from your logic out.

Greetings,
Iana
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
0
Mark Mazzitello
Top achievements
Rank 1
answered on 09 Jan 2014, 04:17 PM
This works great, but when a new row is added any selected items in existing rows is lost (all pick lists revert to the first item).  
How can this be fixed?  

(I realize I am re-opening an old thread, but this is exactly what I need except for the existing rows need to retain their data when a new row is added).

Thanks
0
Kostadin
Telerik team
answered on 14 Jan 2014, 12:54 PM
Hi Mark,

A possible solution is to persist the currently selected items from RadComboBox controls and when the grid is rebound to select the same items. This could be achieved by looping through all items and store the values into a List object. After the rebind you have to loop one more time through all items and select item from each RadComboBox. At the following help article could be find detailed information about accessing a controls into a TemplateColumn.

Regards,
Kostadin
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
Arnaud
Top achievements
Rank 1
answered on 05 Aug 2014, 03:36 PM
Hello Neema

What would be the explanation if I add an element to my list in TSession, set again that modified list to my grid's datasource and rebind the grid, but then grid does not show the new row ?
0
Viktor Tachev
Telerik team
answered on 08 Aug 2014, 10:56 AM
Hi Arnaud,

Would you elaborate more on the scenario that you currently have? How are you binding the grid? Is the data for the RadGrid saved in a Session variable? Or only the List object containing the RadComboBox items?

I would appreciate it if you could also provide your code. This way anyone who would like to help would have better understanding of your setup and the goal you would like to achieve.

Regards,
Viktor Tachev
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Arnaud
Top achievements
Rank 1
answered on 08 Aug 2014, 11:31 AM
Hi Viktor

As my issue is not exactly the same as in the original post, I opened a new thread here :
http://www.telerik.com/forums/radgrid-rebind%28%29-does-not-show-new-datasource

and will asap post my code as you required.

Thank you
Arnaud
0
Viktor Tachev
Telerik team
answered on 13 Aug 2014, 07:08 AM
Hi Arnaud,

I have answered your query in the other thread that you have opened. Please refer to it for additional information.


Regards,
Viktor Tachev
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Ajax
Asked by
Neema Sridhar
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
Neema Sridhar
Top achievements
Rank 1
Mark Mazzitello
Top achievements
Rank 1
Kostadin
Telerik team
Arnaud
Top achievements
Rank 1
Viktor Tachev
Telerik team
Share this question
or