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

Many Grid Questions

5 Answers 215 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Brett
Top achievements
Rank 2
Brett asked on 13 Jun 2008, 02:50 PM
I am using the Grid to get data reported back to me  I need to accomplish the following tasks.  I will ayout my issues, please feel to help with any or all of them.

1. Can add multiple records at a time.  If so how?  What I envision, is the the grid loads, you click add new record, fill out the in-line form, click Add New record, and a new row is appended in edit mode still, tehn at teh end a sdingle postback sends all rows to the database.

2. How can I set a GridDateTimeColumn editor to "Today's Date"

3. How can I set a GridDropDownColumn editor to not wrap the options in the drop down list

4.How can I "pre-populate"  a field with the current UserID when the grid is set in "edit" or "Add new record" mode.  I envision something like this:

Dim UserID As String  
If RadGrid1.CurrentMode = RadGridMode.Insert Then  
UserID = Membership.GetUser.UserName.ToString()  
CType(Me.RadGrid1.FindControl("UserIDTextbox"), Text).Text = UserID 
End If 

5. How do I make the value of a field be the default for subsequent added records.  For example, one column contains population.  So after the first row as been added I would like the column to default to the initial value entered in the population field during the same session.

Any help would be great thanks,
Brett

5 Answers, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 16 Jun 2008, 02:03 PM
Hi Bslaght,

1. You can find attached a sample project demonstrating the functionality you requested. InsertCommand places the inserted data into a hashtable and stores in the Session, whereas we have a UpdateAll button in the command item which retrieves the hashtables from the session and you can manually insert them in the data source.

2. For getting the current date in the DateTime editor, you can do the following:

void RadGrid1_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e) 
    GridEditableItem item = (GridEditableItem)e.Item; 
    (item.EditManager.GetColumnEditor("ColumnUniqueName"as GridDateTimeColumnEditor).Text = System.DateTime.Today.ToString(); 

3. To force a GridDropDownListColumnEditor not to wrap, you can set its DropDownStyle.Width to a custom unit in the ItemDataBound event handler:

(item.EditManager.GetColumnEditor("Category"as GridDropDownListColumnEditor).DropDownStyle.Width = Unit.Pixel(80); 


4. To set an initial value to a field in insert mode, you can do the following:

void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridEditFormInsertItem) 
        { 
            GridEditFormInsertItem item = (GridEditFormInsertItem)e.Item; 
            (item.EditManager.GetColumnEditor("ProductID"as GridTextBoxColumnEditor).Text = "102"
        } 
    } 

In the ItemDataBound the EditFormInsertItem is referenced and its respective column editor is set. This is also demonstrated in the attached sample project.

5. This question is closely related to the previous one. Again in the ItemDataBound event you can set values for any of the column editors you have in your grid. You can also store values in the session and place them in the editors.

Greetings,
Veli
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Brett
Top achievements
Rank 2
answered on 24 Jun 2008, 01:12 PM
Hi Veli,
  I have been trying to get some of the code to work but without much luck.  Ihave been using code converter to move it over to VB, but am getting some syntax errors.

Current DateTime: Syntax Error - Line 3
Sub RadGrid1_InsertCommand(ByVal source As ObjectByVal e As Telerik.Web.UI.GridCommandEventArgs)  
    Dim item As GridEditableItem = DirectCast(e.Item, GridEditableItem)  
    (TryCast(item.EditManager.GetColumnEditor("ColumnUniqueName"), GridDateTimeColumnEditor)).Text = System.DateTime.Today.ToString()  
End Sub 

NoWrap Drop Down Values: Syntax Error - Line 2
Protected Sub RadGrid1_ItemDataBound(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound  
        (TryCast(item.EditManager.GetColumnEditor("CauseID"), GridDropDownListColumnEditor)).DropDownStyle.Width = Unit.Pixel(80)  
    End Sub 

Seting Default Values: Syntax Error - Line 4
 Sub RadGrid1_ItemDataBound(ByVal sender As ObjectByVal e As GridItemEventArgs)  
        If TypeOf e.Item Is GridEditFormInsertItem Then 
            Dim item As GridEditFormInsertItem = DirectCast(e.Item, GridEditFormInsertItem)  
        (TryCast(item.EditManager.GetColumnEditor("ProductID"), GridTextBoxColumnEditor)).Text = "102" 
        End If 
    End Sub 


What is teh proper VB method of achiving these tasks?

Thanks again,
Brett
0
Veli
Telerik team
answered on 24 Jun 2008, 04:26 PM
Hi bslaght,

Try the following:

Private Sub RadGrid1_InsertCommand(ByVal source As System.ObjectByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.InsertCommand 
        Dim item As GridEditableItem = DirectCast(e.Item, GridEditableItem) 
        Dim Editor = CType(item.EditManager.GetColumnEditor("ColumnUniqueName"), GridDateTimeColumnEditor) 
        Editor.Text = System.DateTime.Today.ToString() 
    End Sub 

And also:

Protected Sub RadGrid1_ItemDataBound(ByVal source As ObjectByVal e As GridItemEventArgs) Handles RadGrid1.ItemDataBound 
        If TypeOf e.Item Is GridEditFormInsertItem Then 
            Dim item = CType(e.Item, GridEditFormInsertItem) 
            Dim editor = CType(item.EditManager.GetColumnEditor("ProductID"), GridTextBoxColumnEditor) 
            editor.Text = "102" 
        End If 
    End Sub 


Kind regards,
Veli
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Brett
Top achievements
Rank 2
answered on 08 Jul 2008, 06:43 PM
Hi Guys,
 I am sorry to say I am still not havng much luck with examples you have given.  I think I am going to forget about the multiple new records for now and just try and get the features workng.

I have posted my code for vb and aspx, please review and let me know where I have gone wrong.

None of the pre-populated fields are working (UserID/ReportDate) and my dropdown column still is wrapping the text.

Thanks for any insight you can provide.

VB.net

 Protected Sub RadGrid1_InsertCommand(ByVal source As System.ObjectByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.InsertCommand  
        Dim item As GridEditableItem = DirectCast(e.Item, GridEditableItem)  
        Dim Editor = CType(item.EditManager.GetColumnEditor("ReportDate"), GridDateTimeColumnEditor)  
        Editor.Text = System.DateTime.Today.ToString()  
    End Sub 
    Protected Sub RadGrid1_ItemDataBound(ByVal source As ObjectByVal e As GridItemEventArgs) Handles RadGrid1.ItemDataBound  
        If TypeOf e.Item Is GridEditFormInsertItem Then 
            Dim item = CType(e.Item, GridEditFormInsertItem)  
            Dim editor = CType(item.EditManager.GetColumnEditor("UserID"), GridTextBoxColumnEditor)  
            'editor.Text = Membership.GetUser.UserName.ToString()  
            editor.Text = "102" 
            CType(item.EditManager.GetColumnEditor("CauseID"), GridDropDownListColumnEditor).DropDownStyle.Width = Unit.Pixel(80)  
        End If 
    End Sub 

aspx

<

telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="dsSchoolReports"

Skin="WebBlue" GridLines="None" Width="95%" AutoGenerateDeleteColumn="True"

AutoGenerateEditColumn="True" OnItemDataBound="RadGrid1_ItemDataBound" OnInsertCommand="RadGrid1_InsertCommand">

<

MasterTableView autogeneratecolumns="False" datakeynames="id"

datasourceid="dsSchoolReports" AllowAutomaticDeletes="True"

AllowAutomaticInserts="True" AllowAutomaticUpdates="True"

CommandItemDisplay="Bottom" EditMode="InPlace">

<

RowIndicatorColumn Visible="False">

<

HeaderStyle Width="20px"></HeaderStyle>

</

RowIndicatorColumn>

<

ExpandCollapseColumn Visible="False" Resizable="False">

<

HeaderStyle Width="20px"></HeaderStyle>

</

ExpandCollapseColumn>

<Columns>

<telerik:GridBoundColumn DataField="id" DataType="System.Int32" HeaderText="id"

ReadOnly="True" SortExpression="id" UniqueName="id" Visible="False">

</telerik:GridBoundColumn>

<telerik:GridBoundColumn DataField="SchoolID" DataType="System.Int32"

HeaderText="SchoolID" SortExpression="SchoolID" UniqueName="SchoolID">

</telerik:GridBoundColumn>

<telerik:GridDateTimeColumn DataField="ReportDate" DataType="System.DateTime"

HeaderText="ReportDate" SortExpression="ReportDate"

UniqueName="ReportDate"

PickerType="DatePicker"

ColumnEditorID="ReportDate">

</telerik:GridDateTimeColumn>

<telerik:GridBoundColumn DataField="UserID" DataType="System.Int32"

HeaderText="UserID" SortExpression="UserID" UniqueName="UserID"

ColumnEditorID="UserID">

</telerik:GridBoundColumn>

<telerik:GridBoundColumn DataField="StudentsAbsent" DataType="System.Int32"

HeaderText="# Absent" SortExpression="StudentsAbsent"

UniqueName="StudentsAbsent" MaxLength="4">

<HeaderStyle Width="100px" />

<ItemStyle Width="50px" />

</telerik:GridBoundColumn>

<%

-- <telerik:GridBoundColumn DataField="CauseID" DataType="System.Int32"

HeaderText="Cause of Absence" SortExpression="CauseID"

UniqueName="CauseID">

</telerik:GridBoundColumn>

<telerik:GridDropDownColumn DataField="CauseID" DataType="System.Int32"

HeaderText="Cause of Absence" SortExpression="CauseID"

UniqueName="CauseID">

</telerik:GridDropDownColumn>--

%>

<telerik:GridDropDownColumn

UniqueName="CauseID"

ListTextField="CauseName"

ListValueField="id"

DataSourceID="dsCauses"

HeaderText="Cause of Absence"

DataField="CauseID"

DropDownControlType="RadComboBox"

AllowSorting="true"

ColumnEditorID="CauseID">

<ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False"

Font-Strikeout="False" Font-Underline="False" Wrap="False" />

</

telerik:GridDropDownColumn>

<telerik:GridBoundColumn DataField="SchoolPopulation" DataType="System.Int32"

HeaderText="School Population" SortExpression="SchoolPopulation"

UniqueName="SchoolPopulation">

</telerik:GridBoundColumn>

</Columns>

<

EditFormSettings>

<

PopUpSettings ScrollBars="None"></PopUpSettings>

</

EditFormSettings>

</

MasterTableView>

</telerik:RadGrid>



Brett
0
Veli
Telerik team
answered on 09 Jul 2008, 07:55 AM
Hello bslaght,

The sample code you provided seems fine. I wonder why it does not work at your site. If the problem persists, please consider opening a regular support ticket where  you can provide us a sample runnable project, so that we can look into the matter and advise you further.

Regards,
Veli
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
Grid
Asked by
Brett
Top achievements
Rank 2
Answers by
Veli
Telerik team
Brett
Top achievements
Rank 2
Share this question
or