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

Radgrid Edit and Insert with different number of columns

4 Answers 350 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Inayat Rasool Syed
Top achievements
Rank 1
Inayat Rasool Syed asked on 13 Dec 2012, 08:51 PM
I tried to build a grid which has selected 10 columns  out of which 3 are readonly.

I should be able to insert by selecting "Add new item"  into only 4 columns and when it comes to edit

it should edit 6 columns.

I used this URL to start working
http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/templateformupdate/defaultcs.aspx

Grid doesn't compile if I use less fields in insert than edit in SQL Datasource.

There is a also a problem to hide and print  grid. One another problem about displaying grid rows in Red if there is a negative value in row.
Please help me and suggest some examples with the above requirement.

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 14 Dec 2012, 04:30 AM
Hi,

I am not sure about your requirement. You can set the column visibility while in edit mode and insert mode (for EditFormType="Template") as follows.

ASPX:
<%--here the textbox control is showing in insert mode not in edit mode --%>
<asp:TextBox ID="TextBox1" Text='<%# Bind("Notes") %>' runat="server" TextMode="MultiLine"
    Rows="5" Columns="40" TabIndex="5" Visible='<%#(Container is GridEditFormInsertItem)? true : false %>'>
</asp:TextBox>

2.For displaying grid rows in Red if there is a negative value in row.

C#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem ditem = (GridDataItem)e.Item;
        if (ditem["UniqueName"].Text.StartsWith("-"))
        {
            ditem.BackColor = Color.Red;
        }
    }
}

Please elaborate your scenario if it doesn't help.

Thanks,
Shinu.


0
Inayat Rasool Syed
Top achievements
Rank 1
answered on 14 Dec 2012, 03:52 PM
As I said, I am following this example

http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/templateformupdate/defaultcs.aspx

I attached the screen shots of code and screen. I deal with Null values.

I want to be able to do these
1) During Insert, only insert into location, product,ACount,SelectBCount and SelectCCount ( I see your email to set visibility to false). I added the small code to intialize checkboxes to false as I was getting cast error.

ERROR: If I insert and try to edit the same row I get the same cast error on checkboxes.

2) During Edit, all fields can be used. This works fine.
3) I want the SelectBCount and SelectCCount gets autoselected if the Variance in ACount-Book or BCount-Book is NOT equal to ZERO.I should also be able to edit those checkboxes later.
I don't know how to do this.
4) Can I group by total variances on each product group?


Please let me know on this. Thanks a lot in advance.
0
Inayat Rasool Syed
Top achievements
Rank 1
answered on 14 Dec 2012, 04:00 PM
protected void RadGrid1_SelectedIndexChanged(object sender, EventArgs e)
  {
 
  }
  private void DisplayMessage(bool isError, string text)
  {
      Label label = (isError) ? this.Label1 : this.Label2;
      label.Text = text;
  }
 
  protected void RadGrid1_ItemInserted(object sender, GridInsertedEventArgs e)
  {
      if (e.Exception != null)
      {
          e.ExceptionHandled = true;
          e.KeepInInsertMode = true;
          DisplayMessage(true, "TicketNumber cannot be inserted. Reason: " + e.Exception.Message);
      }
      else
      {
          DisplayMessage(false, "TicketNumber inserted");
 
      }
  }
 
  protected void RadGrid1_ItemUpdated(object sender, GridUpdatedEventArgs e)
  {
      if (e.Exception != null)
      {
          e.KeepInEditMode = true;
          e.ExceptionHandled = true;
          DisplayMessage(true, "TicketNumber " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["TicketNumber"] + " cannot be updated. Reason: " + e.Exception.Message);
      }
      else
      {
          DisplayMessage(false, "TicketNumber " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["TicketNumber"] + " updated");
      }
 
  }
 
 
  protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
  {
      if (e.CommandName == RadGrid.InitInsertCommandName) //"Add new" button clicked
      {
          e.Canceled = true;
          //Prepare an IDictionary with the predefined Values.
          System.Collections.Specialized.ListDictionary newValues = new System.Collections.Specialized.ListDictionary();
          //Set Initial values for check box on Init insert
          newValues["SelectBCount"] = false;
          newValues["SelectCCount"] = false;
          //Insert the item and rebind.
          e.Item.OwnerTableView.InsertItem(newValues);
          GridEditCommandColumn editColumn = (GridEditCommandColumn)RadGrid1.MasterTableView.GetColumn("EditCommandColumn");
          editColumn.Visible = false;
      }
      else if (e.CommandName == RadGrid.RebindGridCommandName && e.Item.OwnerTableView.IsItemInserted)
      {
          e.Canceled = true;
      }
      else
      {
          GridEditCommandColumn editColumn = (GridEditCommandColumn)RadGrid1.MasterTableView.GetColumn("EditCommandColumn");
          if (!editColumn.Visible)
              editColumn.Visible = true;
      }
 
  }
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DefaultGrid.aspx.cs" Inherits="DefaultGrid" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<head runat="server">
    <title></title>
    <telerik:RadStyleSheetManager id="RadStyleSheetManager1" runat="server" />
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        <Scripts>
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" />
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js" />
        </Scripts>
    </telerik:RadScriptManager>
     <p id="divMsgs" runat="server">
        <asp:Label ID="Label1" runat="server" EnableViewState="False" Font-Bold="True" ForeColor="#FF8080"></asp:Label>
        <asp:Label ID="Label2" runat="server" EnableViewState="False" Font-Bold="True" ForeColor="#00C000"></asp:Label>
    </p>
    <p runat="server">
        <asp:CheckBox ID="ChkVarianceA" runat="server" Text="VarianceBookVsA" />
        
        <asp:CheckBox ID="ChkVarianceB" runat="server" Text="VarianceBookVsB" />
         
        <asp:CheckBox ID="ChkVarianceC" runat="server" Text="VarianceBookVsC" />
    </p>
    <p runat="server">
        <asp:CheckBox ID="ChkA" runat="server" Text="Hide ACount" />
                 
        <asp:CheckBox ID="CheckHideB" runat="server" Text="Hide BCount" />
                 
        <asp:CheckBox ID="ChkC" runat="server" Text="Hide CCount" />
    </p>
    <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">       
    <script type="text/javascript">
        //Put your JavaScript code here.      
            function RowDblClick(sender, eventArgs) {
                sender.get_masterTableView().editItem(eventArgs.get_itemIndexHierarchical());
            }
        </script>
 </telerik:RadCodeBlock>
 
   
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
     <AjaxSettings>         
            <telerik:AjaxSetting AjaxControlID="RadGrid1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1">
                    </telerik:AjaxUpdatedControl>
                    <telerik:AjaxUpdatedControl ControlID="divMsgs"></telerik:AjaxUpdatedControl>
                    <telerik:AjaxUpdatedControl ControlID="Label3"></telerik:AjaxUpdatedControl>
                    <telerik:AjaxUpdatedControl ControlID="Label4"></telerik:AjaxUpdatedControl>
 
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server">
    </telerik:RadAjaxLoadingPanel>
 
     <div>    
       <asp:Label ID="Label3" runat="server" EnableViewState="False" Font-Bold="True" ForeColor="#FF8080"></asp:Label>
        <asp:Label ID="Label4" runat="server" EnableViewState="False" Font-Bold="True" ForeColor="#00C000"></asp:Label>
    </div>
 
    <div>
        <telerik:RadGrid ID="RadGrid1" runat="server" CssClass="RadGrid" GridLines="None"
        AllowPaging="True" PageSize="50" AllowSorting="True" AutoGenerateColumns="False"
        ShowStatusBar="True" AllowAutomaticDeletes="True" AllowAutomaticInserts="True"
        AllowAutomaticUpdates="True" DataSourceID="SqlDataSource1"
        OnItemInserted="RadGrid1_ItemInserted" OnItemUpdated="RadGrid1_ItemUpdated" OnItemCommand="RadGrid1_ItemCommand"
        OnPreRender="RadGrid1_PreRender" CellSpacing="0" AllowFilteringByColumn="True"
            ShowFooter="True" ShowGroupPanel="True" Skin="Outlook"
            onitemdatabound="RadGrid1_ItemDataBound">
      
      
            
<MasterTableView  CommandItemDisplay="Top"               
                DataSourceID="SqlDataSource1"
                ShowGroupFooter="True" AllowAutomaticDeletes="False"
                DataKeyNames="TicketNumber" >
 
 
 
 
<CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
 
<RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column"></RowIndicatorColumn>
 
<ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column"></ExpandCollapseColumn>
                <Columns>
 
                    <telerik:GridEditCommandColumn FilterControlAltText="Filter EditCommandColumn column">
                    </telerik:GridEditCommandColumn>
 
                    <telerik:GridBoundColumn DataField="TicketNumber" DataType="System.Int32"
                        FilterControlAltText="Filter TicketNumber column" HeaderText="TicketNumber"
                        ReadOnly="True" SortExpression="TicketNumber" UniqueName="TicketNumber">
                        
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Whse"
                        FilterControlAltText="Filter Whse column" HeaderText="Whse"
                        SortExpression="Whse" UniqueName="Whse">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Customer"
                        FilterControlAltText="Filter Customer column" HeaderText="Customer"
                        SortExpression="Customer" UniqueName="Customer">
                         
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Location"
                        FilterControlAltText="Filter Location column" HeaderText="Location"
                        SortExpression="Location" UniqueName="Location">
                         
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Product"
                        FilterControlAltText="Filter Product column" HeaderText="Product"
                        SortExpression="Product" UniqueName="Product">
                         
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Sku"
                        FilterControlAltText="Filter Sku column" HeaderText="Sku"
                        SortExpression="Sku" UniqueName="Sku">
                          
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="BookValue" DataType="System.Int64"
                        FilterControlAltText="Filter BookValue column" HeaderText="BookValue"
                        SortExpression="BookValue" UniqueName="BookValue">
                         
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="ACount" DataType="System.Int64"
                        FilterControlAltText="Filter ACount column" HeaderText="ACount"
                        SortExpression="ACount" UniqueName="ACount">
                        
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="BCount" DataType="System.Int64"
                        FilterControlAltText="Filter BCount column" HeaderText="BCount"
                        SortExpression="BCount" UniqueName="BCount">
                         
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="CCount" DataType="System.Int64"
                        FilterControlAltText="Filter CCount column" HeaderText="CCount"
                        SortExpression="CCount" UniqueName="CCount">
                          
                    </telerik:GridBoundColumn>
                    <telerik:GridCheckBoxColumn DataField="SelectBCount" DataType="System.Boolean"
                        FilterControlAltText="Filter SelectBCount column" HeaderText="SelectBCount"
                        SortExpression="SelectBCount" UniqueName="SelectBCount">
                    </telerik:GridCheckBoxColumn>
                    <telerik:GridCheckBoxColumn DataField="SelectCCount" DataType="System.Boolean"
                        FilterControlAltText="Filter SelectCCount column" HeaderText="SelectCCount"
                        SortExpression="SelectCCount" UniqueName="SelectCCount">
                    </telerik:GridCheckBoxColumn>
                    <telerik:GridBoundColumn DataField="VarianceABook" DataType="System.Int64"
                        FilterControlAltText="Filter VarianceABook column" HeaderText="VarianceABook"
                        ReadOnly="True" SortExpression="VarianceABook" UniqueName="VarianceABook">
                         
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="VarianceBBook" DataType="System.Int64"
                        FilterControlAltText="Filter VarianceBBook column" HeaderText="VarianceBBook"
                        ReadOnly="True" SortExpression="VarianceBBook" UniqueName="VarianceBBook">
                       
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="VarianceCBook" DataType="System.Int64"
                        FilterControlAltText="Filter VarianceCBook column" HeaderText="VarianceCBook"
                        ReadOnly="True" SortExpression="VarianceCBook" UniqueName="VarianceCBook">
                       
                    </telerik:GridBoundColumn>
                      
 
                    <telerik:GridBoundColumn DataField="VarianceBA" DataType="System.Int64"
                        FilterControlAltText="Filter VarianceBA column" HeaderText="VarianceBA"
                        ReadOnly="True" SortExpression="VarianceBA" UniqueName="VarianceBA">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="VarianceCB" DataType="System.Int64"
                        FilterControlAltText="Filter VarianceCB column" HeaderText="VarianceCB"
                        ReadOnly="True" SortExpression="VarianceCB" UniqueName="VarianceCB">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="VarianceCA" DataType="System.Int64"
                        FilterControlAltText="Filter VarianceCA column" HeaderText="VarianceCA"
                        ReadOnly="True" SortExpression="VarianceCA" UniqueName="VarianceCA">
                    </telerik:GridBoundColumn>
                      
 
                </Columns>
                
 
                 <EditFormSettings EditFormType="Template">
 
<EditColumn UniqueName="EditCommandColumn1" FilterControlAltText="Filter EditCommandColumn1 column"></EditColumn>
 
                <FormTemplate>
                  
      <table id="Table2" cellspacing="2" cellpadding="1" width="100%" border="0" rules="none"
                        style="border-collapse: collapse; background: white;">
                        <tr class="EditFormHeader">
                            <td style="font-size: small">
                                <b>Ticket Details</b>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <table id="Table3" cellspacing="1" cellpadding="1" width="250" border="0" class="module">
                                    <tr>
                                        <td>
                                            Location:
                                        </td>
                                        <td>
                                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Location") %>' TabIndex="1" >
                                           </asp:TextBox>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Product:
                                        </td>
                                        <td>
                                            <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Product") %>' TabIndex="2" >
                                           </asp:TextBox>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Acount:
                                        </td>
                                        <td>
                                            <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("ACount") %>' TabIndex="3" >
                                           </asp:TextBox>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            BCount:</td>
                                        <td>
                                             <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("BCount") %>' TabIndex="4" >
                                            </asp:TextBox>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            CCount:
                                        </td>
                                        <td>
                                             <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("CCount") %>' TabIndex="5" >
                                           </asp:TextBox>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            SelectBCount:
                                        </td>
                                        <td>
                                           <asp:CheckBox ID="CheckBox1" runat="server"  Checked ='<%# Bind("SelectBCount") %>'  />
                                           
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            SelectCCount:</td>
                                        <td>
                                        <asp:CheckBox ID="CheckBox2" runat="server"  Checked ='<%# Bind("SelectCCount") %>'  />
                                                      
                                            </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr>
                            <td align="left">
                                <asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
                                    runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'>
                                </asp:Button
 <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
 CommandName="Cancel"></asp:Button>
                            </td>
                        </tr>
                    </table>
                </FormTemplate>
            </EditFormSettings>
        </MasterTableView>
        <ClientSettings AllowDragToGroup="True" AllowColumnsReorder="True">
            <ClientEvents OnRowDblClick="RowDblClick"></ClientEvents>
            <Scrolling AllowScroll="True" UseStaticHeaders="True" ScrollHeight="500px" />
        </ClientSettings>
 
 
 
<FilterMenu EnableImageSprites="False"></FilterMenu>
 
 
 
        </telerik:RadGrid>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:PhysicalInventoryGridConnectionString %>"
            SelectCommand="SELECT TicketNumber, Whse, Customer, Location, Product, Sku, BookValue, ACount, BCount, CCount,
 
SelectBCount,
 
SelectCCount,
 
 ACount - BookValue AS VarianceABook, BCount - BookValue AS VarianceBBook, CCount - BookValue AS VarianceCBook, BCount - ACount AS VarianceBA, CCount - BCount AS VarianceCB, CCount - ACount AS VarianceCA FROM UT_PhysicalInventory"
             
             
             
            UpdateCommand="UPDATE UT_PhysicalInventory SET Location = COALESCE (@Loc, Location), Product = COALESCE (@Prod, Product), ACount = COALESCE (@ACount, ACount), BCount = COALESCE (@BCount, BCount), CCount = COALESCE (@CCount, CCount) WHERE (TicketNumber = @TicketNumber)"
             
             
             
             
             
            InsertCommand="INSERT INTO UT_PhysicalInventory(Location, Product, BookValue, ACount) VALUES (@location, @product , 0, @Acount)">
            <InsertParameters>
                <asp:Parameter Name="location" />
                <asp:Parameter Name="product" />
                <asp:Parameter Name="Acount" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="Loc" />
                <asp:Parameter Name="Prod" />
                <asp:Parameter Name="ACount" />
                <asp:Parameter Name="BCount" />
                <asp:Parameter Name="CCount" />
                <asp:Parameter Name="TicketNumber" />
            </UpdateParameters>
        </asp:SqlDataSource>
 
    </div>
 
 
    <div>
     
     
     
    </div>
    </form>
</body>
</html>
0
Angel Petrov
Telerik team
answered on 18 Dec 2012, 04:08 PM
Hello Inayat,

Based on your code it is hard to determine the cause of the issue. Could you elaborate more on where the cast error is thrown? Please provide a step-by-step explanation on how to reproduce the problem, what is the input and what should be the output.
Problem number three can be resolved by:
  1. Intercepting the OnItemDataBound event
  2. Check the values of the variance
  3. If the values is not equal to zero find the control using findControl and set Checked=true
The final problem that you are experiencing is difficult to understand. Please clarify what does total variances mean. I suggest that you go through the following help articles which will help you understand RadGrid's grouping:
In order to further investigate we will need a sample project successfully replicating the issue. You could upload it to a FTP service and provide an URL so we could download it.

All the best,
Angel Petrov
the Telerik team
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 their blog feed now.
Tags
Grid
Asked by
Inayat Rasool Syed
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Inayat Rasool Syed
Top achievements
Rank 1
Angel Petrov
Telerik team
Share this question
or