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

Getting values on insert command

14 Answers 805 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mark Ferkaluk
Top achievements
Rank 1
Mark Ferkaluk asked on 29 Aug 2011, 09:29 AM
Hi there,

I'm having trouble to get the insert values of my grid. My grid does on every postback a databind and it seems, that if that is done, the insert values are gone. If I don't databind on every postback, the grid doesn't show up (no columns, no rows).

My code to get the values is this:

Dim insertedItem As GridEditFormInsertItem = TryCast(e.Item, GridEditFormInsertItem)
 
Dim test As Object = TryCast(insertedItem.FormColumns(0).Controls(0).Controls(1).Controls(0), TextBox)

test.Text should contain the value, but it doesn't. Do you have any idea?

Thanks and regards,
Mark

14 Answers, 1 is accepted

Sort by
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 29 Aug 2011, 09:42 AM
Hello,

please use radgrid advanced data binding.

or
......solution for your existing code.

page_load()
{
if (!Page.IsPostBack)
{
 // bind your grid here
}

}

insert_command()
{
 // do your logic here
// bind your grid here
}

 NOTE : bind your grid when it needed not every time..

Thanks,
Jayesh Goyani
0
Princy
Top achievements
Rank 2
answered on 29 Aug 2011, 09:58 AM
Hello Mark,

As Jayesh suggested you can try Advanced databinding to populate RadGrid. Also try the followimg code snippet to get the control in insert form while inserting.

ASPX:
<telerik:GridBoundColumn UniqueName="EmployeeID"  HeaderText="EmployeeID" DataField="EmployeeID">
</telerik:GridBoundColumn>

VB.NET:
Protected Sub RadGrid1_InsertCommand(sender As Object, e As GridCommandEventArgs)
    Dim item As GridEditFormInsertItem = DirectCast(e.Item, GridEditFormInsertItem)
    Dim txt As TextBox = DirectCast(item("EmployeeID").Controls(0), TextBox)
End Sub

Thanks,
Princy.
0
Mark Ferkaluk
Top achievements
Rank 1
answered on 29 Aug 2011, 12:33 PM
Hi Jayesh,

thanks for your reply. The NeedDataSource Event does the trick.

But now I'm having trouble to get the second insertet data  control.
Dim customer As String = (TryCast(insertedItem("Customer").Controls(0), TextBox)).Text
Dim value As String = (TryCast(insertedItem("Value").Controls(0), TextBox)).Text
Dim units As String = (TryCast(insertedItem("Units").Controls(0), TextBox)).Text

The Customer value works, but on the "Value" value an error appears: Object reference not set to an instance of an object.

The Value field is a numeric column. Is that why it doesn't work?

Thanks,
Mark
0
Jayesh Goyani
Top achievements
Rank 2
answered on 30 Aug 2011, 09:59 AM
hello,

Dim value As String = (TryCast(insertedItem("Value").Controls(0), TextBox)).Text
if your value field is numericColumn then...
TryCast(insertedItem("Value").Controls(0), RadNumericTextBox))

let me know if any concern.


Thanks,
Jayesh Goyani
0
Mark Ferkaluk
Top achievements
Rank 1
answered on 30 Aug 2011, 10:41 AM
Hi there,

unfortunately this doesn't work. Either with .Value and .Text. There is no data in it?

Regards,
Mark
0
Jayesh Goyani
Top achievements
Rank 2
answered on 30 Aug 2011, 10:45 AM
Hello,

Are you checked in edit mode or insert mode?

let me know what type of functionality you want.

Thanks,
Jayesh Goyani
0
Mark Ferkaluk
Top achievements
Rank 1
answered on 30 Aug 2011, 10:48 AM
Hi,

I'm currently in insert mode. I have three fields (customer, value and units). Customer is text and value and units are numeric. Getting the customer value works, but both other values not. When I'm debugging there is no data in those RadNumeriTextBoxes.

Regards,
Mark
0
Princy
Top achievements
Rank 2
answered on 30 Aug 2011, 10:58 AM
Hello Mark,

If you are using GridNumericColumn, it is rendered as RadNumericTextBox in insert and edit mode. Here is the sample code that I tried which worked as expected.
VB:
Protected Sub RadGrid1_InsertCommand(sender As Object, e As GridCommandEventArgs)
    Dim item As GridEditFormInsertItem = DirectCast(e.Item, GridEditFormInsertItem)
    Dim txt1 As RadNumericTextBox = DirectCast(item("Value").Controls(0), RadNumericTextBox)
    Dim val As String = txt1.Text
End Sub

Thanks,
Princy.
0
Mark Ferkaluk
Top achievements
Rank 1
answered on 30 Aug 2011, 01:37 PM
Hello Princy,

I tried it that way, but like I said. It seems, that there is no value in it. Here my code again:

Dim insertedItem As GridEditFormInsertItem = TryCast(e.Item, GridEditFormInsertItem)
 
Dim customer As String = (TryCast(insertedItem("Customer").Controls(0), TextBox)).Text
Dim value As Double = (TryCast(insertedItem("Value").Controls(0), RadNumericTextBox)).Value
Dim units As Integer = (TryCast(insertedItem("Units").Controls(0), RadNumericTextBox)).Value


Strange thing is, I can get the customer value.

Regards,
Mark
0
Jayesh Goyani
Top achievements
Rank 2
answered on 30 Aug 2011, 07:16 PM
Hello,

Are you able to get ALL columns(customer,value,Units)'s value or not ?

if no then please check below scenario and if you find this then remove(that binding Datasource code) from your code.
after click on "Insert" Button your grid was binding datasource again before coming in the
insertcomand event.

Thanks,
Jayesh Goyani
0
Mark Ferkaluk
Top achievements
Rank 1
answered on 31 Aug 2011, 08:28 AM
Hello Jayesh,

I'm only able to get the column Customer. When I try to get the values of the others, it is not possible, because there are no values (no .Text and no .Value). But I can access the RadNumericTextBox controls, there is just no value.

After I clicked insert, there is no databind. I use the Event NeedDataSource and it is not called after I click insert.

When I change the RadNumericTextBoxes into normal Bound Textboxes, everything works fine.

Regards.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 31 Aug 2011, 09:40 AM
Hello,

Please send your RadGrid's code so i can check it from my side.

Thanks,
Jayesh Goyani

0
Mark Ferkaluk
Top achievements
Rank 1
answered on 31 Aug 2011, 10:31 AM
Hi,

here is all my code.

ASCX Markup
<telerik:RadGrid ID="RadGrid_customers" runat="server" AutoGenerateColumns="False" AutoGenerateDeleteColumn="True" AutoGenerateEditColumn="True" CellSpacing="0"
                                GridLines="Horizontal" Skin="Telerik" Width="450px" Culture="de-DE">
                                <MasterTableView CommandItemDisplay="Top"
                                    CommandItemSettings-AddNewRecordText="Add new customer">
                                    <commanditemsettings addnewrecordtext="Add new customer"
                                        exporttopdftext="Export to PDF" />
                                    <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
                                        <HeaderStyle Width="20px" />
                                    </RowIndicatorColumn>
                                    <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
                                        <HeaderStyle Width="20px" />
                                    </ExpandCollapseColumn>
                                    <Columns>
                                        <telerik:GridBoundColumn DataField="customer"
                                            FilterControlAltText="Filter Customer column" HeaderText="Customer"
                                            UniqueName="Customer">
                                        </telerik:GridBoundColumn>
                                        <telerik:GridNumericColumn DataField="value" DataType="System.Decimal"
                                            DecimalDigits="2" FilterControlAltText="Filter Value column"
                                            HeaderText="Value 2010" UniqueName="Value">
                                            <HeaderStyle HorizontalAlign="Right" />
                                            <ItemStyle HorizontalAlign="Right" />
                                        </telerik:GridNumericColumn>
                                        <telerik:GridNumericColumn DataField="units" DataType="System.Int64"
                                            DecimalDigits="0" FilterControlAltText="Filter Units column"
                                            HeaderText="Units 2010" UniqueName="Units">
                                            <HeaderStyle HorizontalAlign="Right" />
                                            <ItemStyle HorizontalAlign="Right" />
                                        </telerik:GridNumericColumn>
                                        <telerik:GridNumericColumn DataField="id" DataType="System.Int64"
                                            DecimalDigits="0" FilterControlAltText="Filter ID column" HeaderText="ID"
                                            ReadOnly="True" UniqueName="ID" Visible="False">
                                        </telerik:GridNumericColumn>
                                    </Columns>
                                    <EditFormSettings>
                                        <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                                        </EditColumn>
                                    </EditFormSettings>
                                </MasterTableView>
                                <FilterMenu EnableImageSprites="False">
                                </FilterMenu>
                                <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default">
                                </HeaderContextMenu>
                            </telerik:RadGrid>


InsertCommand Event
Protected Sub RadGrid_customers_InsertCommand(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid_customers.InsertCommand
        Dim topicnav As RadPanelItem = DirectCast(topic_navigation_cc.SelectedItem, RadPanelItem)
        Dim insertedItem As GridEditFormInsertItem = TryCast(e.Item, GridEditFormInsertItem)
 
        Dim customer As String = (TryCast(insertedItem("Customer").Controls(0), TextBox)).Text
        Dim value As String = (TryCast(insertedItem("Value").Controls(0), RadNumericTextBox)).Text
        Dim units As String = (TryCast(insertedItem("Units").Controls(0), RadNumericTextBox)).Text
End Sub

NeedDataSource Event
Protected Sub RadGrid_customers_NeedDataSource(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid_customers.NeedDataSource
       Dim table_customers_work As DataTable
       Dim table_version As DataTable = getSQLData("SELECT years_cc FROM versions WHERE version = 'test'")
 
       table_customers_work = getSQLData("SELECT customer, value, units, id FROM customer_work WHERE topic_id = 1 AND assigned_version = 'test' AND country = 'test'")
 
       RadGrid_customers.DataSource = table_customers_work
 
       RadGrid_customers.MasterTableView.Columns(1).HeaderText = "Value " & table_version.Rows(0)(0).ToString
       RadGrid_customers.MasterTableView.Columns(2).HeaderText = "Units " & table_version.Rows(0)(0).ToString
   End Sub

Regards,
Mark
0
Jayesh Goyani
Top achievements
Rank 2
answered on 31 Aug 2011, 11:53 AM
Hello,

i am successfully get values of column in your code.

Please create one page and put my code in to page and check its working or not?

<telerik:RadGrid ID="RadGrid_customers" runat="server" AutoGenerateColumns="False" AutoGenerateDeleteColumn="True" AutoGenerateEditColumn="True" CellSpacing="0"
                              GridLines="Horizontal" Skin="Telerik" Width="450px" Culture="de-DE">
                              <MasterTableView CommandItemDisplay="Top"
                                  CommandItemSettings-AddNewRecordText="Add new customer">
                                  <commanditemsettings addnewrecordtext="Add new customer"
                                      exporttopdftext="Export to PDF" />
                                  <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
                                      <HeaderStyle Width="20px" />
                                  </RowIndicatorColumn>
                                  <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
                                      <HeaderStyle Width="20px" />
                                  </ExpandCollapseColumn>
                                  <Columns>
                                      <telerik:GridBoundColumn DataField="customer"
                                          FilterControlAltText="Filter Customer column" HeaderText="Customer"
                                          UniqueName="Customer">
                                      </telerik:GridBoundColumn>
                                      <telerik:GridNumericColumn DataField="value" DataType="System.Decimal"
                                          DecimalDigits="2" FilterControlAltText="Filter Value column"
                                          HeaderText="Value 2010" UniqueName="Value">
                                          <HeaderStyle HorizontalAlign="Right" />
                                          <ItemStyle HorizontalAlign="Right" />
                                      </telerik:GridNumericColumn>
                                      <telerik:GridNumericColumn DataField="units" DataType="System.Int64"
                                          DecimalDigits="0" FilterControlAltText="Filter Units column"
                                          HeaderText="Units 2010" UniqueName="Units">
                                          <HeaderStyle HorizontalAlign="Right" />
                                          <ItemStyle HorizontalAlign="Right" />
                                      </telerik:GridNumericColumn>
                                      <telerik:GridNumericColumn DataField="id" DataType="System.Int64"
                                          DecimalDigits="0" FilterControlAltText="Filter ID column" HeaderText="ID"
                                          ReadOnly="True" UniqueName="ID" Visible="False">
                                      </telerik:GridNumericColumn>
                                  </Columns>
                                  <EditFormSettings>
                                      <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                                      </EditColumn>
                                  </EditFormSettings>
                              </MasterTableView>
                              <FilterMenu EnableImageSprites="False">
                              </FilterMenu>
                              <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default">
                              </HeaderContextMenu>
                          </telerik:RadGrid>
Imports System.Data
Imports Telerik.Web.UI
 
 
 
Partial Class Default5
    Inherits System.Web.UI.Page
 
    Protected Sub RadGrid_customers_NeedDataSource(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid_customers.NeedDataSource
 
        RadGrid_customers.DataSource = New DataTable
 
        RadGrid_customers.MasterTableView.Columns(1).HeaderText = "Value " & "123"
        RadGrid_customers.MasterTableView.Columns(2).HeaderText = "Units " & "456"
    End Sub
 
 
    Protected Sub RadGrid_customers_InsertCommand(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid_customers.InsertCommand
 
        Dim insertedItem As GridEditFormInsertItem = TryCast(e.Item, GridEditFormInsertItem)
 
        Dim customer As String = (TryCast(insertedItem("Customer").Controls(0), TextBox)).Text
        Dim value As String = (TryCast(insertedItem("Value").Controls(0), RadNumericTextBox)).Text
        Dim units As String = (TryCast(insertedItem("Units").Controls(0), RadNumericTextBox)).Text
    End Sub
 
 
End Class



Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Mark Ferkaluk
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Princy
Top achievements
Rank 2
Mark Ferkaluk
Top achievements
Rank 1
Share this question
or