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

Manual Insert and Update Error

3 Answers 25 Views
Grid
This is a migrated thread and some comments may be shown as answers.
L
Top achievements
Rank 1
L asked on 26 Nov 2013, 09:19 AM
hi

I faced an error that it cannot Cannot find a cell bound to column name 'CustTextBox'  How should I solve this? Thanks a lot

here is my code:

 <telerik:GridTemplateColumn DataField="CustName"
            FilterControlAltText="Filter CustName column" HeaderText="Customer Name"
            SortExpression="CustName" UniqueName="CustName">
            <EditItemTemplate>
                <asp:TextBox ID="CustNameTextBox" runat="server"
                    Text='<%# Bind("CustName") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="SCustNameLabel" runat="server" Text='<%# Eval("CustName") %>'></asp:Label>
            </ItemTemplate>
</telerik:GridTemplateColumn>

    Protected Sub RadGrid1_InsertCommand(sender As Object, e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.InsertCommand
        Dim insertedItem As GridEditFormInsertItem = DirectCast(e.Item, GridEditFormInsertItem)
        Dim CustName As String = (TryCast(insertedItem("CustNameTextBox").Controls(0), TextBox)).Text
        Customer_Insert(CustName)
        RadGrid1.Rebind()
    End Sub

Protected Sub RadGrid1_UpdateCommand(sender As Object, e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.UpdateCommand
        Dim editedItem As GridEditableItem = TryCast(e.Item, GridEditableItem)
        Dim CustID As String = editedItem.OwnerTableView.DataKeyValues(editedItem.ItemIndex)("CustID").ToString()
        Dim CustName As String = (TryCast(editedItem("CustNameTextBox").Controls(0), TextBox)).Text
        Customer_Update(New Guid(CustID), CustName)
        RadGrid1.Rebind()
    End Sub

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 26 Nov 2013, 09:55 AM
Hi,

You are getting such an error because the way you have accessed the TextBox in the GridTemplateColumn is not correct. For accessing a Control inside GridTemplateColumn please try the following code snippet.

VB:
Dim txt As TextBox = DirectCast(editedItem.FindControl("CustNameTextBox"), TextBox)
Dim CustName As String = txt.Text

Thanks,
Princy

0
L
Top achievements
Rank 1
answered on 26 Nov 2013, 01:43 PM
Thanks Princy

Now I face invalid columns when try to delete. Also how do I refresh the grid after insert, Update and delete? It seems that I did a radgrid1.rebind for every Insert, update but the grid just goes blank and I need to refresh the browser to get the grid databind. Here is my code. Thanks a lot.
Invalid column name 'CustID'.

<MasterTableView AutoGenerateColumns="False" DataKeyNames="CustID" CommandItemDisplay="Top">
<CommandItemSettings ExportToPdfText="Export to PDF"
        AddNewRecordText="New Record"></CommandItemSettings>

<RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>

<ExpandCollapseColumn Visible="True"
        FilterControlAltText="Filter ExpandColumn column" Created="True">
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>

    <Columns>
        <telerik:GridBoundColumn DataField="CustID" DataType="System.Guid"
            FilterControlAltText="Filter CustID column" HeaderText="CustID"
            ReadOnly="True" SortExpression="CustID" Visible="false" UniqueName="CustID">
        </telerik:GridBoundColumn>

 

Protected Sub RadGrid1_DeleteCommand(sender As Object, e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.DeleteCommand
        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
        Dim CustID As String = e.Item.OwnerTableView.DataKeyValues(item.ItemIndex)("CustID").ToString()
        Customer_Delete(New Guid(CustID))
        RadGrid1.Rebind()
End Sub

0
Accepted
Princy
Top achievements
Rank 2
answered on 27 Nov 2013, 04:22 AM
Hi ,

You can delete a column using the DataKeyValue as shown below. In your code I see that you have set the Visibility of CustID to false and it is in readonly mode. When you specify a primary key field by adding it to the DataKeyNames array, the value of the specified column becomes available in DataKeyValues array, hence we donot have to add it as BoundColumn if you don't want to display it. In case you are using this column, please set Display as false, and not Visible="false", because setting visibility will not give its access in the code behind.
Then you don't have to call Rebind() after the Insert/Update/Delete operation, the Grid automatically refresh after each operation.

C#:
protected void RadGrid1_DeleteCommand(object sender, GridCommandEventArgs e)
 {
  GridDataItem item = (GridDataItem)e.Item;
  string CustID = item.GetDataKeyValue("CustID").ToString();
 // Code to Delete
 }

Hope this helps, please let me know if any concern.
Thanks,
Princy
Tags
Grid
Asked by
L
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
L
Top achievements
Rank 1
Share this question
or