locked
Gridview Auto generated update not working RRS feed

  • Question

  • User1811156929 posted

    Below showing is the entire source code Pls suggest to work Friends

    <div >

    <asp:gridview runat="server" ID="grdvwRMWO3" AutoGenerateColumns="False" DataKeyNames="R2IGTNo" DataSourceID="SqlDataSource1">
    <Columns>
    <asp:CommandField ShowEditButton="True" />
    <asp:BoundField DataField="R2IGTNo" HeaderText="R2IGTNo" ReadOnly="True" SortExpression="R2IGTNo" />
    <asp:BoundField DataField="SinterLotNo" HeaderText="SinterLotNo" SortExpression="SinterLotNo" />
    <asp:TemplateField HeaderText="WO3Blend" SortExpression="WO3Blend">
    <EditItemTemplate>
    <asp:DropDownList ID="DropDownList1" runat="server">
    <asp:ListItem>1</asp:ListItem>
    <asp:ListItem>2</asp:ListItem>
    <asp:ListItem>3</asp:ListItem>
    </asp:DropDownList>
    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%# Bind("WO3Blend") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="WO3LotNo" HeaderText="WO3LotNo" SortExpression="WO3LotNo" />
    <asp:TemplateField HeaderText="SinterLottype" SortExpression="SinterLottype">
    <EditItemTemplate>
    <asp:DropDownList ID="DropDownList2" runat="server">
    <asp:ListItem>A</asp:ListItem>
    <asp:ListItem>B</asp:ListItem>
    </asp:DropDownList>
    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label2" runat="server" Text='<%# Bind("SinterLottype") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="WO3ECntnrWt" HeaderText="WO3ECntnrWt" SortExpression="WO3ECntnrWt" />
    <asp:TemplateField HeaderText="WO3MagneticPdby" SortExpression="WO3MagneticPdby">
    <EditItemTemplate>
    <asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSource2" DataTextField="Name" DataValueField="Name" AutoPostBack="True">
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:RedtagConnectionString %>" SelectCommand="SELECT Name FROM [Login] WHERE Department LIKE '%CRYSTAL%' AND Status='A' and Name not in ('GIRISH C BUDAKOTI','THANMAYA R')"></asp:SqlDataSource>
    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label3" runat="server" Text='<%# Bind("WO3MagneticPdby") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="WO3MtrlWt" HeaderText="WO3MtrlWt" SortExpression="WO3MtrlWt" />
    <asp:TemplateField HeaderText="WO3Weighedby" SortExpression="WO3Weighedby">
    <EditItemTemplate>
    <asp:DropDownList ID="DropDownList4" runat="server" DataSourceID="SqlDataSource3" DataTextField="Name" DataValueField="Name" AutoPostBack="True">
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:RedtagConnectionString2 %>" SelectCommand="SELECT Name FROM [Login] WHERE Department LIKE '%CRYSTAL%' AND Status='A' and Name not in ('GIRISH C BUDAKOTI','THANMAYA R')"></asp:SqlDataSource>
    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label4" runat="server" Text='<%# Bind("WO3Weighedby") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:gridview>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues" ConnectionString="<%$ ConnectionStrings:CrystalGrowthDbConnectionString3 %>"
    InsertCommand="INSERT INTO [tbl1RMWeighingDetails] ([R2IGTNo], [SinterLotNo], [WO3Blend], [WO3LotNo], [SinterLottype], [WO3ECntnrWt], [WO3MagneticPdby], [WO3MtrlWt], [WO3Weighedby]) VALUES (@R2IGTNo, @SinterLotNo, @WO3Blend, @WO3LotNo, @SinterLottype, @WO3ECntnrWt, @WO3MagneticPdby, @WO3MtrlWt, @WO3Weighedby)" OldValuesParameterFormatString="original_{0}"
    SelectCommand="SELECT [R2IGTNo], [SinterLotNo], [WO3Blend], [WO3LotNo], [SinterLottype], [WO3ECntnrWt], [WO3MagneticPdby], [WO3MtrlWt], [WO3Weighedby] FROM [tbl1RMWeighingDetails]"
    UpdateCommand="UPDATE [tbl1RMWeighingDetails] SET [SinterLotNo] = @SinterLotNo, [WO3Blend] = @WO3Blend, [WO3LotNo] = @WO3LotNo, [SinterLottype] = @SinterLottype, [WO3ECntnrWt] = @WO3ECntnrWt, [WO3MagneticPdby] = @WO3MagneticPdby, [WO3MtrlWt] = @WO3MtrlWt, [WO3Weighedby] = @WO3Weighedby WHERE [R2IGTNo] = @original_R2IGTNo AND [SinterLotNo] = @original_SinterLotNo AND [WO3Blend] = @original_WO3Blend AND [WO3LotNo] = @original_WO3LotNo AND [SinterLottype] = @original_SinterLottype AND [WO3ECntnrWt] = @original_WO3ECntnrWt AND [WO3MagneticPdby] = @original_WO3MagneticPdby AND [WO3MtrlWt] = @original_WO3MtrlWt AND [WO3Weighedby] = @original_WO3Weighedby">

    <InsertParameters>
    <asp:Parameter Name="R2IGTNo" Type="String" />
    <asp:Parameter Name="SinterLotNo" Type="String" />
    <asp:Parameter Name="WO3Blend" Type="Int32" />
    <asp:Parameter Name="WO3LotNo" Type="String" />
    <asp:Parameter Name="SinterLottype" Type="String" />
    <asp:Parameter Name="WO3ECntnrWt" Type="Double" />
    <asp:Parameter Name="WO3MagneticPdby" Type="String" />
    <asp:Parameter Name="WO3MtrlWt" Type="Int32" />
    <asp:Parameter Name="WO3Weighedby" Type="String" />
    </InsertParameters>
    <UpdateParameters>
    <asp:Parameter Name="SinterLotNo" Type="String" />
    <asp:Parameter Name="WO3Blend" Type="Int32" />
    <asp:Parameter Name="WO3LotNo" Type="String" />
    <asp:Parameter Name="SinterLottype" Type="String" />
    <asp:Parameter Name="WO3ECntnrWt" Type="Double" />
    <asp:Parameter Name="WO3MagneticPdby" Type="String" />
    <asp:Parameter Name="WO3MtrlWt" Type="Int32" />
    <asp:Parameter Name="WO3Weighedby" Type="String" />
    <asp:Parameter Name="original_R2IGTNo" Type="String" />
    <asp:Parameter Name="original_SinterLotNo" Type="String" />
    <asp:Parameter Name="original_WO3Blend" Type="Int32" />
    <asp:Parameter Name="original_WO3LotNo" Type="String" />
    <asp:Parameter Name="original_SinterLottype" Type="String" />
    <asp:Parameter Name="original_WO3ECntnrWt" Type="Double" />
    <asp:Parameter Name="original_WO3MagneticPdby" Type="String" />
    <asp:Parameter Name="original_WO3MtrlWt" Type="Int32" />
    <asp:Parameter Name="original_WO3Weighedby" Type="String" />
    </UpdateParameters>
    </asp:SqlDataSource>

    </div>

    Tuesday, August 28, 2018 6:33 AM

All replies

  • User-893317190 posted

    Hi ShankarNaren ,

    Because you use a SqlDataSource as the datasource of your gridview , it only updates  data which is in a boundfield and your columns have a lot of template fields.

    If you want to use sqldatasource with templatefield, you should manually add the value in template field to the data collection which will be updated by sqldatasource.

    Below I use a datepicker  template field  as a sample.

    <div>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="RecordId" DataSourceID="SqlDataSource1" AutoGenerateEditButton="True" OnRowUpdating="GridView1_RowUpdating">
                    <Columns>
                        <asp:BoundField DataField="RecordId" HeaderText="RecordId" ReadOnly="True" SortExpression="RecordId" InsertVisible="False" />
                        <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
                        <asp:TemplateField HeaderText="AdministerDate" >
                                <EditItemTemplate>
                                    <asp:TextBox ID="AdministerDate" runat="server" CssClass="myDate"></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblAdministerDate" runat="server" Text='<%#Bind("AdministerDate") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
    
    
                    </Columns>
                </asp:GridView>
                <script type="text/javascript">
                    $(document).ready(function () {
                        var dp = $('.myDate');
                        dp.datepicker({
                            changeMonth: true,
                            changeYear: true,
                            format: "dd/mm/yyyy",
                            language: "tr"
                        }).on('changeDate', function (ev) {
    
                            $(this).datepicker('hide');
                        });
                    });
                </script>
    
                  <script type="text/javascript">
                       $(function () {
                           $('#ShipDate').datetimepicker();
                       });
                    </script>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:WebFormCases.Models.supplyModelConnectionString %>" SelectCommand="SELECT [RecordId], [FirstName], [AdministerDate] FROM [Record]" DeleteCommand="DELETE FROM [Record] WHERE [RecordId] = @RecordId" InsertCommand="INSERT INTO [Record] ([FirstName], [AdministerDate]) VALUES (@FirstName, @AdministerDate)" UpdateCommand="UPDATE [Record] SET [FirstName] = @FirstName, [AdministerDate] = @AdministerDate WHERE [RecordId] = @RecordId">
                    <DeleteParameters>
                        <asp:Parameter Name="RecordId" Type="Int32" />
                    </DeleteParameters>
                    <InsertParameters>
                        <asp:Parameter Name="FirstName" Type="String" />
                        <asp:Parameter Name="AdministerDate" Type="DateTime" />
                    </InsertParameters>
                    <UpdateParameters>
                        <asp:Parameter Name="FirstName" Type="String" />
                        <asp:Parameter Name="AdministerDate" Type="DateTime" />
                        <asp:Parameter Name="RecordId" Type="Int32" />
                    </UpdateParameters>
                </asp:SqlDataSource>
            </div>
        </form>

    Code behind. Please add value  in the gridview's  RowUpdating event.

     protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
            {
                //get the edit row
                GridViewRow gvr = this.GridView1.Rows[this.GridView1.EditIndex];
                //get the date in the textbox
                TextBox textBox = (TextBox)gvr.FindControl("AdministerDate");
                //convert it to datetime
               DateTime time= DateTime.ParseExact(textBox.Text, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
                //add it to the collection which stores data to be updated
                e.NewValues["AdministerDate"] = time;
            }

    You could also not use sqldatasource and bind data manually. If so , you should also update all the columns  manually in the  rowupdating event.

    Best regards ,

    Ackerly Xu 

    Wednesday, August 29, 2018 3:03 AM
  • User1811156929 posted

    So i understand Manually we need to write the code for dropdown list in template field , auto generated code won't help for data binding in template field.

    Wednesday, August 29, 2018 8:54 AM
  • User-893317190 posted

    Hi ShankarNaren,

    I'm sorry , I haven't stated clearly.

    At least when you use a dropdownlist to edit your column,it is the case.

    However, when you use Bind method, even it is in itemtemplate, it will still be updated by the sqldatasource.

    Bind will not only show the data  but also pass data to the server.

    For example , below code will still be updated automatically.

    <asp:TemplateField HeaderText="country">
                       <ItemTemplate>
                           <asp:Label ID="Label1" runat="server" Text='<%# Eval("Country") %>'></asp:Label>
                       </ItemTemplate>
                       <EditItemTemplate>
                           <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Country") %>'></asp:TextBox>
                       </EditItemTemplate>
                   </asp:TemplateField>

    Best regards,

    Ackerly Xu

    Wednesday, August 29, 2018 9:18 AM
  • User1811156929 posted

    Its not working in my code right. Iam having two template field with dropdown list. its get the data from sql source but in edit mode the value is not taking to server.

    Wednesday, August 29, 2018 11:04 AM
  • User-893317190 posted

    Hi ShankarNaren,

    Yes ,in your case , you should update the data in your template field manually in the gridview's RowUpdating event through the  e.NewVaues property,

    I just want to say in some cases , bind in template field will automatically be updated.

    Best regards,

    Ackerly Xu

    Thursday, August 30, 2018 1:04 AM