Update Detail Table in Radgrid

1 Answer 360 Views
Grid ODataDataSource
axel
Top achievements
Rank 1
axel asked on 09 Nov 2021, 02:44 PM | edited on 22 Nov 2021, 03:29 PM

hello

I am using a radgrid with a master table view and details table, I can update my master table view with a stored procedure however I cannot update detail table with another stored procedure. The two updates must be done simultaneously with the save button of radgrid in edit mode.

Here is what I did for my master table view, it works and i would like to do the same button on my detail table, Can you help me pls


protected void RadGrid1_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {

            GridEditableItem editedItem = e.Item as GridEditableItem;
           
            string pID_OF = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["ID_OF"].ToString();
       
            string pNO_WO = (editedItem["NO_OF"].Controls[0] as TextBox).Text;
            string pRef = (editedItem["CodeArticle"].Controls[0] as TextBox).Text;
            string pQte = (editedItem["QteLancee"].Controls[0] as TextBox).Text;
            string pStatut_WO = (editedItem["StatutOF"].Controls[0] as TextBox).Text;
            string pDate_WO = (editedItem["DateLancement"].Controls[0] as TextBox).Text;
            string pID_Affectation = (editedItem["ID_Affectation"].FindControl("RadCombobox1") as RadComboBox).SelectedValue;


            try
            {
                SqlDSListeOF.UpdateParameters["pID_OF"].DefaultValue = pID_OF;
                SqlDSListeOF.UpdateParameters["pNO_WO"].DefaultValue = pNO_WO;
                SqlDSListeOF.UpdateParameters["pRef"].DefaultValue = pRef;
                SqlDSListeOF.UpdateParameters["pQte"].DefaultValue = pQte;
                SqlDSListeOF.UpdateParameters["pStatut_WO"].DefaultValue = pStatut_WO;
                SqlDSListeOF.UpdateParameters["pDate_WO"].DefaultValue = pDate_WO;
                SqlDSListeOF.UpdateParameters["pID_Affectation"].DefaultValue = pID_Affectation;
                SqlDSListeOF.Update();

            }
            catch (Exception ex)
            {
                e.Canceled = true;
                ErrorAffectation.Text = "Impossible de mettre à jour . " + ex.Message;
            }           
        }

 

 

axel
Top achievements
Rank 1
commented on 09 Nov 2021, 02:46 PM | edited

I'm starting on telerik
Attila Antal
Telerik team
commented on 12 Nov 2021, 08:57 AM

Hi Axel. 

We're glad to hear you're Interested in the Telerik Components. I will try to explain how this works, but first, please share the complete RadGrid markup and all of it's backend code. Since the Grid can be configured in many different ways, it is important that we understand the structure and how it was built. Based on that we can give you a more precise answer.

axel
Top achievements
Rank 1
commented on 15 Nov 2021, 08:43 AM | edited

Thank you for your reply, here is my radgrid and my backend.

ASPX :

 <telerik:RadGrid ID="RadGrid1"  runat="server"  AllowFilteringByColumn="True" AllowPaging="True" AllowSorting="True" DataSourceID="SqlDSListeOF" CellSpacing="-1" GridLines="Both" Skin="Bootstrap" Width="1156px" 
         OnDetailTableDataBind ="RadGrid1_DetailTableDataBind1"  OnUpdateCommand="RadGrid1_UpdateCommand" OnNeedDataSource="RadGrid1_NeedDataSource" Height="119px" style="margin-top: 29px; margin-left: 26px;" AutoGenerateEditColumn="True" Font-Size="Medium">
<GroupingSettings CollapseAllTooltip="Collapse all groups"></GroupingSettings>      
        <MasterTableView DataSourceID="SqlDSListeOF" AutoGenerateColumns="False"  DataKeyNames="ID_OF" BackColor="Transparent" GridLines="Horizontal" Width="" Font-Size="Smaller" NoDetailRecordsText="Aucun enregistrement à afficher" NoMasterRecordsText="Aucun enregistrement à afficher">
            <DetailTables>
                <telerik:GridTableView runat="server"  AutoGenerateEditColumn="false" DataKeyNames="ID_OF,Composant" DataSourceID="SqlDSComposant" AllowFilteringByColumn="False" AutoGenerateColumns="false" BorderStyle="Solid" >
                    <Columns>
                         <telerik:GridBoundColumn DataField="ID_OF" FilterControlAltText="Filter ID_OF column" HeaderText="ID_OF" SortExpression="ID_OF" UniqueName="ID_OF" ReadOnly="true">
                             <ItemStyle HorizontalAlign="Center" />
                </telerik:GridBoundColumn>
                         <telerik:GridBoundColumn DataField="Composant" FilterControlAltText="Filter Composant column" HeaderText="Composant" SortExpression="Composant" UniqueName="Composant" ReadOnly="true">
                             <ItemStyle HorizontalAlign="Center" />
                </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="RefDescription" FilterControlAltText="Filter RefDescription column" HeaderText="RefDescription" SortExpression="RefDescription" UniqueName="RefDescription" ReadOnly="true" Display="false">
                             <ItemStyle HorizontalAlign="Center" />
                </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="AcheteurPlanificateur" FilterControlAltText="Filter AcheteurPlanificateur column" HeaderText="AcheteurPlanificateur" SortExpression="AcheteurPlanificateur" UniqueName="AcheteurPlanificateur" ReadOnly="true" Display="false">
                             <ItemStyle HorizontalAlign="Center" />
                </telerik:GridBoundColumn>
                         <telerik:GridBoundColumn DataField="QteDemandee" FilterControlAltText="Filter QteDemandee column" HeaderText="QteDemandee" SortExpression="QteDemandee" UniqueName="QteDemandee" DataFormatString="{0:N4}" ReadOnly="true">
                             <ItemStyle HorizontalAlign="Right" />
                </telerik:GridBoundColumn>
                        <telerik:GridTemplateColumn DataField="Machine" FilterControlAltText="Filter Machine column" HeaderText="Machine" SortExpression="Machine" UniqueName="Machine" ReadOnly ="true" >
                             <ItemStyle HorizontalAlign="Center" />
                            <ItemTemplate>
                        <asp:Label ID="Nom" runat="server" Text='<%# Eval("Nom") %>'></asp:Label>
                    </ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridTemplateColumn DataField="Description" FilterControlAltText="Filter Description column" HeaderText="Description Machine" SortExpression="Description" UniqueName="Description" ReadOnly ="true" >
                        <ItemStyle HorizontalAlign="Center" />
                            <ItemTemplate>
                        <asp:Label ID="Description" runat="server" Text='<%# Eval("Description") %>'></asp:Label>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                    </Columns>
                    <HeaderStyle Font-Bold="True" Font-Overline="False" HorizontalAlign="Center" BorderStyle="Solid" />
                </telerik:GridTableView>
            </DetailTables>
            <Columns>
                <telerik:GridTemplateColumn UniqueName="CheckBoxTemplateColumn" HeaderStyle-Width="5px">
              <ItemTemplate>
                <asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="ToggleRowSelection"
                  AutoPostBack="True" />
              </ItemTemplate>
              <HeaderTemplate>
                <asp:CheckBox ID="headerChkbox" runat="server" OnCheckedChanged="ToggleSelectedState" Width="20px"
                  AutoPostBack="true" />
              </HeaderTemplate>
                    <ItemStyle HorizontalAlign="Center"/>
            </telerik:GridTemplateColumn>
                <telerik:GridBoundColumn DataField="ID_OF" FilterControlAltText="Filter ID_OF column" HeaderText="ID_OF" SortExpression="ID_OF" UniqueName="ID_OF" ReadOnly="true">
                    <ItemStyle HorizontalAlign="Center" />
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="NO_OF" FilterControlAltText="Filter NO_OF column" HeaderText="NO_OF" SortExpression="NO_OF" UniqueName="NO_OF" ReadOnly="true">
                    <ItemStyle HorizontalAlign="Center" />
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="CodeArticle" FilterControlAltText="Filter CodeArticle column" HeaderText="CodeArticle" SortExpression="CodeArticle" UniqueName="CodeArticle" ReadOnly="true">
                    <ItemStyle HorizontalAlign="Center" />
                </telerik:GridBoundColumn>
                 <telerik:GridBoundColumn DataField="StatutOF" FilterControlAltText="Filter StatutOF column" HeaderText="StatutOF" SortExpression="StatutOF" UniqueName="StatutOF" ReadOnly="true">
                    <ItemStyle HorizontalAlign="Center" />
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="QteLancee"  DataType="System.Double" FilterControlAltText="Filter QteLancee column" HeaderText="QteLancee" SortExpression="QteLancee" UniqueName="QteLancee"  ReadOnly="true" >
                    <ItemStyle HorizontalAlign="Right" />
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="AcheteurPlanificateur" FilterControlAltText="Filter AcheteurPlanificateur column" HeaderText="AcheteurPlanificateur" SortExpression="AcheteurPlanificateur" UniqueName="AcheteurPlanificateur" ReadOnly="true">
                    <ItemStyle HorizontalAlign="Center" />
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Base_MFG" FilterControlAltText="Filter Base_MFG column" HeaderText="Base_MFG" SortExpression="Base_MFG" UniqueName="Base_MFG" ReadOnly="true" Display="false">
                    <ItemStyle HorizontalAlign="Center" Width="5px" />
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="DateLancement" DataFormatString="{0:dd / MM / yyyy}" DataType="System.DateTime" FilterControlAltText="Filter DateLancement column" HeaderText="DateLancement" SortExpression="DateLancement" UniqueName="DateLancement" ReadOnly="true">
                    <ItemStyle HorizontalAlign="Center" />
                </telerik:GridBoundColumn>
                <telerik:GridTemplateColumn DataField="ID_Affectation" FilterControlAltText="Filter ID_Affectation column" HeaderText="ID_Affectation" SortExpression="ID_Affectation" UniqueName="ID_Affectation" ReadOnly="false">
                    <EditItemTemplate>
                        <telerik:RadComboBox runat="server" ID="RadComboBox1" EnableLoadOnDemand="False" DataTextField="Affectation"
                                DataValueField="ID_Affectation" Height="140px" Width="220px" DropDownWidth="420px" DataSourceID="SqlDSAffectation" SelectedValue='<%# Bind("ID_Affectation") %> '>                                                              
                        </telerik:RadComboBox>            
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="AffectationLabel" runat="server" Text='<%# Eval("Affectation") %>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Center" />
                </telerik:GridTemplateColumn>
            </Columns>
            <EditItemStyle BackColor="#00C000" Font-Bold="True" />
             
            <HeaderStyle BackColor="Transparent" Font-Bold="True" HorizontalAlign="Center" Width="10px" />
        </MasterTableView>
    </telerik:RadGrid>

 

BACKEND :


 protected void RadGrid1_DetailTableDataBind1(object sender, GridDetailTableDataBindEventArgs e)
        {
            GridDataItem parentItem = e.DetailTableView.ParentItem as GridDataItem;

            if (parentItem.Edit)
            {
                return;
            }


            SqlDSComposant.SelectParameters["pID_OF"].DefaultValue = parentItem.GetDataKeyValue("ID_OF").ToString();


        }
        protected void ToggleRowSelection(object sender, EventArgs e)
        {
            ((sender as CheckBox).NamingContainer as GridItem).Selected = (sender as CheckBox).Checked;
            bool checkHeader = true;
            foreach (GridDataItem dataItem in RadGrid1.MasterTableView.Items)
            {
                if (!(dataItem.FindControl("CheckBox1") as CheckBox).Checked)
                {
                    checkHeader = false;
                    break;
                }
            }
            GridHeaderItem headerItem = RadGrid1.MasterTableView.GetItems(GridItemType.Header)[0] as GridHeaderItem;
            (headerItem.FindControl("headerChkbox") as CheckBox).Checked = checkHeader;
        }
        protected void ToggleSelectedState(object sender, EventArgs e)
        {
            CheckBox headerCheckBox = (sender as CheckBox);
            foreach (GridDataItem dataItem in RadGrid1.MasterTableView.Items)
            {
                (dataItem.FindControl("CheckBox1") as CheckBox).Checked = headerCheckBox.Checked;
                dataItem.Selected = headerCheckBox.Checked;
            }
        }

        protected void ChoixStatutOF_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlDSListeOF.SelectParameters["pStatutOF"].DefaultValue = ChoixStatutOF.SelectedValue;
        }


        protected void RadGrid1_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {

            GridEditableItem editedItem = e.Item as GridEditableItem;
            // Obtenir la valeur de la clé primaire grâce à DataKeyValue
            string pID_OF = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["ID_OF"].ToString();
            // Accédez à la zone de texte à partir du formulaire d'édition et stockez les valeurs dans des variables de chaîne.
            string pNO_WO = (editedItem["NO_OF"].Controls[0] as TextBox).Text;
            string pRef = (editedItem["CodeArticle"].Controls[0] as TextBox).Text;
            string pQte = (editedItem["QteLancee"].Controls[0] as TextBox).Text;
            string pStatut_WO = (editedItem["StatutOF"].Controls[0] as TextBox).Text;
            string pDate_WO = (editedItem["DateLancement"].Controls[0] as TextBox).Text;
            string pID_Affectation = (editedItem["ID_Affectation"].FindControl("RadCombobox1") as RadComboBox).SelectedValue;



            string day = pDate_WO.Substring(0, 2);
            string month = pDate_WO.Substring(5, 2);
            string year = pDate_WO.Substring(10, 4);
            pDate_WO = (year + "-" + month + "-" + day);

            try
            {
                SqlDSListeOF.UpdateParameters["pID_OF"].DefaultValue = pID_OF;
                SqlDSListeOF.UpdateParameters["pNO_WO"].DefaultValue = pNO_WO;
                SqlDSListeOF.UpdateParameters["pRef"].DefaultValue = pRef;
                SqlDSListeOF.UpdateParameters["pQte"].DefaultValue = pQte;
                SqlDSListeOF.UpdateParameters["pStatut_WO"].DefaultValue = pStatut_WO;
                SqlDSListeOF.UpdateParameters["pDate_WO"].DefaultValue = pDate_WO;
                SqlDSListeOF.UpdateParameters["pID_Affectation"].DefaultValue = pID_Affectation;
                SqlDSListeOF.Update();

            }
            catch (Exception ex)
            {
                e.Canceled = true;
                ErrorAffectation.Text = "Impossible de mettre à jour . " + ex.Message;
            }

            GridDataItem dataItem = e.Item as GridDataItem;
            string pID_WO = (dataItem.OwnerTableView.DetailTables as TextBox).text;
            string pRefComposant = dataItem["Composant"].DetailTables.ToString();
            string pMachine = dataItem["Machine"].ToString();
            string pRefDescription = dataItem["RefDescription"].ToString();
            string pAcheteurPlanificateur = dataItem["AcheteurPlanificateur"].ToString();
            string pQteOr = dataItem["QteDemandee"].ToString();
            
            try
            {
                SqlDSComposant.UpdateParameters["pID_WO"].DefaultValue = pID_WO;
                SqlDSComposant.UpdateParameters["pMachine"].DefaultValue = pMachine;
                SqlDSComposant.UpdateParameters["pRefComposant"].DefaultValue = pRefComposant;
                SqlDSComposant.UpdateParameters["pQteDemandee"].DefaultValue = pQteOr;
                SqlDSComposant.UpdateParameters["pRefDescription"].DefaultValue = pRefDescription;
                SqlDSComposant.UpdateParameters["pAcheteurPlanificateur"].DefaultValue = pAcheteurPlanificateur;
                SqlDSComposant.Update();

            }
            catch (Exception ex)
            {
                e.Canceled = true;
                ErrorAffectation.Text = "Impossible de mettre à jour . " + ex.Message;
            }
        }

        protected void AffectationChecked_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            
        }

        protected void RadButton1_Click(object sender, EventArgs e)
        {
            string Affectation = AffectationChecked.SelectedValue;
            if (string.IsNullOrEmpty(AffectationChecked.Text))
            {
                ErrorAffectationMAJ.Text = "Veuillez sélectionner une affectation . ";
            }
            else
            { 
                foreach (GridDataItem dataItem in RadGrid1.Items)
                {
                    CheckBox chk = (CheckBox)dataItem.FindControl("CheckBox1");
                    if (chk.Enabled)
                    {

                        if (chk.Checked == true)
                        {
                            
                            string pID_OF = dataItem.OwnerTableView.DataKeyValues[dataItem.ItemIndex]["ID_OF"].ToString();
                            // Accédez à la zone de texte à partir du formulaire d'édition et stockez les valeurs dans des variables de chaîne.
                            string pNO_WO = (dataItem["NO_OF"].Text);
                            string pRef = (dataItem["CodeArticle"].Text);
                            string pQte = (dataItem["QteLancee"].Text);
                            string pStatut_WO = (dataItem["StatutOF"].Text);
                            string pDate_WO = (dataItem["DateLancement"].Text);



                            string day = pDate_WO.Substring(0, 2);
                            string month = pDate_WO.Substring(5, 2);
                            string year = pDate_WO.Substring(10, 4);
                            pDate_WO = (year + "-" + month + "-" + day);

                            try
                            {
                                SqlDSListeOF.UpdateParameters["pID_OF"].DefaultValue = pID_OF;
                                SqlDSListeOF.UpdateParameters["pNO_WO"].DefaultValue = pNO_WO;
                                SqlDSListeOF.UpdateParameters["pRef"].DefaultValue = pRef;
                                SqlDSListeOF.UpdateParameters["pQte"].DefaultValue = pQte;
                                SqlDSListeOF.UpdateParameters["pStatut_WO"].DefaultValue = pStatut_WO;
                                SqlDSListeOF.UpdateParameters["pDate_WO"].DefaultValue = pDate_WO;
                                SqlDSListeOF.UpdateParameters["pID_Affectation"].DefaultValue = Affectation;
                                SqlDSListeOF.Update();
                            }
                            catch (Exception ex)
                            {

                                ErrorAffectationChecked.Text = "Impossible de mettre à jour . " + ex.Message;
                            }



                        }


                    }
                    RadGrid1.Rebind();
                }
            }
I did not put all the code only what you may be interested in, thank you for your help :)

1 Answer, 1 is accepted

Sort by
0
Accepted
Attila Antal
Telerik team
answered on 17 Nov 2021, 10:18 AM

Hi Axel. 

Thanks for sharing the details. I just needed to make sure that you're using the right structure for this scenario.

By looking at the code I found a few errors which may be the reason for the issue.

 

GridTableViewCollection object cannot be converted to a TextBox

string pID_WO = (dataItem.OwnerTableView.DetailTables as TextBox).text;

 

Since that is a collection of TableViews, the correct way to reference that is:

GridTableViewCollection tableViews = dataItem.OwnerTableView.DetailTables;

You can then loop through the collection and find each TableView.

 

The TableCell object does not have a Property called "DetailTables"

string pRefComposant = dataItem["Composant"].DetailTables.ToString();


If you want to access the Cell value, you access the Cell, then access its Text.

// This is a TableCell object
TableCell composantCell = dataItem["Composant"];
// This is the TableCell's Text
string cellText = composantCell.Text;

Please fix these errors first.

 

Then you can check out the Server-Side section in the Accessing Values and Controls article for examples of accessing Tables, Cells, Values, etc. This will help you understand how to collect data from the Grid.

For debugging tips while working on the server you can watch the videos from the 6 Server-Side Debugging Tips That Will Make Developing Easier Blog post. Knowing how to debug your app can be very useful and time-saving.

 

Regards,
Attila Antal
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

axel
Top achievements
Rank 1
commented on 22 Nov 2021, 03:28 PM

Thanks for your help it works
Tags
Grid ODataDataSource
Asked by
axel
Top achievements
Rank 1
Answers by
Attila Antal
Telerik team
Share this question
or