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

Radgrid as a datagrid

7 Answers 116 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Myriam
Top achievements
Rank 1
Myriam asked on 18 Aug 2009, 12:37 PM
Hello
I would like to have a Radgrid which allow to add new rows as datagrid.  I would like something like that

1

Test1

TesT1

Test2

2

Test2

Test3

Test4

*

 

 

 

And if I add a new record where the * is...I would like a new line with the * appear. Is it possible to do something like that?

I don't want to set the number of row. I want the user can enter as much as he wants
Thank you

Myriam

7 Answers, 1 is accepted

Sort by
0
Schlurk
Top achievements
Rank 2
answered on 18 Aug 2009, 04:41 PM
From what I understand of what you are looking for you could have everything be taken care of client-side, and then when the user is done just do a rebind() to the server. I'd check out the following demos:
http://demos.telerik.com/aspnet-ajax/grid/examples/client/insertupdatedelete/defaultcs.aspx
http://demos.telerik.com/aspnet-ajax/grid/examples/client/clientsideevents/defaultcs.aspx
This way you wouldn't have to worry about adding a set amount of rows before the user starts adding/removing items etc.





0
Myriam
Top achievements
Rank 1
answered on 18 Aug 2009, 05:18 PM
Hmm doesn't look how I want to.
I want to be able to add record one just after the other to the grid and then next, inserted all of it. I know how to passed all rows and insert them in my database. I just want to add a new row to my grid when the last one is full... I don't want to set the rows number value as I want my user be able to add as much as he wants. I don't know if I'm clear enought..
0
Schlurk
Top achievements
Rank 2
answered on 18 Aug 2009, 07:15 PM
Ah yes, it seems like I misunderstood what you were looking for. Although I don't have any example of this but perhaps upon editing an item that happens to be the last one (so for example, you edit the * row and it's the last one) you also couple that with an entry that is formatted the way you want it. In other words, after the user edits and confirms his/her changes when you are adding the information to the grid you also run the same add-methods however this entry only contains the *. Just to make sure I'm making sense here's a list of what I was thinking of:
- User edits a row - check if it is the last one
   * If not - you don't need to do anything special
   * If it is - continue
- Upon confirming the new information do all the necessary steps for an entry to be inserted
- In the same method have a call to a function that does the same steps, only inserts a * into the first field and the rest are blank.
Like I said, it's only a thought and I don't have any code to support it right now but it's an idea! :D
0
Myriam
Top achievements
Rank 1
answered on 18 Aug 2009, 08:17 PM
Maybe I don't explain it correctly
here is an example of what I would like to do instead with a Radgrid..
http://www.aspsnippets.com/post/2009/06/10/Adding-Dynamic-Rows-in-ASPNet-GridView-Control-with-TextBoxes.aspx
0
Princy
Top achievements
Rank 2
answered on 19 Aug 2009, 07:06 AM
Hello Myriam,

You can create a new DataTable and add columns to it in the PageLoad event. Then in the NeedDataSource event of the grid add rows to the DataTable and save it in a session variable which should be bound to the grid. Then on button click you can add more rows to the DataTable and rebind the grid to the new DataTable. The following code should help you understand better :
aspx:
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Add Row" /> 
 
<telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource"
      <MasterTableView> 
             <Columns>                    
                  <telerik:GridTemplateColumn UniqueName="TemplateColumn1"
                        <ItemTemplate> 
                            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
                         </ItemTemplate> 
                  </telerik:GridTemplateColumn> 
                  <telerik:GridTemplateColumn UniqueName="TemplateColumn2"
                         <ItemTemplate> 
                            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> 
                         </ItemTemplate> 
                  </telerik:GridTemplateColumn> 
             </Columns> 
       </MasterTableView> 
</telerik:RadGrid> 

cs:
 
  public DataTable dt;  
        protected void Page_Load(object sender, EventArgs e)  
        {  
            dt= new DataTable();  
            dt.Columns.Add(new DataColumn("RowNumber"typeof(string)));      
  
        }  
  
        protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)  
        {  
            if (!IsPostBack)  
            {                
                dt=  AddRow(dt);  // call the method to create row 
                ViewState["dt"] = dt;  
            }  
            dt = (DataTable)ViewState["dt"];  
            RadGrid1.DataSource = dt;  
        }  
  
        private DataTable AddRow(DataTable dt) 
        { // method to create row 
            DataRow dr = dt.NewRow();  
            dr["RowNumber"] = ""
            
            dt.Rows.Add(dr);  
            return dt;  
        }  
  
        protected void Button1_Click(object sender, EventArgs e)  
        {  
            DataTable dt = (DataTable)ViewState["dt"];  
            ViewState["dt"] = AddRow(dt);  
            RadGrid1.Rebind();  
                         
        }  
 

Hope this helps... :)
Princy.
0
Myriam
Top achievements
Rank 1
answered on 19 Aug 2009, 01:49 PM
Yes this is a good one.
But does it have a way to keep the values the user already entered before to hit the Add new row button?
For example if he enter 6 records and realize he need to add 7 values, he hit the add new row value and then lose all the values already entered?
Thank you
Myriam
0
Myriam
Top achievements
Rank 1
answered on 19 Aug 2009, 07:01 PM
Here is my code
This is working as I want now!
Thank you very much!

Protected Sub RadGrid2_NeedDataSource(ByVal source As ObjectByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs)  
        If Not IsPostBack Then 
            dt = AddRow(dt)  
            ' call the method to create row   
            ViewState("dt") = dt  
        End If 
        dt = DirectCast(ViewState("dt"), DataTable)  
        RadGrid2.DataSource = dt  
    End Sub 
 
    Private Function AddRow(ByVal dt As DataTable) As DataTable  
        ' method to create row   
        Dim dr As DataRow = dt.NewRow()  
        dr("RowNumber") = "" 
        dt.Rows.Add(dr)  
        Return dt  
    End Function 
 
    Protected Sub Button1_Click(ByVal sender As ObjectByVal e As EventArgs)  
        Dim countitem As Integer 
        Dim dt As DataTable = DirectCast(ViewState("dt"), DataTable)  
 
        countitem = RadGrid2.Items.Count  
        Dim cpt As Integer = 0  
        For Each Item As GridDataItem In RadGrid2.Items  
 
            Dim txtcode As TextBox = DirectCast(Item("CodePermanent").FindControl("TxtCodePermanent"), TextBox)  
            If Not (txtcode.Text Is NothingAnd txtcode.Text <> "" Then 
                liste.Add(txtcode.Text)  
 
            End If 
 
        Next 
        ViewState("dt") = AddRow(dt)  
        listcount = liste.Count  
 
        RadGrid2.Rebind()  
        While cpt < listcount  
            Dim txtcode As TextBox = DirectCast(RadGrid2.Items(cpt)("CodePermanent").FindControl("TxtCodePermanent"), TextBox)  
            txtcode.Text = liste.Item(cpt)  
            cpt = cpt + 1  
        End While 
    End Sub 
 
    Public Sub RadGrid2_ItemCommand(ByVal source As ObjectByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid2.ItemCommand  
        If e.CommandName = "UpdateChanges" Then 
            For Each Item As GridDataItem In RadGrid2.Items  
                Dim txtcode As TextBox = DirectCast(Item("CodePermanent").FindControl("TxtCodePermanent"), TextBox)  
                If Not (txtcode.Text Is NothingThen 
                    ImporterEtudiants(txtcode.Text)  
 
                End If 
            Next 
           End If 
    End Sub 
Tags
Grid
Asked by
Myriam
Top achievements
Rank 1
Answers by
Schlurk
Top achievements
Rank 2
Myriam
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or